ReduxSharp 1.0.0

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

ReduxSharp

Unidirectional Data Flow in C# - Inspired by Redux

NuGet

About

There is a library in JavaScript called Redux. If you have ever developed a Single Page Application with React, you may have used it. For more information on Redux, please read the official documentation.

ReduxSharp is a port of Redux to C#.

Installation

First, install Nuget. Then, install ReduxSharp from the package manager console:

PM> Install-Package ReduxSharp

Quick-start

State

Define a class that represents a state of the app.

namespace HelloWorld
{
    public class AppState
    {
        public AppState(int count) => Count = count;

        public int Count { get; }
    }
}

Actions

Define actions. Actions are payloads of information that send data from your application to your store.

namespace HelloWorld
{
    public readonly struct Increment
    {
    }

    public readonly struct Decrement
    {
    }
}

Reducers

Define a reducer. A reducer need to implement the interface IReducer<TState>. It describes how an action transforms the state into the next state.

using System;
using ReduxSharp;

namespace HelloWorld
{
    public class AppReducer : IReducer<AppState>
    {
        public AppState Invoke<TAction>(AppState state, TAction action)
        {
            switch (action)
            {
                case Increment _:
                    return new AppState(state.Count + 1);
                case Decrement _:
                    return new AppState(state.Count - 1);
                default:
                    return state;
            }
        }
    }
}

Store

Build a store using StoreBuilder<TState>. The Store<TState> is the class that bring actions and reducer together. The store has the following responsibilities:

  • Holds application state of type TState.
  • Allows state to be update via Dispatch<TAction>(TAction action).
  • Registers listeners via Subscribe(IObserver observer).The Store<TState> class implements IObservable.

Pass the initial state to the builder and the reducer to Build.

using System;
using ReduxSharp;

namespace HelloWorld
{
    class Program : IObserver<AppState>
    {
        static void Main(string[] args)
        {
            Store<AppState> store = new StoreBuilder<AppState>(new AppState(0)).Build(new AppReducer());

            var observer = new Program();
            using (store.Subscribe(observer))
            {
                // Prints 1.
                store.Dispatch(new Increment());
                // Prints 2.
                store.Dispatch(new Increment());
                // Prints 1.
                store.Dispatch(new Decrement());
                // Prints 2.
                store.Dispatch(new Increment());
            }
        }

        public void OnNext(AppState value) =>
            Console.WriteLine(value.Count);

        public void OnCompleted()
        {
        }

        public void OnError(Exception error)
        {
        }
    }
}

Middleware

Register factories in execution order with Use, then call Build. Each store builds the chain once per dispatched action type and caches it. The factory can capture per-store, per-action-type data when building the delegate.

using System;
using ReduxSharp;

public sealed class ConsoleLoggingMiddleware<TState> : IMiddlewareFactory<TState>
{
    public Action<TAction> Build<TAction>(IStore<TState> store, Action<TAction> next)
    {
        string actionName = typeof(TAction).Name;
        return action =>
        {
            Console.WriteLine($"Dispatching {actionName}");
            next(action);
            Console.WriteLine($"Dispatched {actionName}");
        };
    }
}
Store<AppState> store = new StoreBuilder<AppState>(new AppState(0))
    .Use(new ConsoleLoggingMiddleware<AppState>())
    .Build(new AppReducer());
store.Dispatch(new Increment());

Registration extensions can accept and return IStoreBuilder<TState>. See the logging extension.

Migrating from the old API. Replace new Store<TState>(reducer, state) with new StoreBuilder<TState>(state).Build(reducer). For each old middleware, move the per-dispatch body of IMiddleware.Invoke into the delegate returned by IMiddlewareFactory.Build, as shown above. Replace next.Invoke(action) with next(action) to continue the current chain. To send another action type, call store.Dispatch(otherAction) to start its pipeline at the first middleware. Existing reducers and async action creators can be reused.

Examples

Publishing to NuGet.org

Publishing a GitHub Release runs the Publish NuGet package workflow for the release commit. Both stable and prerelease versions are supported. Creating or pushing a tag alone does not publish a package.

One-time setup

  1. On NuGet.org, sign in as an owner of the ReduxSharp package and add a Trusted Publishing policy with repository owner tnakamura, repository ReduxSharp, and workflow file publish-nuget.yml (the filename only). Leave the environment empty. In Glob Patterns and Packages (Separate by a new line), enter ReduxSharp on a single line to restrict publishing to this package ID. Do not include a version number or a .nupkg extension.
  2. In the GitHub repository's Settings > Secrets and variables > Actions, add the repository secret NUGET_USER containing that owner's NuGet.org username (not an email address). No long-lived NuGet API key is required.
  3. Merge the publishing workflow before creating a release tag so the tagged commit includes the workflow.

Publish a version

  1. Update Version in src/ReduxSharp/ReduxSharp.csproj, then commit and merge the change. AssemblyVersion and FileVersion are derived automatically from Version without its prerelease suffix (for example, 1.0.0 becomes 1.0.0.0).
  2. Create and push a tag matching Version exactly, such as 0.6.0 or 0.6.0-beta.1. Use three numeric components without leading zeroes, optionally followed by a SemVer prerelease suffix; do not add a v prefix or build metadata.
  3. Create a GitHub Release using that tag, prepare its release notes, and select Publish release. For a prerelease version, mark the release as a prerelease.
  4. Check the workflow in Actions. It validates the tag against the evaluated project version, builds, checks formatting, runs tests, and validates the generated package versions before obtaining a short-lived publishing key.

The workflow saves .nupkg and .snupkg files as Actions artifacts and publishes both to NuGet.org. If a run fails, fix the setup issue and use Re-run failed jobs; already published packages are skipped, and symbols are pushed separately so a partial publication can be retried. A published NuGet version cannot be replaced; code changes require a new version and release. Keep release tags unchanged.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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
1.0.0 100 11/19/2026
0.5.1 1,850 10/8/2019
0.5.0 1,598 9/18/2019
0.4.1 1,668 9/13/2019
0.4.0 1,651 9/12/2019
0.4.0-beta2 1,456 9/11/2019
0.4.0-beta 1,506 9/11/2019
0.4.0-alpha 1,465 9/10/2019
0.3.0 2,414 5/18/2018
0.2.0 2,462 9/4/2017
0.1.0 2,197 4/28/2017
0.0.9 2,199 3/27/2017
0.0.8 2,163 3/24/2017
0.0.7 2,291 3/13/2017
0.0.6 2,232 3/1/2017
0.0.5 2,534 11/8/2016
0.0.4 2,246 10/14/2016
0.0.3 2,244 10/7/2016
0.0.2 2,356 9/20/2016
0.0.1 2,528 9/20/2016