This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Build the solution
dotnet build
# Run tests
dotnet test
# Run a specific test
dotnet test --filter "FullyQualifiedName~InvoiceStateMachineTests.Configure_SetsInitialState"
# Build and pack for NuGet
dotnet pack -c ReleaseSlimStateMachine is a lightweight C# library for defining and managing state machines based on an entity class and an enum property representing its state. It targets .NET 9.0, .NET 8.0, and .NET Standard 2.0.
-
StateMachine<TEntity, TEnum>(StateMachine.cs,StateMachine.D2.cs): Static generic class providing the public API. Uses double-checked locking for thread-safe one-time configuration. Split into partial classes:- Main file: Configuration, transitions (
CanTransition,TryTransition,TryTransitionAny), queries (GetPossibleTransitions,GetDefinedTransitions,IsFinalState), Mermaid graph generation - D2 file: D2 diagram generation and
DiagramTypeenum for format selection
- Main file: Configuration, transitions (
-
StateMachineConfigurationBuilder<TEntity, TEnum>: Fluent builder for configuring state machines viaConfigure(). Validates configuration and prevents duplicate transitions. -
Internal Classes (
Internal/):StateMachineConfiguration<TEntity, TEnum>: Holds compiled configuration including getter/setter delegates from expression trees, transitions dictionary (frozen after build), and computed final statesTransitionDefinition<TEntity, TEnum>: Represents a single transition with optional pre-condition function, pre-condition expression string (for diagrams), and post-action
- Static generic access: Each
TEntity/TEnumcombination gets its own static configuration cache - Expression tree compilation: Status property accessor is compiled to delegates for efficient runtime access
- Frozen collections: Transitions and final states use
FrozenDictionary/FrozenSetfor immutable, optimized lookups - Pre/Post conditions: Pre-conditions are
Func<TEntity, bool>evaluated at transition time; post-actions areAction<TEntity>executed before state update
Tests use MSTest with [TestInitialize] calling ClearConfiguration_TestOnly() to reset static state between tests. The InternalsVisibleTo attribute exposes internal members to the test project.