Skip to content

Actions

Mikołaj Milewski edited this page Apr 5, 2025 · 1 revision

Overview

Action is a Behavior type that simply executes the block of code, accepting input Tokens and producing output Tokens.

Action consists of:

  1. Unique name that identifies the Action Class,
  2. Logic - code that is to be executed.

Observability

Developers can register a class that implements IActionInterceptor to observe or influence the execution of the Action.

Versioning

Action definitions can be versioned, ensuring that new versions do not affect already running instances.

Definition

Action does not represent any UML model, it is simply a code block. Stateflows Action can be defined directly in Program.cs:

using Stateflows;
using Stateflows.Actions;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddStateflows(b => b
        .AddAction("DatabaseCleanup", async c =>
        {
            /* database cleanup logic */
        })
    );

Above example Action directly in Program.cs file - it is the simplest way to define it. Yet, for most scenarions it is recommended to define Actions as separate classes:

using Stateflows.Actions;

namespace Example
{
    public class DatabaseCleanup : IAction
    {
        public async Task ExecuteAsync()
        {
            /* database cleanup logic */
        }
    }
}

...and then register it in Program.cs:

using Stateflows;
using Example;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddStateflows(b => b
        .AddAction<DatabaseCleanup>()
    );

Type-based definition presented above requires a class that implements the IAction interface.

Interacting with Actions

Action Behavior can be interacted with by sending Events to it. To do this, the Behavior handle must be located using IActionLocator:

using Stateflows;
using Stateflows.Actions;

namespace Example
{
    public class DocumentService(IActionLocator locator) // IActionLocator is available via Dependency Injection
    {
        public Task ApproveAsync(int documentId)
        {
            if (locator.TryLocateAction(new ActionId("DatabaseCleanup", Random.Shared.Next()), out var databaseCleanup))
            {
                await databaseCleanup.SendAsync(new Initialize());
            }
        }
    }
}

Overview
Installation
Behaviors
   State Machines
       Building blocks
           States
               State
               Composite State
               Orthogonal State
               Final State
           Pseudostates
               Choice
               Junction
               Fork
               Join
           Transitions
               Transition
               Default Transition
               Internal Transition
       Concepts
           Evaluation of Transitions
   Activities
       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
               Data Store Node
               Structured Activity Node
               Iterative Activity Node
               Parallel Activity Node
           Flows
               Data Flow
               Control Flow
       Concepts
           Implicit fork and join
   Actions

Clone this wiki locally