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
- 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