Ducky
Ducky is a state library for .NET and Blazor: slices of immutable state, reduced by actions on a single dispatcher, with effects for the work that leaves the store.
Quick start
Reference the Ducky package directly from every project that declares actions or slices. A slice declares its initial state and one reducer per action type:
public sealed record Counter(int Value);
public sealed record Increment(int By);
public sealed class CounterSlice : Slice<Counter>
{
public CounterSlice() => On<Increment>((state, action) => state with { Value = state.Value + action.By });
protected override Counter Initial => new(0);
}Register the assembly's slices and effects with the generated AddDuckyGenerated_{Assembly}() (namespace Ducky.Generated), initialize the store and dispatch:
await using var services = new ServiceCollection()
.AddDucky(d => d.AddDuckyGenerated_Ducky_Docs_Tests()) // generated: every slice and effect of this assembly
.BuildServiceProvider();
var store = services.GetRequiredService<IStore>();
await store.InitializeAsync(cancellationToken);
var result = await store.DispatchAsync(new Increment(2));
var count = store.State.Get<Counter>().Value; // 2Every snippet on this site is a region of a compiled, tested file.
Where to go next
- The tutorial builds a small duck shop from an empty Blazor WebAssembly app to a tested one, one idea per step, for readers new to Redux-style stores.
- Getting started builds a first slice, an effect and a component, step by step.
- Effects and concurrency policies covers the part most state libraries leave to you: what happens when an action arrives while its effect is still running.
- Render modes and the load rule shows the one rule that works in WebAssembly, Interactive Server, Interactive Auto and static SSR.
- How Ducky compares sets Ducky beside Fluxor, TimeWarp.State and a hand-written state container, including where the others are ahead; Migrating from Fluxor maps a Fluxor app to Ducky.
- The playground, linked at the top of every page, is a small shop built only from Ducky: switch on X-ray to see which feature handles each part, with its source.