BLState 2.0.0

dotnet add package BLState --version 2.0.0
                    
NuGet\Install-Package BLState -Version 2.0.0
                    
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="BLState" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BLState" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="BLState" />
                    
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 BLState --version 2.0.0
                    
#r "nuget: BLState, 2.0.0"
                    
#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 BLState@2.0.0
                    
#: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=BLState&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=BLState&version=2.0.0
                    
Install as a Cake Tool

BLState Simple State Management For Blazor

Simple source generator library for working with global state and updates in Blazor. Use dataannotations to declare stores and values.

Prerequisites

Blazor WASM or Server project based on .NET 6 or higher.

How to use BLState

Download package

  • Download the package trough nuget store.
  • Or add via CLI - dotnet add package BLState.
  • Or add a reference directly in the project file. <PackageReference Include="BLState" Version="2.0.0" />.

Note: If you are using Visual Studio you might have to restart Visual Studio after downloading the package for the first time to trigger start of source generation

Register Services

First register BLState so the stores so they can be used with dependency injection. Each store registers with a scoped lifetime

// In program.cs
using BLState;
builder.Services.AddBLStore();

Add a Store

Create a public partial class and anotate it with the attribute [BLStore]. For each value in the store add a private field with the annotation BLValue.

using BLState;

...

[BLStore]
public partial class CountStore
{
    [BLValue]
    private int count = 1;
    
    [BLValue]
    private int _multiplier = 3;
}

The example above creates a store named CountStore with the properties Count and Multiplier.

Use in a component

To use in a component

  1. Inject the store (don't forget to add a using directive to where you have defined your store).
  2. Add a BLUpdate component to subscribe to changes in a specific store. This component requires two arguments. A store it will listen to and a OnUpdate action that will be triggerd when an update occurs.
  3. Access the values in the store by the generated property names.
  4. Update a value, this change will be reflected in all components that uses the store.
@inject CountStore CountStore

<BLUpdate Store="CountStore" OnUpdate="StateHasChanged" /> 
<p>Current count: @CountStore.Count</p>  

<button @onclick="MultiplyValue">Multiply</button>

@code {  
   private void MultiplyValue()
   {
       // Sets a new value to the count filed in CountStore
       CountStore.Count *= CountStore.Multiplier;
   }
}

Trigger update on reference type

When making a change to a reference type without changing the actual reference the store needs to be told that a update occured. To do this use the Update or UpdateAsync method.

Example a store with a list.

[BLStore]
public partial class UserStore
{
    [BLValue]private List<string> names = new List<string>();
}

Then in the compoenent

using BLState;
@inject UserStore UserStore
...

UserStore.Update(x => x.Names.Add("Scott"));

//Or the async variant
await UserStore.UpdateAsync(async x => x.Names.Add(await GetName()));

To manually trigger that the store has been updated call the method InvokeUpdates on the store.

UserStore.Names.Add("Scott");
UserStore.InvokeUpdates();

Custom property names

For the need of custom property names of the values in the store add a string to the BLValue attribute [BLValue(PropertyName = "CustomName")] private string secretName = "";. The public property will in this case be named CustomName.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.0.0 418 7/2/2023 2.0.0 is deprecated because it has critical bugs.
1.0.0 623 4/17/2022