feat(state): implemented fluent state pattern#55
Conversation
- AsyncStateMachine<TState,TEvent> API redesigned to avoid ref parameters (not allowed on async): - TryTransitionAsync(TState state, TEvent event, CancellationToken) -> (bool handled, TState state) - TransitionAsync(TState state, TEvent event, CancellationToken) -> TState - Preserves ordering: Exit -> Effect -> Enter; Stay runs effect only; per-state default supported. - Updated unit tests for async state machine to new API and added coverage for ordering, default handling, and unhandled throws. - Added Async Connection demo: - src/PatternKit.Examples/AsyncStateDemo/AsyncStateDemo.cs - test/PatternKit.Examples.Tests/AsyncStateDemo/AsyncStateDemoTests.cs (TinyBDD overloads disambiguated) - Documentation: - docs/patterns/behavioral/state/state.md explains both sync and async flavors with quick starts and API summary. - docs/examples/async-state-machine.md example page; linked in docs/examples/toc.yml. Build & tests: - Verified with `dotnet test -v minimal`: all projects compile, and all tests pass across target frameworks.
There was a problem hiding this comment.
Pull Request Overview
This PR implements a fluent state pattern providing both synchronous and asynchronous state machines with entry/exit hooks, transition effects, and predictable first-match rule ordering.
- Adds StateMachine<TState, TEvent> for synchronous state transitions with ref state parameter updates
- Adds AsyncStateMachine<TState, TEvent> for asynchronous operations returning state tuples
- Includes comprehensive test coverage and example demos for order and connection lifecycles
Reviewed Changes
Copilot reviewed 15 out of 16 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/PatternKit.Core/Behavioral/State/StateMachine.cs | Core synchronous state machine implementation with fluent builder API |
| src/PatternKit.Core/Behavioral/State/AsyncStateMachine.cs | Asynchronous variant with ValueTask-based predicates and effects |
| test/PatternKit.Tests/Behavioral/State/StateMachineTests.cs | Unit tests for synchronous state machine behavior and transitions |
| test/PatternKit.Tests/Behavioral/State/AsyncStateMachineTests.cs | Unit tests for asynchronous state machine operations |
| src/PatternKit.Examples/StateDemo/StateDemo.cs | Example order lifecycle demo using synchronous state machine |
| src/PatternKit.Examples/AsyncStateDemo/AsyncStateDemo.cs | Example connection lifecycle demo using async state machine |
| test/PatternKit.Examples.Tests/StateDemo/StateDemoTests.cs | Tests for order lifecycle demo scenarios |
| test/PatternKit.Examples.Tests/AsyncStateDemo/AsyncStateDemoTests.cs | Tests for async connection demo scenarios |
| docs/patterns/behavioral/state/state.md | Documentation page for state pattern implementation |
| docs/examples/state-machine.md | Example documentation for order lifecycle demo |
| docs/examples/async-state-machine.md | Example documentation for async connection demo |
| docs/patterns/toc.yml | Table of contents update to include state pattern |
| docs/patterns/index.md | Main patterns index update with state machine references |
| docs/examples/toc.yml | Examples table of contents update |
| docs/examples/index.md | Examples index update with state machine demos |
| README.md | Root readme update marking state pattern as completed |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Code Coverage |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #55 +/- ##
==========================================
+ Coverage 77.39% 84.51% +7.12%
==========================================
Files 60 64 +4
Lines 4472 4741 +269
Branches 678 744 +66
==========================================
+ Hits 3461 4007 +546
+ Misses 767 734 -33
+ Partials 244 0 -244
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
No description provided.