A basic state machine implementation for .Net Framework and .Net Core.
Jason State is a simple state machine implementation. It’s configured by a JSON file.
Package | |
---|---|
JasonState | |
JasonState.Extension |
Following commands can be used to install JasonState and JasonState.Extension, run the following command in the Package Manager Console
dotnet add package JasonState
dotnet add package JasonState.Extension
Or use dotnet cli
dotnet add package JasonState
dotnet add package JasonState.Extension
First you need to provide a valid JSON file.
This JSON file must contain a States array. This array should have objects.
An example of a valid JSON file can be found throug
{
"States": [
{
"Namespace": "TestClient.Impls.States",
"Name": "InitialState",
"NextState": [
{
"Condition": "!string.IsNullOrEmpty(FromEmail) && FromEmail.Equals(\"[email protected]\")",
"State": "ValidatePaymentState"
},
{
"Condition": "!string.IsNullOrEmpty(FromEmail) && FromEmail.Equals(\"[email protected]\")",
"State": "FinalState"
}
],
"ErrorState": "ErrorState"
},
{
"Namespace": "TestClient.Impls.States",
"Name": "ErrorState",
"NextState": [
{
"Condition": "true",
"State": "FinalState"
}
],
"ErrorState": null
},
{
"Namespace": "TestClient.Impls.States",
"Name": "FinalState",
"NextState": null,
"ErrorState": null
}
]
}
States must inherit from and implement Execute, or ExecuteAsync, method with your state context. You can use any dependency injection framework for construction injections. It will not break anything.
public class InitialState : BaseState<TestStateContext>
{
public override void Execute(TestStateContext context)
{
// do the magic
}
}
Jason State allows you to add any kind of object to the context. Everything you need during the state execution should be in the context.
public class TestStateContext
{
public long CreditCardNumber { get; set; }
public string CardHolderName { get; set; }
public decimal Amount { get; set; }
}
public class InitialState : BaseState<TestStateContext>
{
public override void Execute(TestStateContext context)
{
context.CreditCardNumber = "4545454545454545";
}
}
By referencing JasonState.Extension, register necessary dependencies to ServiceCollection as follows
serviceCollection.AddJasonState<TestStateContext>();
or
serviceCollection.AddAsyncJasonState<TestStateContext>();
TestClient can be found
AsyncTestClient can be found
Licensed under MIT, see LICENSE for the full text.