StateMachineSrcGen 0.0.1-preview2

This is a prerelease version of StateMachineSrcGen.
There is a newer version of this package available.
See the version list below for details.
dotnet add package StateMachineSrcGen --version 0.0.1-preview2
                    
NuGet\Install-Package StateMachineSrcGen -Version 0.0.1-preview2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StateMachineSrcGen" Version="0.0.1-preview2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StateMachineSrcGen" Version="0.0.1-preview2" />
                    
Directory.Packages.props
<PackageReference Include="StateMachineSrcGen">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StateMachineSrcGen --version 0.0.1-preview2
                    
#r "nuget: StateMachineSrcGen, 0.0.1-preview2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StateMachineSrcGen@0.0.1-preview2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StateMachineSrcGen&version=0.0.1-preview2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=StateMachineSrcGen&version=0.0.1-preview2&prerelease
                    
Install as a Cake Tool

StateMachineSrcGen

A Roslyn incremental source generator that turns concise, attribute-decorated state machine declarations into generated dispatch/orchestration code at compile time.

Features

  • Incremental Roslyn source generation
  • Attribute-driven transition, guard, and side-effect model
  • Generated async orchestration (HandleAsync) with default in-memory persistence and no-op lock
  • Compile-time validation diagnostics (SMSG*)

Installation

dotnet add package StateMachineSrcGen

The package includes the source generator and required attributes/interfaces.

Current Supported Usage

The current end-to-end generation path is centered on string state values.

1. Define event type

using StateMachineSrcGen;

public record OrderEvent(string Action) : IDispatchableEvent<string>
{
    public string GetEventId() => Action;
}

2. Declare machine class

using StateMachineSrcGen;

[State("Pending", IsInitial = true)]
[State("Confirmed")]
[Trigger("Confirm")]
public static partial class OrderMachine
{
    [Transition("Pending", "Confirmed", "Confirm", EventId = "confirm")]
    public static string HandleConfirm(string state, OrderEvent @event)
        => "Confirmed";
}

3. Call generated API

var result = await OrderMachine.HandleAsync(new OrderEvent("confirm"));

Declaration Contract (Current)

Class requirements currently enforced:

  • public static partial class
  • Event type used by handlers must implement IDispatchableEvent<TEventId>
  • Class generic parameters must be either 0 or 2

Handler requirements currently enforced:

  • public static
  • Exactly two parameters: (TState state, TEvent @event)
  • Return type by attribute kind:
    • [Transition]TState
    • [Guard]bool
    • [SideEffect]void

Model requirements currently enforced:

  • At least one [State]
  • Exactly one initial state (IsInitial = true)
  • Unique state names and unique trigger names
  • Handler references must point to declared states/triggers
  • No duplicate transition handlers with same (From, To, Trigger)

Runtime Notes

Generated code currently:

  • Calls @event.GetEventId() and routes via generated switch logic.
  • Uses an internal default InMemoryPersistence and NoOpLock.
  • Executes orchestration in this order: acquire lock, load, dispatch/guard/action, save, side effect, release.

At this time, public customization hooks for persistence/lock injection are not part of the documented contract.

Compile-Time Diagnostics

The generator validates your definition at compile time and reports diagnostics:

ID Severity Meaning
SMSG001 Error Duplicate transition handler (same From/To/Trigger)
SMSG002 Error Handler references undeclared state
SMSG003 Error Handler references undeclared trigger
SMSG004 Error No states declared
SMSG005 Error No initial state designated
SMSG006 Error Multiple initial states
SMSG007 Error Duplicate state names
SMSG008 Error Duplicate trigger names
SMSG009 Warning Unreachable state (no inbound transitions)
SMSG010 Error Invalid class declaration (modifiers / class generic parameter count)
SMSG012 Error Invalid handler method signature
SMSG014 Error Transition missing target state
SMSG015 Error Internal generator error
SMSG016 Error Event type missing IDispatchableEvent

Diagnostics SMSG011 and SMSG013 exist as descriptors in code but are not currently part of the active validation path.

Building from Source

dotnet restore
dotnet build
dotnet test

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 75 5/11/2026
0.0.1-preview4 84 5/8/2026
0.0.1-preview3 77 5/8/2026
0.0.1-preview2 72 5/6/2026
0.0.1-preview1 72 5/5/2026