Blaztore 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Blaztore --version 1.1.2
NuGet\Install-Package Blaztore -Version 1.1.2
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="Blaztore" Version="1.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blaztore --version 1.1.2
#r "nuget: Blaztore, 1.1.2"
#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 Blaztore as a Cake Addin
#addin nuget:?package=Blaztore&version=1.1.2

// Install Blaztore as a Cake Tool
#tool nuget:?package=Blaztore&version=1.1.2

Blaztore

.NET

blaztore home

A simple and modest library to implement Flux/Redux pattern in .NET Blazor.

Introduction

If you are not familiar with the Flux/Redux model, have a look on the following nice resources:

Goal

Blazor does not contain a native Flux/Redux or MVU api internally.

The objective of this library is to provide a very simple and minimalist api to implement a Flux/Redux architecture.

This repository is largely inspired by the following existing repositories:

If you are not satisfied by this library, don't hesitate to check them out, they are great.

Advantages

Compared to the listed existing libraries, Blaztore has the following advantages:

  • ✅ Centered on immutability of every concepts (State, Action, ...)
  • ✅ Never force us to inherit from a base class or a base record. Every concepts are based on interfaces. It allows you to structure your code as you like (multiple handling, file structure, ...)
  • ✅ Use the underlying MediatR library to dispatch action. It allows you to easily implement pipeline or preprocessing if you want custom code.
  • ✅ Use Flux/Redux terminology and not a custom one.
  • ✅ Provide scoped-state that allows to handle multiple instance of the same state type, but uniquely identified by an id

Installation

You can download the latest release NuGet packages from the official Blaztor nuget pages.

Getting started

You can find below examples to illustrate how to implement concepts with Blazstore.

State

A state represents the data of a particular component. In .NET record is largely recommended for state immutability.

public record TaskCreationState(bool IsAddingTask, string? NewTaskDescription) : IState
{
    // Mandatory method to create the initial state.
    public static TaskCreationState Initialize() => new(false, null);
}

Action

Actions are messages that can represent a command to mutate the system or an event that happened in the system. You must implement IAction<TState> to explicitly define for which state is this action.

public record StartAddingNewTask : IAction<TaskCreationState>;

public record DefineNewDescription(string NewDescription) : IAction<TaskCreationState>

public record TaskListLoaded(IReadOnlyCollection<TaskListItem> Payload) : IAction<TaskListState>;

Getting state reference and dispatching actions for a component

A base component StateComponent is provided to easily access the Dispatch<TAction>(TAction action) and GetState<TState>() method.

@inherits Blaztore.Components.StateComponent
@code {

    private TaskCreationState State => GetState<TaskCreationState>();

    protected override Task OnAfterInitialRenderAsync() =>
        Dispatch(new TaskCreationState.Load());
}

You can dispatch action directly from the html, you have no more code-behind !

<button onclick="@(() => Dispatch(new StartAddingNewTask()))">
    New task
</button>

You can find more code on the examples folder.

Reducer

A pure reducer is a function that execute an action on a state, returning a new state. Theoretically, it should not have any dependencies.

public record TaskCreationStateReducer(IStore Store) 
    : IPureReducer<TaskCreationState, StartAddingNewTask>
{
    public TaskCreationState Reduce(TaskCreationState state, StartAddingNewTask action) =>
        state with
        {
            IsAddingTask = true
        };
}

You can organize you reducers like you prefer: a reducer for each action or a single reducer for all your actions.

public record StartAddingNewTaskReducer(IStore Store) 
    : IPureReducer<TaskCreationState, StartAddingNewTask>,
    : IPureReducer<TaskCreationState, EndAddingNewTask>
{
    public TaskCreationState Reduce(TaskCreationState state, StartAddingNewTask action) =>
        state with
        {
            IsAddingTask = true
        };
        
    public TaskCreationState Reduce(TaskCreationState state, EndAddingNewTask action) =>
        state with
        {
            NewTaskDescription = null,
            IsAddingTask = false
        };
}

Effect

An effect allows you to execute side effects on external system and dispatching new actions.

public record ExecuteTaskCreationEffect(IStore Store, ITodoListApi Api, IActionDispatcher ActionDispatcher)
    : IEffect<TaskCreationState, ExecuteTaskCreation>
{
    public async Task Effect(TaskCreationState state, ExecuteTaskCreation action)
    {
        if (string.IsNullOrWhiteSpace(state.NewTaskDescription))
        {
            return;
        }

        await Api.Create(Guid.NewGuid(), state.NewTaskDescription);
        await ActionDispatcher.Dispatch(new EndAddingNewTask());
        await ActionDispatcher.Dispatch(new TodoListState.Load());
    }
}
Product Compatible and additional computed target framework versions.
.NET 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. 
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.3 856 12/11/2023
2.0.2 106 12/10/2023
2.0.1 106 12/8/2023
2.0.0 106 12/8/2023
1.1.3 561 10/7/2023
1.1.2 118 10/6/2023
1.1.1 107 10/6/2023
1.1.0 106 10/6/2023
1.0.0 112 10/6/2023