Unidirectional Data Flow in C# - Inspired by Redux
First, install Nuget. Then, install ReduxSharp from the package manager console:
PM> Install-Package ReduxSharp
using System;
using ReduxSharp;
namespace ReduxSharpSample
{
public class AppState
{
public int Count { get; set; } = 0;
}
}
using System;
using ReduxSharp;
namespace ReduxSharpSample
{
public class IncrementAction : IAction {}
public class DecrementAction : IAction {}
}
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;
}
}
}
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
}
}
}
- Fork it
- Create your feature branch ( git checkout -b my-new-feature )
- Commit your changes ( git commit -am 'Add some feature' )
- Push to the branch ( git push origin my-new-feature )
- Create new Pull Request