Workflow API
Workflow API
Section titled “Workflow API”Core types for defining and executing workflows in Strategos.
Workflow<TState>
Section titled “Workflow<TState>”Entry point for fluent workflow definitions. Creates a workflow builder for the specified state type.
Methods
Section titled “Methods”| Method | Returns | Description |
|---|---|---|
Create(string name) | IWorkflowBuilder<TState> | Creates a new workflow with the given name |
Example
Section titled “Example”Workflow<OrderState>.Create("process-order") .StartWith<ValidateOrderStep>() .Then<ProcessPaymentStep>() .Finally<FulfillOrderStep>();IWorkflowStep<TState>
Section titled “IWorkflowStep<TState>”Interface for implementing workflow steps. Each step receives state, executes logic, and returns updated state.
Methods
Section titled “Methods”| Method | Parameters | Returns | Description |
|---|---|---|---|
ExecuteAsync | TState state, StepContext context, CancellationToken ct | Task<StepResult<TState>> | Executes the step logic |
Example
Section titled “Example”public class ValidateOrderStep : IWorkflowStep<OrderState>{ public async Task<StepResult<OrderState>> ExecuteAsync( OrderState state, StepContext context, CancellationToken ct) { var isValid = await ValidateAsync(state.Order, ct);
return state .With(s => s.IsValid, isValid) .AsResult(); }}IWorkflowDefinition<TState>
Section titled “IWorkflowDefinition<TState>”Interface for workflow definition classes. Implemented by generated partial classes.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
Definition | WorkflowDefinition<TState> | The complete workflow definition |
Example
Section titled “Example”[Workflow("process-order")]public static partial class ProcessOrderWorkflow : IWorkflowDefinition<OrderState>{ public static WorkflowDefinition<OrderState> Definition => Workflow<OrderState>.Create("process-order") .StartWith<ValidateOrderStep>() .Finally<CompleteOrderStep>();}StepResult<TState>
Section titled “StepResult<TState>”Result type returned from step execution. Contains the updated state and optional routing information.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
State | TState | The updated workflow state |
BranchValue | object? | Optional value for branch routing |
IsComplete | bool | Whether workflow should terminate |
Creation Methods
Section titled “Creation Methods”| Method | Description |
|---|---|
state.AsResult() | Creates result with updated state |
state.AsResult(branchValue) | Creates result with branch routing |
StepResult<TState>.Complete(state) | Creates terminal result |
Example
Section titled “Example”// Simple state updatereturn state.With(s => s.Status, "Validated").AsResult();
// With branch routingreturn state.AsResult(state.OrderType); // Routes based on OrderType
// Terminal resultreturn StepResult<OrderState>.Complete(state);StepContext
Section titled “StepContext”Execution context passed to every step. Contains metadata about the current execution.
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
WorkflowId | Guid | Unique identifier for this workflow instance |
CorrelationId | string | Correlation ID for tracing |
Timestamp | DateTimeOffset | When the step execution started |
Phase | string | Current workflow phase name |
StepName | string | Current step name |
Metadata | IReadOnlyDictionary<string, object> | Additional context data |
Example
Section titled “Example”public async Task<StepResult<OrderState>> ExecuteAsync( OrderState state, StepContext context, CancellationToken ct){ _logger.LogInformation( "Processing order {WorkflowId} at phase {Phase}", context.WorkflowId, context.Phase);
// Step logic...}State Attributes
Section titled “State Attributes”Attributes that control how state properties are merged between steps.
[WorkflowState]
Section titled “[WorkflowState]”Marks a record as workflow state. Required for source generator to produce state reducers.
[WorkflowState]public record OrderState{ public Guid OrderId { get; init; } public string Status { get; init; }}[Append]
Section titled “[Append]”Merge lists by appending new items to existing items.
| Constraint | Value |
|---|---|
| Valid On | Collection properties (List<T>, IList<T>, etc.) |
| Behavior | Combines source and target lists |
[WorkflowState]public record OrderState{ [Append] public List<string> AuditLog { get; init; } = new();}Merge Behavior:
// Before: AuditLog = ["Created", "Validated"]// Update: AuditLog = ["Payment processed"]// After: AuditLog = ["Created", "Validated", "Payment processed"][Merge]
Section titled “[Merge]”Merge dictionaries. New values overwrite existing keys.
| Constraint | Value |
|---|---|
| Valid On | Dictionary properties (Dictionary<TKey, TValue>) |
| Behavior | Combines dictionaries, newer values win |
[WorkflowState]public record OrderState{ [Merge] public Dictionary<string, decimal> LinePrices { get; init; } = new();}Merge Behavior:
// Before: LinePrices = {"item1": 10.00, "item2": 20.00}// Update: LinePrices = {"item2": 25.00, "item3": 30.00}// After: LinePrices = {"item1": 10.00, "item2": 25.00, "item3": 30.00}Fluent Builder Methods
Section titled “Fluent Builder Methods”Methods available on the workflow builder for constructing workflow definitions.
Flow Control
Section titled “Flow Control”| Method | Description |
|---|---|
StartWith<TStep>() | First step in workflow (required) |
Then<TStep>() | Sequential step |
Finally<TStep>() | Terminal step (recommended) |
Branching
Section titled “Branching”| Method | Description |
|---|---|
Branch(selector, cases...) | Route based on state value |
BranchCase<TValue>(value, builder) | Define branch case |
Otherwise(builder) | Default branch case |
Parallel Execution
Section titled “Parallel Execution”| Method | Description |
|---|---|
Fork(paths...) | Execute paths in parallel |
Join<TStep>() | Merge parallel results |
| Method | Description |
|---|---|
RepeatUntil(condition, name, builder) | Loop until condition is true |
Human-in-the-Loop
Section titled “Human-in-the-Loop”| Method | Description |
|---|---|
AwaitApproval<TStep>() | Pause for human approval |
Example
Section titled “Example”Workflow<OrderState>.Create("process-order") .StartWith<ValidateOrderStep>() .Branch(s => s.OrderType, BranchCase<OrderType>(OrderType.Standard, path => path .Then<ProcessStandardStep>()), BranchCase<OrderType>(OrderType.Express, path => path .Then<ProcessExpressStep>()), Otherwise(path => path .Then<ProcessCustomStep>())) .Fork( path => path.Then<NotifyCustomerStep>(), path => path.Then<UpdateInventoryStep>()) .Join<AggregateResultsStep>() .AwaitApproval<ShipmentApprovalStep>() .Finally<FulfillOrderStep>();