PureSM is a small, dependency-free C# library for building explicit state machines with a fluent builder API. It demonstrates state lifecycle hooks (Entry/Action/Exit), transitions (with async conditions), context-passing, and terminal (end) states.
Features
- Lightweight state and transition model
- Async-friendly (
Task-based lifecycle and conditions) - Context object for passing data between states
- Fluent
StateMachineBuilderfor easy construction - Example projects demonstrating common patterns
Contents
src/PureSM— library sourcestests— MSTest unit tests project (PureSM.Tests)examples— sample applications demonstrating usage:TrafficLightExample(previouslySimpleExample) — simple traffic-light with a terminal Off stateOrderProcessingExample(previouslyAdvancedExample) — order workflow with conditional transitionsCrawlerExample— iterative crawler-like workflow using a queue
docs— additional documentation (if present)
Getting started Prerequisites:
- .NET SDK 6.0+ (9.0 SDK used for multi-target builds in CI)
Build the library and examples:
# from repository root
dotnet restore
dotnet buildRun an example (choose one):
cd examples\TrafficLightExample
dotnet run -f net9.0 --no-build
cd ..\OrderProcessingExample
dotnet run -f net9.0 --no-build
cd ..\CrawlerExample
dotnet run -f net9.0 --no-buildIf you want to run for another target framework (e.g. net6.0), change the -f flag accordingly.
Usage (library overview)
- Create a
Contextand set any initial data viacontext.SetItem(key, object). - Implement
Statesubclasses and overrideEntry(),Action(),Exit()asTask<State>returning the state instance. - Create
Transitioninstances with async condition functions and target state lists. - Register transitions on states via
state.AddTransition(transition). - Build the state machine via
new StateMachineBuilder().AddInitialState(...).AddState(...).WithContext(context).Build(). - Start execution with
await stateMachine.StartAsync().
Examples
TrafficLightExampledemonstrates sequential states and a terminalOffStateafter N cycles.OrderProcessingExampledemonstrates conditional flow (payment success/failure, inventory checks) using context values.CrawlerExampledemonstrates an iterative workflow with a URL queue, parsing and extraction, and a final summary.
Testing Run unit tests (MSTest):
cd tests\PureSM.Tests
dotnet testContributing
- Fork, implement a small focused change, add tests where applicable, and open a pull request.
- Keep changes targeted to one area per PR and follow the project coding style.
Notes
- The library targets multiple frameworks (net5.0, net6.0, net7.0, net8.0, net9.0). Conditional
ImplicitUsingsand small compatibility shims are in place to maintain net5.0 compatibility. - Example projects live under
examples/— their project names were recently updated to more descriptive names.
If you want, I can:
- Update
examples/README.mdto reflect the new example names and add quick-run snippets (I can do this next). - Run all examples and collect sample outputs into a single
examples/demo-output.mdfile.