Skip to content

Getting started

Introduction

Blex is lightweight, source-generator-powered state management for Blazor and .NET MAUI. You write one [BlexState] field and one [BlexAction] method; the generator writes the reactive properties, the dispatch plumbing, the JSON snapshots and the loading and error lifecycle - and Redux DevTools time travel is built in.

The whole store

This is a complete store, not a sketch of one - it compiles and runs as it stands. There are no action classes, no reducers, no feature registrations and no selector boilerplate.

CounterStore.cs
[BlexStore(Name = "counter", Persist = true)]
public partial class CounterStore
{
    [BlexState] private int _count;
    [BlexState] private int _step = 1;

    [BlexComputed] private int  ComputeDoubleCount() => Count * 2;
    [BlexComputed] private bool ComputeIsEven()      => Count % 2 == 0;

    [BlexAction] private void OnIncrement()       => Count += Step;
    [BlexAction] private void OnDecrement()       => Count -= Step;
    [BlexAction] private void OnSetStep(int step) => Step = step;
}

From that, the generator emits:

  • reactive Count / Step properties whose setters flow through the dispatch pipeline,
  • memoized DoubleCount / IsEven accessors that invalidate themselves on every state change,
  • public Increment(), Decrement() and SetStep(int) wrappers that batch their mutations into one named, recorded action,
  • typed descriptors of those actions - CounterStore.Actions.SetStep - to match and dispatch them as data,
  • SerializeState() / DeserializeState() for snapshots, persistence and time-travel,
  • and the BlexStoreBase base type itself.
Live: That store, running right now

This site's own CounterStore, resolved from DI: the store above plus one more action, Apply preset, used by other pages of these docs. Open the Inspector at the bottom of the page to watch the actions stream in.

Count loading
DoubleCount loading
IsEven loading
Step loading

This store is marked Persist = true - reload the page and the count survives.

Why it exists

Fluxor is the de-facto Redux implementation for Blazor, and it works - but a single counter costs you a state class, an action class, a reducer and often an effect class, and there is no first-class DevTools time-travel. Blex keeps what makes Flux good - one observable state tree, named actions, a middleware pipeline - and deletes the ceremony with a Roslyn incremental generator.

TaskFluxorBlex
Define a piece of stateFeature + State classone [BlexState] field
Define an actionAction class + Reducer methodone [BlexAction] method
Action payloads in DevTools / middlewaremanualautomatic (ctx.Args)
Match an action and read its payload, typedby action classgenerated Store.Actions.Xxx descriptors
State changes only through actionsby convention[BlexStore(EnforceActions = true)], checked by the compiler
Derived statemanual selectors[BlexComputed], memoized
Async side-effectsEffect classes[BlexEffect] with generated loading/error
Effect cancellation & concurrencymanualCancellationToken + Latest/Drop/Queue
Ad-hoc batched mutations✗store.Batch(name, …)
Reset to initial statemanualstore.ResetState()
Granular re-rendermanual selectorsselector Subscribe(…), <BlexSelect> in markup
One store instance per dialog / tabmanual<BlexScope> / manager.CreateScope
Normalized collectionsmanualBlexEntityAdapter / BlexEntityState
Persistence3rd-partybuilt in, with debounce + migrations + per-field opt-out
Undo / redo✗BlexHistory, app-wide or scoped to chosen stores
Redux DevTools time-travel✗✓ built in, dispatcher included
Test helpers✗Bit.Blex.Testing harness, typed log, replay
Deterministic time in tests✗TimeProvider for debounces and timestamps
Boilerplatehighgenerated away

What's in the box

Installation

The packages, the DI registrations and every option, the <BlexProvider> around the router, each hosting model, headless hosts and trimming.

Quick start

A complete, reactive, time-travelable counter in three files and about twenty lines.

.NET MAUI

Blazor Hybrid and native XAML apps: UseBitBlex, singleton registrations, binding XAML to stores, Preferences persistence, queries and the UI thread.

Stores & state

[BlexStore] and [BlexState]: the reactive container, its fields, change detection and its JSON snapshot.

Computed state

[BlexComputed]: memoized derived values that are recalculated only after the state changes.

Actions

[BlexAction] methods, typed action descriptors, dispatch by name, batching, "Set X" assignments, EnforceActions, Batch, ResetState, rollback on error, transactions across stores and async actions.

Effects

[BlexEffect]: async work with a generated loading flag, error slot, cancellation, concurrency policy, debounce, throttling, retries and timeouts.

Server state & queries

BlexQueryClient: a cache for server data - shared fetches, freshness, retries, invalidation, mutations with optimistic rollback, and the prerender handoff.

Scoped stores

One instance of a store per dialog, tab or document: <BlexScope> and CreateScope, instance keys, and persistence that follows each instance.

Components & selectors

BlexComponentBase, <BlexSelect>, <BlexView> and selector subscriptions, so only the markup that uses a value re-renders - plus IObservable for Rx.

Cross-store coordination

BlexManager: react to one store's actions from another, and derive values that span stores.

Entity adapter

BlexEntityState and BlexEntityAdapter: normalized collections with generated CRUD and automatic sorting.

Middleware

IBlexMiddleware: observe every action after it applies, veto it before it runs, or report it when it throws.

Error isolation

Where the failures Blex keeps out of the dispatch pipeline go - options.OnError and BlexError - and logging, tracing and metrics.

Persistence

Persist = true: rehydration on startup and on late registration, transient fields, debounced writes, versioning, migrations, sync across tabs and encrypted storage.

Undo & redo

BlexHistory: in-app undo and redo over the state tree or a chosen scope of it, with labels, jumps, grouping and filters.

DevTools & time travel

The Redux DevTools bridge: every action and state tree streamed to the extension, time travel, the dispatcher, and redaction.

Testing

Bit.Blex.Testing: the test harness, the action log, typed payload assertions, deterministic time, replay and waiting on async state.

Compile-time diagnostics

Every BLEX diagnostic the generator can report, with the store it rejects and the fix.

API reference

Every public type Blex ships, grouped by assembly and namespace, with the page that explains it.

MCP server

The Model Context Protocol server this site hosts, so an AI agent works against Blex's real API, docs and generator.

Packages

Package Description
Bit.Blex Everything a Blazor app needs: the runtime (BlexStoreBase, attributes, dispatch, middleware, persistence, entity adapter, undo/redo and BlexManager), the Roslyn incremental generator that writes the store plumbing (packed as an analyzer), and the Blazor integration, in the same Bit.Blex namespace: <BlexProvider>, BlexComponentBase, browser-storage persistence and the Redux DevTools bridge.
Bit.Blex.Maui .NET MAUI integration: UseBitBlex() startup initializer, Preferences-backed persistence and XAML-bindable stores.
Bit.Blex.Testing Test harness and assertions: BlexTestHarness, BlexActionLog, WaitForAsync, ReplayAsync.
The docs site is the demo

Every page here runs against real stores registered in this app's Program.cs. The Inspector at the bottom of the window shows the live action feed, the serialized global state tree, the undo stack and the error sink - all fed by the same public APIs you are reading about.

An unhandled error has occurred. Reload 🗙