drittich.StateMachine 1.0.2

Suggested Alternatives

drittich.StateMachine 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package drittich.StateMachine --version 1.0.2                
NuGet\Install-Package drittich.StateMachine -Version 1.0.2                
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.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add drittich.StateMachine --version 1.0.2                
#r "nuget: drittich.StateMachine, 1.0.2"                
#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.2

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

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
{
    DoStuff,
    DoOtherStuff,
    SomeOtherRandomStuff
}

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.DoStuff, SomeMethodToExecute), MyStates.SomeState },
    { new Transition<MyStates, MyEvents, MyCustomDto>(MyStates.SomeState, MyEvents.DoOtherStuff, SomeMethodToExecute), MyStates.Complete }
};

var data = new MyDTO { Prop1 = 1 };

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

// Invalid transitions will result in an exception
var resultingState2 = sm.GetNext(MyEvents.SomeOtherRandomStuff, data);  // since no transition defined for `SomeOtherRandomStuff`, 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.

Initial release