Skip to content

Implement state machine patterns in C#/.NET#1

Draft
gaoziyin with Copilot wants to merge 3 commits into
mainfrom
copilot/add-state-machine-patterns
Draft

Implement state machine patterns in C#/.NET#1
gaoziyin with Copilot wants to merge 3 commits into
mainfrom
copilot/add-state-machine-patterns

Conversation

Copilot AI commented Dec 30, 2025

Copy link
Copy Markdown

Implements three state machine patterns with examples and comprehensive test coverage.

Patterns Implemented

State Pattern (Gang of Four)

  • IState<TContext> interface with OnEnter/OnExit/Execute lifecycle
  • StateContext<TContext> base class handling state transitions
  • Example: Document workflow (Draft → Moderation → Approved → Published)

State Machine with Explicit Transitions

  • StateMachine<TState, TTrigger> with configurable allowed transitions
  • Entry/Exit actions and transition events
  • Validation via CanFire() before state changes
  • Example: Traffic light with timed transitions

Hierarchical State Machine

  • HierarchicalState supporting nested states
  • Event bubbling from child to parent
  • Full state path tracking
  • Example: Vending machine with sub-states

Usage

// State Machine with Transitions
var sm = new StateMachine<LightState, Trigger>(LightState.Red);

sm.Configure(LightState.Red)
    .Permit(Trigger.TimerExpired, LightState.Green)
    .OnEntry(() => Console.WriteLine("Stop!"));

sm.Fire(Trigger.TimerExpired);  // Red → Green

// State Pattern
var document = new Document("Proposal");
document.Submit();   // Draft → Moderation
document.Approve();  // Moderation → Approved
document.Publish();  // Approved → Published

Structure

StateMachinePatterns.Core/      - Pattern implementations
StateMachinePatterns.Examples/  - Working examples
StateMachinePatterns.Tests/     - 18 unit tests

Type-safe enums, separation of concerns, full test coverage.

Original prompt

State machine patterns by C#/.Net


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Dec 30, 2025
Copilot AI and others added 2 commits December 30, 2025 09:06
- Created .NET solution with three projects: Core, Examples, and Tests
- Implemented State Pattern (classic Gang of Four pattern)
- Implemented State Machine with explicit transitions and validation
- Implemented Hierarchical State Machine with nested states
- Added practical examples: Document workflow, Traffic Light, Vending Machine
- Created 18 comprehensive unit tests (100% passing)
- Updated README with detailed documentation and usage examples

Co-authored-by: gaoziyin <17057760+gaoziyin@users.noreply.github.com>
…transition method

- Changed _stateMachine field from internal to private in VendingMachine
- Added internal TransitionTo method to properly encapsulate state transitions
- Updated all state classes to use the new TransitionTo method
- Maintains functionality while improving encapsulation

Co-authored-by: gaoziyin <17057760+gaoziyin@users.noreply.github.com>
Copilot AI changed the title [WIP] Add state machine patterns in C#/.Net Implement state machine patterns in C#/.NET Dec 30, 2025
Copilot AI requested a review from gaoziyin December 30, 2025 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants