Repository navigation
6.5. OneOf
Stateflows.Extensions.OneOf integrates OneOf union types with Stateflows type mapping. It allows the alternatives in a OneOf<T0, ...> type to be treated as the possible event trigger types where Stateflows resolves trigger types.
Register the mapper before registering State Machines or Activities:
services.AddStateflows(stateflows => stateflows
.AddOneOf()
.AddStateMachines(machines => machines
.AddStateMachine<Order>()));This ordering matters: Stateflows uses registered type mappers while definitions are being built. The extension does not change the event payload or automatically choose a matching alternative for application code; it maps the union's declared generic alternatives for trigger discovery.
The scheduling extension uses this mapper internally for its own trigger definitions. See the extension source and the OneOf documentation for the union type itself.
After AddOneOf(), a State Machine can declare one transition for several incoming CLR event types:
using OneOf;
using Stateflows.StateMachines;
public sealed record CancelOrder;
public sealed record RejectOrder;
public sealed class Order : IStateMachine
{
public static void Build(IStateMachineBuilder machine) => machine
.AddInitialState("Pending", state => state
.AddTransition<OneOf<CancelOrder, RejectOrder>>("Closed"))
.AddFinalState("Closed");
}Send the ordinary alternative payload, for example await order.SendAsync(new CancelOrder()); no union wrapper is needed. This example deliberately has no guard/effect that reads the union. The mapper expands trigger discovery; it is not an application-level conversion service for arbitrary payloads.
Home page Support Code licensed under an MIT-style License. Documentation licensed under CC BY 4.0. © by Mikołaj Milewski, 2026