Apparatus.Blazor.State 2.1.11

There is a newer version of this package available.
See the version list below for details.
dotnet add package Apparatus.Blazor.State --version 2.1.11
NuGet\Install-Package Apparatus.Blazor.State -Version 2.1.11
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.11" />
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.11
#r "nuget: Apparatus.Blazor.State, 2.1.11"
#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.11

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

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 67 6/10/2024
2.1.23 222 12/11/2023
2.1.22 104 12/11/2023
2.1.21 103 12/11/2023
2.1.17 104 12/10/2023
2.1.16 102 12/10/2023
2.1.15 110 12/10/2023
2.1.14 110 12/10/2023
2.1.13 111 12/10/2023
2.1.12 106 12/10/2023
2.1.11 114 12/10/2023
2.0.1 333 11/19/2022
1.0.764 321 11/11/2022
1.0.763 328 11/6/2022
1.0.762 327 11/6/2022
1.0.759 334 11/6/2022
1.0.758 319 11/5/2022
1.0.757 332 11/5/2022
1.0.756 346 11/5/2022
1.0.755 325 11/5/2022
1.0.754 318 11/5/2022
1.0.753 334 11/5/2022
1.0.752 330 11/5/2022
1.0.751 343 11/5/2022
1.0.750 335 11/5/2022
1.0.749 335 11/5/2022
1.0.748 343 11/4/2022
1.0.747 346 11/4/2022
1.0.746 334 11/4/2022
1.0.745 349 11/4/2022
1.0.744 342 11/4/2022
1.0.743 343 11/4/2022
1.0.742 364 11/4/2022
1.0.741 340 11/4/2022
1.0.740 347 11/4/2022
1.0.739 348 11/4/2022
1.0.738 338 11/4/2022
1.0.737 345 11/4/2022
1.0.736 309 11/3/2022
1.0.735 362 11/3/2022