drittich.StateMachine 1.0.3

dotnet add package drittich.StateMachine --version 1.0.3
NuGet\Install-Package drittich.StateMachine -Version 1.0.3
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="drittich.StateMachine" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add drittich.StateMachine --version 1.0.3
#r "nuget: drittich.StateMachine, 1.0.3"
#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.
// Install drittich.StateMachine as a Cake Addin
#addin nuget:?package=drittich.StateMachine&version=1.0.3

// Install drittich.StateMachine as a Cake Tool
#tool nuget:?package=drittich.StateMachine&version=1.0.3

state-machine

A simple convention-based finite state machine that lets you pass event data through to your transition actions.

Example Usage

StateMachine expects you to define your own states, events, transitions, and a DTO so that data can easily be passed with events so that they can then be provided to the resulting action that will run when a transition occurs. It will automatically select the first value in your states enum as the initial state of the state machine. (If you don't want this you can define and execute a transition immediately after creating the state machine.)

E.g.,

enum MyStates
{
    Initial,
    SomeState,
    SomeOtherState,
    Complete
}

enum MyEvents
{
    SomethingHappened,
    SomethingElseHappened,
    SomeOtherRandomEvent
}

public class MyDto {
    public int Prop1 { get; set; }    
}
  
...
  
// create the state machine
var sm = new StateMachine(MyStates, MyEvents, MyDto>();

// specify the allowed transitions
stateMachine.Transitions = new Dictionary<Transition<MyStates, MyEvents, MyCustomDto>, MyStates>
{
    { new Transition<MyStates, MyEvents, MyCustomDto>(MyStates.Initial, MyEvents.SomethingHappened, SomeMethodToExecuteAsync), MyStates.SomeState },
    { new Transition<MyStates, MyEvents, MyCustomDto>(MyStates.SomeState, MyEvents.SomethingElseHappened, SomeMethodToExecuteAsync), MyStates.Complete }
};

var data = new MyDto { Prop1 = 1 };

// Execute a transition.
// This will transition the state as per the specification above, and call method SomeMethodToExecuteAsync,
// passing the parameter `data` to it.
var resultingState = await sm.GetNextAsync(MyEvents.SomethingHappened, data);  // new state is `MyStates.SomeState`

// Invalid transitions will result in an exception
var resultingState2 = sm.GetNextAsync(MyEvents.SomeOtherRandomEvent, data);  // since no transition defined for `SomeOtherRandomEvent`, wil throw
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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.1

    • 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.3 174 5/18/2023
1.0.2 120 5/17/2023
1.0.1 142 5/10/2023
1.0.0 136 5/10/2023

Switched to async GetNext method