Guides & Recipes — Processes
Verified workflow recipes compiled on .NET 10 (ImplicitUsings=disable, Nullable=enable) with EricksonLopez.Processes.Analyzers attached.
Recipe 1: Multi-Step Transition (Advance -> Complete)
Section titled “Recipe 1: Multi-Step Transition (Advance -> Complete)”using System;using System.Threading.Tasks;using EricksonLopez.Processes.Abstractions;
namespace Example.ProcessesQuickStart;
public sealed record InvoiceSagaState(Guid InvoiceId, bool Authorized, bool Captured) : IProcessState;public sealed record AuthorizeRequested(Guid InvoiceId);public sealed record CaptureCompleted(Guid InvoiceId);
[SagaDefinition("invoice-settlement")]public sealed class InvoiceSettlementSaga : ISaga<InvoiceSagaState>, IProcessHandler<InvoiceSagaState, AuthorizeRequested>, IProcessHandler<InvoiceSagaState, CaptureCompleted>{ public ProcessType Type => ProcessType.From("invoice-settlement"); public ProcessVersion Version => ProcessVersion.Initial;
public ValueTask<ProcessTransitionResult<InvoiceSagaState>> HandleAsync( InvoiceSagaState state, AuthorizeRequested eventMessage, ProcessContext context) { var next = state with { InvoiceId = eventMessage.InvoiceId, Authorized = true }; return ValueTask.FromResult( ProcessTransitionResult<InvoiceSagaState>.Advance(next, ProcessStatus.Running)); }
public ValueTask<ProcessTransitionResult<InvoiceSagaState>> HandleAsync( InvoiceSagaState state, CaptureCompleted eventMessage, ProcessContext context) { var done = state with { Captured = true }; return ValueTask.FromResult(ProcessTransitionResult<InvoiceSagaState>.Complete(done)); }}Recipe 2: Triggering Compensation Without reason: (Compensate -> Compensated)
Section titled “Recipe 2: Triggering Compensation Without reason: (Compensate -> Compensated)”Pass [CompensationAction.Create("StepName", payload)] directly to ProcessTransitionResult<TState>.Compensate(state, actions) (do not pass a reason: named argument) and return .Compensated(...) from ICompensationHandler<TState>.CompensateAsync:
using System;using System.Threading.Tasks;using EricksonLopez.Processes.Abstractions;
namespace Example.ProcessesQuickStart;
public static class CompensationHelper{ public static ProcessTransitionResult<OrderSagaState> BeginRollback(OrderSagaState state, Guid orderId) { return ProcessTransitionResult<OrderSagaState>.Compensate( state, [CompensationAction.Create("ReleaseInventory", orderId)]); }}