Skip to content

State Machine Default Transition

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

Default Transition

Default Transition is the way to move from one State to another, by this changing stable configuration of State Machine.

Default Transitions are not explicitly triggered by Events sent to State Machine Behavior, but are automaticaly triggered by Completion Event sent to State Machine Behavior by Stateflows framework after each successful Transition.

Default Transition consist of:

  • Source State or Pseudostate,
  • Target State or Pseudostate,
  • Guard, a predicate logic (that returns boolean value) that is evaluated in order to check if Transition can happen or not,
  • Effect, logic that is executed when Transition is happening.

Definition

In UML, Default Transition is represented by an arrow that connects two States or Pseudostates, but has no Trigger:

stateDiagram-v2
SomeState --> AnotherState
Loading

If there is any logic within Transition, it is described textually using following special characters:

stateDiagram-v2
SomeState --> AnotherState : [Guard]<br/>/Effect
Loading

Equivalent Stateflows notation of Default Transition:

Lambda style

Definition fragment (inside the builder callback):

    /* fragment of State Machine definition */
    .AddState("SomeState", b => b
        .AddDefaultTransition("AnotherState", b => b
            .AddGuard(async c => true /* bool-returning logic here */)
            .AddEffect(c => Task.CompletedTask) 
        )
    )
    .AddState("AnotherState")

Using lambda style here means that States are referred by string name and each bit of Transition logic is implemented as lambda function passed as parameters to Add* methods in Transition definition.

Typed style

    public class SomeState : IState
    { }

    public class AnotherState : IState
    { }

    public class MyDefaultTransition : IDefaultTransitionGuard, IDefaultTransitionEffect
    {
        public Task<bool> GuardAsync()
        {
            /* bool-returning logic here */
            return Task.FromResult(true);
        }

        public Task EffectAsync()
        {
            return Task.CompletedTask;
        }
    }

Definition fragment (inside the builder callback):

    /* fragment of State Machine definition */
    .AddState<SomeState>(b => b
        .AddDefaultTransition<MyDefaultTransition, AnotherState>()
    )
    .AddState<AnotherState>()

Using typed style here means that States and Transitions are classes that is referenced in typed overloads of Add* methods in State Machine definition.

Note: AddDefaultTransition<>() method accepts several type parameters. Regardless the overload variant, the order of those parameters is always the same: Transition class > Target State / Pseudostate class.

Class can be used as Default Transition if it implements at least one interface from IDefaultTransition*<TEvent> family:

Transition logic may also be split into several classes like in the example:

    public class SomeState : IState
    { }

    public class AnotherState : IState
    { }

    public class MyDefaultGuard : IDefaultTransitionGuard
    {
        public Task<bool> GuardAsync()
        {
            /* bool-returning logic here */
            return Task.FromResult(true);
        }
    }

    public class MyDefaultEffect : IDefaultTransitionEffect
    {
        public Task EffectAsync()
        {
            return Task.CompletedTask;
        }
    }

Definition fragment (inside the builder callback):

    /* fragment of State Machine definition */
    .AddState<SomeState>(b => b
        .AddDefaultTransition<AnotherState>(b => b
            .AddGuard<MyDefaultGuard>()
            .AddEffect<MyDefaultEffect>()
        )
    )
    .AddState<AnotherState>()

This technique enables reusing of Default Transition's logic.

Transition classes can constructor-inject ITransitionContext, IStateMachineContext, common behavior/execution contexts, and application services. They cannot inject a current IStateContext; use ITransitionContext.Source / .Target to access the source and target state context.

Generic Transitions

If Default Transition logic is suitable to be reused in other Transitions, Default Transition classes may be created by implementing at least one interface from ITransition* family:

  • ITransitionGuard - represents Transition with Guard logic implemented as GuardAsync() method,
  • ITransitionEffect - represents Transition with Effect logic implemented as EffectAsync() method,
  • ITransitionDefinition - represents Transition with additional Build() method that allows to extend its definition, for example to define Guards and Effects in Transition class.

Such classes are called Generic Transitions and may be used to declare all types of Transitions.

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