Apparatus.Blazor.State 2.1.24

dotnet add package Apparatus.Blazor.State --version 2.1.24
NuGet\Install-Package Apparatus.Blazor.State -Version 2.1.24
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Apparatus.Blazor.State" Version="2.1.24" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Apparatus.Blazor.State --version 2.1.24
#r "nuget: Apparatus.Blazor.State, 2.1.24"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Apparatus.Blazor.State as a Cake Addin
#addin nuget:?package=Apparatus.Blazor.State&version=2.1.24

// Install Apparatus.Blazor.State as a Cake Tool
#tool nuget:?package=Apparatus.Blazor.State&version=2.1.24

Apparatus.Blazor.State

Apparatus.Blazor.State is library which provides centralized state management for Blazor webassembly based implementations.

Build Status Azure DevOps tests Azure DevOps coverage Nuget

Getting Started

1. Create Blazor webassembly project by running:

dotnet new blazorwasm -o BlazorWebAssemblyApp

2. Install the standard Nuget package into your ASP.NET Blazor project.

    Package Manager : Install-Package Apparatus.Blazor.State 
    CLI : dotnet add package Apparatus.Blazor.State

3. In Program.cs add below line to register the service:

	builder.Services.AddStateManagement(typeof(Program).Assembly);

4. Under States folder, create CounterState.cs class which shousl represent Counter page/component state, also all states should inherit Apparatus.Blazor.State.Contracts.IState interface.

    public class CounterState : IState
    {
        public int CurrentCount { get; set; }
    }

5. Create IncrementCount.cs action in "Actions" folder - all actions should inherit Apparatus.Blazor.State.Contracts.IAction interface.

    public class IncrementCount : IAction
    {
        public int IncrementBy { get; set; }
    }

6. Inherit BlazorStateComponent in Counter.razor page/component, reference CounterState in the component and implement Action Dispatcher:

	@inherits Apparatus.Blazor.State.BlazorStateComponent
    ....
<p role="status">Current count: @State.CurrentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    CounterState State => GetState<CounterState>(); 
    [Inject] IActionDispatcher Dispatcher { get; set; }

    private void IncrementCount()
    {
        Dispatcher.Dispatch(new IncrementCount { /*IncrementBy = 2*/ });
    }
}

7. Implement Action Handler - Under "Handlers" folder create IncrementCountHandler.cs

  public class IncrementCountHandler : IActionHandler<IncrementCount>
    {
        private IStore<CounterState> _counterStore;
        public IncrementCountHandler(IStore<CounterState> counterStore)
        {
            _counterStore = counterStore; 
        }
        
        public Task Handle(IncrementCount action)
        {
            var newState = _counterStore.State;
            newState.CurrentCount++;
            /*
             * or use action property for increment value
             * newState.CurrentCount = newState.CurrentCount + action.IncrementBy;
             */
            return _counterStore.SetState(newState); 
        }
    }

In this way you can keep your states separated from logic and components/pages.

Subscribe to Action without creating Handler

Subscribe from Blazor component.

 @code {
   [Inject] IActionSubscriber ActionSubscriber { get; set; }
	ActionSubscriber.Subscribe<MyAction>((action) => { ... });
 }

For more complex use cases please check SampleApp available in the repository.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.24 65 6/10/2024
2.1.23 221 12/11/2023
2.1.22 103 12/11/2023
2.1.21 102 12/11/2023
2.1.17 103 12/10/2023
2.1.16 101 12/10/2023
2.1.15 109 12/10/2023
2.1.14 109 12/10/2023
2.1.13 110 12/10/2023
2.1.12 105 12/10/2023
2.1.11 113 12/10/2023
2.0.1 332 11/19/2022
1.0.764 320 11/11/2022
1.0.763 327 11/6/2022
1.0.762 326 11/6/2022
1.0.759 333 11/6/2022
1.0.758 318 11/5/2022
1.0.757 331 11/5/2022
1.0.756 345 11/5/2022
1.0.755 324 11/5/2022
1.0.754 317 11/5/2022
1.0.753 333 11/5/2022
1.0.752 329 11/5/2022
1.0.751 342 11/5/2022
1.0.750 334 11/5/2022
1.0.749 334 11/5/2022
1.0.748 342 11/4/2022
1.0.747 345 11/4/2022
1.0.746 333 11/4/2022
1.0.745 348 11/4/2022
1.0.744 341 11/4/2022
1.0.743 342 11/4/2022
1.0.742 358 11/4/2022
1.0.741 339 11/4/2022
1.0.740 346 11/4/2022
1.0.739 347 11/4/2022
1.0.738 337 11/4/2022
1.0.737 344 11/4/2022
1.0.736 308 11/3/2022
1.0.735 361 11/3/2022