AtomicBlom / ReduxSharp

Unidirectional Data Flow in C# - Inspired by Redux

Home Page:http://www.nuget.org/packages/ReduxSharp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReduxSharp

Unidirectional Data Flow in C# - Inspired by Redux

Install

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

PM> Install-Package ReduxSharp

Usage

State

using System;
using ReduxSharp;

namespace ReduxSharpSample
{
    public class AppState
    {
        public int Count { get; set; } = 0;
    }
}

Actions

using System;
using ReduxSharp;

namespace ReduxSharpSample
{
    public class IncrementAction : IAction {}

    public class DecrementAction : IAction {}
}

Reducers

using System;
using ReduxSharp;

namespace ReduxSharpSample
{
    public class AppReducer : IReducer<AppState>
    {
        public AppState Invoke(AppState state, IAction action)
        {
            if (action is IncrementAction)
            {
                return new AppState()
                {
                    Count = state.Count + 1
                };
            }

            if (action is DecrementAction)
            {
                return new AppState()
                {
                    Count = state.Count - 1
                };
            }

            return state;
        }
    }
}

Store

using System;
using ReduxSharp;

namespace ReduxSharpSample
{
    class Program
    {
        static void Main(string[] args)
        {
            IStore<AppState> store = new StoreBuilder<AppState>(new AppReducer())
                .UseInitialState(new AppState())
                .Build();

            Console.WriteLine(store.State.Count); // => 0

            store.Dispatch(new IncrementAction());
            Console.WriteLine(store.State.Count); // => 1

            store.Dispatch(new DecrementAction());
            Console.WriteLine(store.State.Count); // => 0
        }
    } 
}

Contribution

  1. Fork it
  2. Create your feature branch ( git checkout -b my-new-feature )
  3. Commit your changes ( git commit -am 'Add some feature' )
  4. Push to the branch ( git push origin my-new-feature )
  5. Create new Pull Request

License

MIT

Author

tnakamura

About

Unidirectional Data Flow in C# - Inspired by Redux

http://www.nuget.org/packages/ReduxSharp

License:MIT License


Languages

Language:C# 100.0%