Apparatus.Blazor.State 2.1.44

dotnet add package Apparatus.Blazor.State --version 2.1.44
                    
NuGet\Install-Package Apparatus.Blazor.State -Version 2.1.44
                    
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.44" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Apparatus.Blazor.State" Version="2.1.44" />
                    
Directory.Packages.props
<PackageReference Include="Apparatus.Blazor.State" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Apparatus.Blazor.State --version 2.1.44
                    
#r "nuget: Apparatus.Blazor.State, 2.1.44"
                    
#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.
#:package Apparatus.Blazor.State@2.1.44
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Apparatus.Blazor.State&version=2.1.44
                    
Install as a Cake Addin
#tool nuget:?package=Apparatus.Blazor.State&version=2.1.44
                    
Install as a Cake Tool

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.44 151 12/28/2024
2.1.43 123 12/28/2024
2.1.42 139 12/28/2024
2.1.41 138 12/28/2024
2.1.40 123 12/28/2024
2.1.39 137 12/28/2024
2.1.38 134 12/28/2024
2.1.37 137 12/28/2024
2.1.36 146 12/28/2024
2.1.35 135 12/28/2024
2.1.34 123 12/28/2024
2.1.33 135 12/28/2024
2.1.32 125 12/28/2024
2.1.31 134 12/28/2024
2.1.30 156 12/28/2024
2.1.29 128 12/28/2024
2.1.26 149 10/28/2024
2.1.25 131 10/28/2024
2.1.24 151 6/10/2024
2.1.23 286 12/11/2023
2.1.22 179 12/11/2023
2.1.21 170 12/11/2023
2.1.17 173 12/10/2023
2.1.16 172 12/10/2023
2.1.15 184 12/10/2023
2.1.14 187 12/10/2023
2.1.13 179 12/10/2023
2.1.12 180 12/10/2023
2.1.11 196 12/10/2023
2.0.1 428 11/19/2022
1.0.764 411 11/11/2022
1.0.763 430 11/6/2022
1.0.762 424 11/6/2022
1.0.759 427 11/6/2022
1.0.758 407 11/5/2022
1.0.757 429 11/5/2022
1.0.756 437 11/5/2022
1.0.755 420 11/5/2022
1.0.754 417 11/5/2022
1.0.753 430 11/5/2022
1.0.752 425 11/5/2022
1.0.751 430 11/5/2022
1.0.750 429 11/5/2022
1.0.749 433 11/5/2022
1.0.748 447 11/4/2022
1.0.747 442 11/4/2022
1.0.746 432 11/4/2022
1.0.745 437 11/4/2022
1.0.744 432 11/4/2022
1.0.743 437 11/4/2022
1.0.742 459 11/4/2022
1.0.741 440 11/4/2022
1.0.740 441 11/4/2022
1.0.739 441 11/4/2022
1.0.738 427 11/4/2022
1.0.737 448 11/4/2022
1.0.736 412 11/3/2022
1.0.735 452 11/3/2022