Apparatus.Blazor.State
2.1.23
dotnet add package Apparatus.Blazor.State --version 2.1.23
NuGet\Install-Package Apparatus.Blazor.State -Version 2.1.23
<PackageReference Include="Apparatus.Blazor.State" Version="2.1.23" />
paket add Apparatus.Blazor.State --version 2.1.23
#r "nuget: Apparatus.Blazor.State, 2.1.23"
// Install Apparatus.Blazor.State as a Cake Addin
#addin nuget:?package=Apparatus.Blazor.State&version=2.1.23
// Install Apparatus.Blazor.State as a Cake Tool
#tool nuget:?package=Apparatus.Blazor.State&version=2.1.23
Apparatus.Blazor.State
Apparatus.Blazor.State is library which provides centralized state management for Blazor webassembly based implementations.
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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Components (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
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.23 | 208 | 12/11/2023 |
2.1.22 | 101 | 12/11/2023 |
2.1.21 | 100 | 12/11/2023 |
2.1.17 | 101 | 12/10/2023 |
2.1.16 | 99 | 12/10/2023 |
2.1.15 | 107 | 12/10/2023 |
2.1.14 | 107 | 12/10/2023 |
2.1.13 | 107 | 12/10/2023 |
2.1.12 | 102 | 12/10/2023 |
2.1.11 | 111 | 12/10/2023 |
2.0.1 | 330 | 11/19/2022 |
1.0.764 | 319 | 11/11/2022 |
1.0.763 | 320 | 11/6/2022 |
1.0.762 | 325 | 11/6/2022 |
1.0.759 | 332 | 11/6/2022 |
1.0.758 | 317 | 11/5/2022 |
1.0.757 | 330 | 11/5/2022 |
1.0.756 | 343 | 11/5/2022 |
1.0.755 | 323 | 11/5/2022 |
1.0.754 | 316 | 11/5/2022 |
1.0.753 | 332 | 11/5/2022 |
1.0.752 | 328 | 11/5/2022 |
1.0.751 | 340 | 11/5/2022 |
1.0.750 | 333 | 11/5/2022 |
1.0.749 | 333 | 11/5/2022 |
1.0.748 | 341 | 11/4/2022 |
1.0.747 | 344 | 11/4/2022 |
1.0.746 | 332 | 11/4/2022 |
1.0.745 | 347 | 11/4/2022 |
1.0.744 | 340 | 11/4/2022 |
1.0.743 | 341 | 11/4/2022 |
1.0.742 | 356 | 11/4/2022 |
1.0.741 | 338 | 11/4/2022 |
1.0.740 | 345 | 11/4/2022 |
1.0.739 | 346 | 11/4/2022 |
1.0.738 | 336 | 11/4/2022 |
1.0.737 | 343 | 11/4/2022 |
1.0.736 | 307 | 11/3/2022 |
1.0.735 | 359 | 11/3/2022 |