|
| 1 | +# AOT and Trim Compatibility |
| 2 | + |
| 3 | +The `net8.0` build of `FunctionalStateMachine.Core` and `FunctionalStateMachine.CommandRunner` is fully compatible with: |
| 4 | + |
| 5 | +- **NativeAOT** (`PublishAot=true`) — compiled ahead of time to a self-contained native binary |
| 6 | +- **Trimming** (`PublishTrimmed=true`) — unused code removed at publish time to reduce binary size |
| 7 | +- **Single-file publishing** (`PublishSingleFile=true`) |
| 8 | + |
| 9 | +## Status |
| 10 | + |
| 11 | +| Package | `IsAotCompatible` | Reflection-free | Trim-safe | |
| 12 | +|---|---|---|---| |
| 13 | +| `FunctionalStateMachine.Core` | ✅ (`net8.0+`) | ✅ | ✅ | |
| 14 | +| `FunctionalStateMachine.CommandRunner` | ✅ (`net8.0+`) | ✅ | ✅ | |
| 15 | +| `FunctionalStateMachine.Diagrams` | N/A (build-time only) | N/A | N/A | |
| 16 | +| `FunctionalStateMachine.Core.Generator` | N/A (build-time only) | N/A | N/A | |
| 17 | +| `FunctionalStateMachine.CommandRunner.Generator` | N/A (build-time only) | N/A | N/A | |
| 18 | + |
| 19 | +The two generators (`Core.Generator` and `CommandRunner.Generator`) are Roslyn analyzers that run at compile time inside the compiler process, not in your application. They are never published as part of your app binary. |
| 20 | + |
| 21 | +## Enabling publishing with trimming |
| 22 | + |
| 23 | +### Executable projects |
| 24 | + |
| 25 | +Add `<PublishTrimmed>true</PublishTrimmed>` to your `.csproj`: |
| 26 | + |
| 27 | +```xml |
| 28 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 29 | + <PropertyGroup> |
| 30 | + <OutputType>Exe</OutputType> |
| 31 | + <TargetFramework>net9.0</TargetFramework> |
| 32 | + <PublishTrimmed>true</PublishTrimmed> |
| 33 | + </PropertyGroup> |
| 34 | +</Project> |
| 35 | +``` |
| 36 | + |
| 37 | +The source generator is bundled inside the `FunctionalStateMachine.Core` NuGet package and applied automatically — no extra package reference needed. |
| 38 | + |
| 39 | +Then publish: |
| 40 | + |
| 41 | +```bash |
| 42 | +dotnet publish -c Release -r linux-x64 --sc true |
| 43 | +``` |
| 44 | + |
| 45 | +### NativeAOT |
| 46 | + |
| 47 | +```xml |
| 48 | +<PropertyGroup> |
| 49 | + <PublishAot>true</PublishAot> |
| 50 | +</PropertyGroup> |
| 51 | +``` |
| 52 | + |
| 53 | +```bash |
| 54 | +dotnet publish -c Release -r linux-x64 |
| 55 | +``` |
| 56 | + |
| 57 | +## How it works: no reflection at runtime |
| 58 | + |
| 59 | +The library was originally written to warn about unused trigger types at state machine build time. This required knowing all defined trigger subtypes — which was initially discovered via `Assembly.GetTypes()` and `Type.GetProperty()` (reflection). |
| 60 | + |
| 61 | +Both reflection APIs are incompatible with AOT/trimming because the trimmer may remove types it doesn't see referenced, and `Assembly.GetTypes()` only returns what survives trimming. |
| 62 | + |
| 63 | +### The source generator approach |
| 64 | + |
| 65 | +The `FunctionalStateMachine.Core.Generator` Roslyn source generator solves this at compile time: |
| 66 | + |
| 67 | +1. It detects all `StateMachine<…, TTrigger, …>.Create()` call sites in your compilation. |
| 68 | +2. For each unique `TTrigger`, it finds all non-abstract concrete derived types using the Roslyn symbol API (no runtime reflection). |
| 69 | +3. It generates a `[ModuleInitializer]` that registers those types in `TriggerTypeRegistry` before any app code runs. |
| 70 | + |
| 71 | +```csharp |
| 72 | +// Example of generated output (in your bin/obj folder): |
| 73 | +// <auto-generated /> |
| 74 | +[ModuleInitializer] |
| 75 | +internal static void Initialize() |
| 76 | +{ |
| 77 | + TriggerTypeRegistry.Register<global::MyApp.OrderTrigger>(new[] |
| 78 | + { |
| 79 | + typeof(global::MyApp.OrderTrigger.Process), |
| 80 | + typeof(global::MyApp.OrderTrigger.Cancel), |
| 81 | + typeof(global::MyApp.OrderTrigger.Complete), |
| 82 | + }); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +At runtime, `AnalyzeUnusedTriggers` reads from this registry — no assembly scanning, no reflection. If the registry has not been populated (e.g. the generator wasn't active for that trigger type), the check is silently skipped rather than throwing. |
| 87 | + |
| 88 | +### CommandRunner dispatcher |
| 89 | + |
| 90 | +`FunctionalStateMachine.CommandRunner` also uses a source generator (`CommandRunner.Generator`) to produce a type-switching dispatcher at compile time. The dispatcher uses a `switch` on concrete command types rather than any reflection-based dispatch, making it fully AOT- and trim-safe. |
| 91 | + |
| 92 | +## Multiple state machines sharing the same trigger type |
| 93 | + |
| 94 | +When several state machines in the same project share the same `TTrigger`, the generator registers the trigger types once (deduplicated by trigger base type). Each machine's unused-trigger analysis runs independently at `.Build()` time: |
| 95 | + |
| 96 | +```csharp |
| 97 | +// Both machines share OrderTrigger; generator registers its types once. |
| 98 | +// machineA may use all triggers; machineB may only use a subset. |
| 99 | +// Each machine independently warns about its own unused triggers. |
| 100 | +
|
| 101 | +var machineA = StateMachine<StateA, OrderTrigger, DataA, CmdA>.Create() |
| 102 | + .For(StateA.Idle) |
| 103 | + .On<OrderTrigger.Process>() // uses Process |
| 104 | + .TransitionTo(StateA.Done) |
| 105 | + // ⚠️ warning: Cancel and Complete unused in this machine |
| 106 | + .Build(); |
| 107 | + |
| 108 | +var machineB = StateMachine<StateB, OrderTrigger, DataB, CmdB>.Create() |
| 109 | + .For(StateB.Idle) |
| 110 | + .On<OrderTrigger.Cancel>() // uses Cancel |
| 111 | + .TransitionTo(StateB.Cancelled) |
| 112 | + // ⚠️ warning: Process and Complete unused in this machine |
| 113 | + .Build(); |
| 114 | +``` |
| 115 | + |
| 116 | +Both machines build successfully — unused-trigger warnings are informational and never block construction. |
| 117 | + |
| 118 | +## Related pages |
| 119 | + |
| 120 | +- [Target Framework Compatibility](./Target-Framework-Compatibility.md) |
| 121 | +- [Packages](./Packages.md) |
| 122 | +- [Static Analysis](./Static-Analysis-for-State-Machine-Configuration.md) |
0 commit comments