Skip to content

Extension OneOf

Mikołaj Milewski edited this page Oct 27, 2026 · 1 revision

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.

Use alternative triggers

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.

Overview
Installation
Runtimes
   Orleans runtime
Concepts
   Events and Structural Events
   Messaging Patterns
   Validation
   Scheduling
   Observability
Behaviors
   Behavior Values
   Dependency Injection
   Behavioral Groups
   State Machines
       Concepts
           State Identity
           In-State Event Deferrals
           Evaluation of Transitions
           Exception Handling
       Building blocks
           Regions
           States
               State
               Composite State
               Orthogonal State
               Final State
           Pseudostates
               Choice
               Junction
               Fork
               Join
               History
           Transitions
               Transition
               Default Transition
               Internal Transition
   Activities
       Concepts
           Exception Handling
           Implicit Fork and Join
           Tokens
       Building blocks
           Nodes
               Action Node
               Decision Node
               Merge Node
               Initial Node
               Final Node
               Input Node
               Output Node
               Fork Node
               Join Node
               Accept Event Action Node
               Send Event Action Node
               Publish Event Action Node
               Time Event Action Node
               Data Store Node
               Structured Activity Node
               Iterative Activity Node
               Parallel Activity Node
           Flows
               Data Flow
               Control Flow
   Actions
   Entities
       Fields
       Mutations
       Projections
Extensions
   ClearScript
   FluentValidation
   JSON definitions
   Minimal APIs
   OneOf
   OpenTelemetry
   PlantUML
   Scheduling

Clone this wiki locally