Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
drittich committed Nov 10, 2024
1 parent 337c89e commit 0da823e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
42 changes: 16 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateMachine<MyStates, MyEvents, MyDto>> logger = new NullLogger<StateMachine<MyStates, MyEvents, MyDto>>();
// 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, MyEvents, MyDto>(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<MyStates, MyEvents, MyDto>(
Expand Down Expand Up @@ -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<MyStates, MyEvents, MyDto>(
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.
Expand Down Expand Up @@ -243,19 +242,9 @@ class Program
var sm = new StateMachine<MyStates, MyEvents, MyDto>(MyStates.Initial, logger);

// Define transitions
sm.AddTransition(new Transition<MyStates, MyEvents, MyDto>(
currentState: MyStates.Initial,
evt: MyEvents.SomethingHappened,
nextState: MyStates.SomeState,
action: SomeMethodToExecuteAsync
));

sm.AddTransition(new Transition<MyStates, MyEvents, MyDto>(
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 };
Expand Down Expand Up @@ -291,6 +280,7 @@ class Program
Console.WriteLine("Executed SomeOtherMethodToExecuteAsync");
}
}

```

## Installation
Expand Down
4 changes: 2 additions & 2 deletions StateMachine/StateMachine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>drittich.$(AssemblyName)</PackageId>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<Title>A simple convention-based finite state machine that lets you pass event data through to your transition actions.</Title>
<Authors>drittich</Authors>
<Company>drittich</Company>
Expand All @@ -16,7 +16,7 @@
<RepositoryUrl>https://github.com/drittich/state-machine</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>statemachine,state-machine,fsm</PackageTags>
<PackageReleaseNotes>More robust, more tests</PackageReleaseNotes>
<PackageReleaseNotes>Has a new, simpler AddTransition method</PackageReleaseNotes>
<AnalysisLevel>none</AnalysisLevel>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
Expand Down

0 comments on commit 0da823e

Please sign in to comment.