From 0da823e0ec3fa9946f8d789108acd23cdfacd5b2 Mon Sep 17 00:00:00 2001 From: D'Arcy Rittich Date: Sun, 10 Nov 2024 08:02:59 -0500 Subject: [PATCH] update README --- README.md | 42 ++++++++++++-------------------- StateMachine/StateMachine.csproj | 4 +-- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 59d292d..faa7e45 100644 --- a/README.md +++ b/README.md @@ -63,18 +63,16 @@ public class MyDto ### Initialize the State Machine ```csharp -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; - -// Create a logger (use NullLogger if you don't need logging) -ILogger> logger = new NullLogger>(); +// Add transitions to the state machine +sm.AddTransition(MyStates.Initial, MyEvents.SomethingHappened, MyStates.SomeState, SomeMethodToExecuteAsync); +sm.AddTransition(MyStates.SomeState, MyEvents.SomethingElseHappened, MyStates.Complete, SomeOtherMethodToExecuteAsync); -// Initialize the state machine with the initial state -var sm = new StateMachine(MyStates.Initial, logger); ``` ### Define the Transitions +With the simplified AddTransition method, you can now add transitions directly without needing to create Transition objects explicitly: + ```csharp // Add transitions to the state machine sm.AddTransition(new Transition( @@ -153,13 +151,14 @@ catch (InvalidTransitionException ex) You can add guard conditions to transitions to control whether the transition should occur based on the event data. ```csharp -sm.AddTransition(new Transition( - currentState: MyStates.SomeState, - evt: MyEvents.SomeOtherRandomEvent, - nextState: MyStates.Complete, - action: SomeOtherMethodToExecuteAsync, +sm.AddTransition( + MyStates.SomeState, + MyEvents.SomeOtherRandomEvent, + MyStates.Complete, + SomeOtherMethodToExecuteAsync, guard: data => data.Prop1 > 0 -)); +); + ``` If the guard condition returns false, a GuardConditionFailedException is thrown, and the transition does not occur. @@ -243,19 +242,9 @@ class Program var sm = new StateMachine(MyStates.Initial, logger); // Define transitions - sm.AddTransition(new Transition( - currentState: MyStates.Initial, - evt: MyEvents.SomethingHappened, - nextState: MyStates.SomeState, - action: SomeMethodToExecuteAsync - )); - - sm.AddTransition(new Transition( - currentState: MyStates.SomeState, - evt: MyEvents.SomethingElseHappened, - nextState: MyStates.Complete, - action: SomeOtherMethodToExecuteAsync - )); + sm.AddTransition(MyStates.Initial, MyEvents.SomethingHappened, MyStates.SomeState, SomeMethodToExecuteAsync); + + sm.AddTransition(MyStates.SomeState, MyEvents.SomethingElseHappened, MyStates.Complete, SomeOtherMethodToExecuteAsync); // Event data var data = new MyDto { Prop1 = 1 }; @@ -291,6 +280,7 @@ class Program Console.WriteLine("Executed SomeOtherMethodToExecuteAsync"); } } + ``` ## Installation diff --git a/StateMachine/StateMachine.csproj b/StateMachine/StateMachine.csproj index 110cd9d..0241a01 100644 --- a/StateMachine/StateMachine.csproj +++ b/StateMachine/StateMachine.csproj @@ -7,7 +7,7 @@ enable True drittich.$(AssemblyName) - 1.1.0 + 1.2.0 A simple convention-based finite state machine that lets you pass event data through to your transition actions. drittich drittich @@ -16,7 +16,7 @@ https://github.com/drittich/state-machine git statemachine,state-machine,fsm - More robust, more tests + Has a new, simpler AddTransition method none MIT False