MapBlazor: Architecture of a Blazor Component Library for ArcGIS Maps (Series)

MapBlazor is a Razor Class Library that puts the ArcGIS Maps SDK for JavaScript (Esri) behind a typed .NET API. A Blazor app, whether Server, WebAssembly or Auto, can add 2D maps and 3D scenes, layers, basemaps, drawing, selection, popups and widgets without writing JavaScript. This series covers the architecture behind it: how the JavaScript SDK is bundled and shipped, how the SDK is bootstrapped, how .NET and JS talk to each other, how map state flows back into Blazor, and how it is packaged and tested. Part Topic 01 Goals, project layout and the build pipeline 02 Configuration-first SDK bootstrapping and pluggable authentication (bring your own IdP) 03 The .NET ↔ JavaScript bridge: registry, DTOs, callbacks, disposal 04 The MapView<TMap> component and per-map services 05 Layers, basemaps, 2D/3D, drawing, selection, popups and widgets 06 State with Fluxor, error contracts, packaging and testing Stack: .NET 10 Razor Class Library, ArcGIS Map...

MapBlazor Architecture, Part 6: Fluxor State, Errors, Packaging and Testing

State

The library publishes what happens on a map as Fluxor state and actions:

[FeatureState]
public sealed record MapViewState(ImmutableDictionary<string, MapState> Maps)
{
    public MapViewState() : this(ImmutableDictionary<string, MapState>.Empty) { }
}

public sealed record MapState(Extent? Extent, double Zoom, double Scale, string Basemap,
                              ViewType ViewType, bool IsBusy, MapPoint? LastClick);
  • State is keyed by map id, so the same feature serves every map on the page.
  • Layer state (which layers, visible, opacity, load status) is also per map. A single global layer list mixes maps up.
  • Actions are generic over the map: MapExtentChanged<TMap>, MapClicked<TMap>, LayerAdded<TMap>, DrawCompleted<TMap>, PopupActionTriggered<TMap>, TokenExpired.

Consumers choose a style:

@inject IState<MapViewState> MapState              // render from state
...
SubscribeToAction<MapClicked<DetailMap>>(a => ShowDetails(a.Point));   // react to events

Keep reducers pure. Do JS calls in effects or services, never in reducers. Add a reducer for every success action you dispatch, including scene layers and opacity changes, so state never drifts from the map.

Error contracts

Pick one contract and use it everywhere:

Situation Contract
Expected failure (layer URL 404, query error, user cancelled a draw) Result / Result<T> (FluentResults) with a reason
Programming error (map not initialized, unknown layer id) Exception (InvalidOperationException)
JS-side failure JS returns { ok: false, error }. .NET turns it into Result.Fail
export async function addFeatureLayer(mapId: string, dto: FeatureLayerDto): Promise<JsResult> {
  try { const l = new FeatureLayer(dto); await l.load(); attach(mapId, dto.id, l, dto.index); return { ok: true }; }
  catch (e) { return { ok: false, error: describe(e) }; }
}

Mixing bool, void and silent console.error catches makes failures invisible to consumers.

Logging: use ILogger<T> on the .NET side. On the JS side, use a small logger gated by the configured log level. Never log configuration objects that may contain keys or tokens.

Packaging

<PropertyGroup>
  <PackageId>MapBlazor</PackageId>
  <Version>1.4.0-sdk5.0.18</Version>
  <PackageReadmeFile>README.md</PackageReadmeFile>
  <StaticWebAssetBasePath>_content/MapBlazor</StaticWebAssetBasePath>
</PropertyGroup>
  • The package carries wwwroot/js (bundles and chunks) and wwwroot/assets (SDK assets). That's large, about tens of MB, because the SDK's workers and locales are included.
  • Offer a CDN mode as well: AssetsPath = https://js.arcgis.com/5.0/@arcgis/core/assets. Self-hosting is still the default for intranets.
  • Build packages in CI (dotnet pack -c Release) and push them to a feed. Don't commit .nupkg files to the repository.

Testing

Layer How
TS units node:test or Vitest on pure modules (extent rounding, URL → layer-type inference, scale labels) with SDK modules stubbed
.NET units xUnit on DTO mapping, reducers, services with a fake MapJs
Components bUnit: MapView renders a container, calls init after the first render, disposes the engine
End to end Playwright against the sample app: map appears, layer loads, draw → select returns ids, 2D↔3D keeps layers
[Fact]
public void Extent_changes_are_kept_per_map()
{
    var s = new MapViewState();
    s = Reducers.OnExtent(s, new MapExtentChanged<OverviewMap>(E1, 1000, 5));
    s = Reducers.OnExtent(s, new MapExtentChanged<DetailMap>(E2, 500, 7));
    Assert.Equal(E1, s.Maps["overview"].Extent);
    Assert.Equal(E2, s.Maps["detail"].Extent);
}

Keep the sample app compiling in CI. It's the first thing new users copy.

Checklist

  • ☐ Config-first bootstrap; the facade can't load the SDK early
  • ☐ Every JS callback, handle and layer cache is per map
  • ☐ MapView disposes its map
  • ☐ Tokens come from a host-supplied IMapTokenProvider; Bearer tokens only go to trusted servers
  • ☐ One error contract across services
  • ☐ Host controls height, widgets, theme and locale
  • ☐ Package version shows the SDK version. Docs are generated from the code
  • ☐ Unit, component and end-to-end tests in CI, including the sample app

Comments

Popular posts from this blog

My Cloud Notes

My CSharp Notes