Path: redb-route/redb.Route/Definitions/SagaDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/SagaDefinition.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Definition for a Saga step. Collects saga step entries and builds a
/// at route-build time.
/// Supports both callback-style (via ) and
/// fluent-scope-style (via / ).
/// Inherits the leaf DSL from .
///
public class SagaDefinition : RouteDefinitionBase, ISagaDefinition, IRouteScope
{
private readonly List _entries = [];
/// The collected saga step entries (forward action + optional compensation).
public IReadOnlyList Entries => _entries;
/// Optional callback invoked after all steps complete successfully.
public Func? CompletionCallback { get; private set; }
// ── ISagaDefinition (callback-style builder) ──────────────────────────────
///
public ISagaDefinition Step(Action action, Action compensate)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(compensate);
_entries.Add(new SagaStepEntry(
(e, _) => { action(e); return Task.CompletedTask; },
(e, _) => { compensate(e); return Task.CompletedTask; }));
return this;
}
///
public ISagaDefinition Step(
Func action,
Func compensate)
{
ArgumentNullException.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(compensate);
_entries.Add(new SagaStepEntry(action, compensate));
return this;
}
///
public ISagaDefinition Step(Action action)
{
ArgumentNullException.ThrowIfNull(action);
_entries.Add(new SagaStepEntry(
(e, _) => { action(e); return Task.CompletedTask; },
null));
return this;
}
///
public ISagaDefinition Step(Func action)
{
ArgumentNullException.ThrowIfNull(action);
_entries.Add(new SagaStepEntry(action, null));
return this;
}
///
public ISagaDefinition Step(IProcessor action, IProcessor? compensate = null)
{
ArgumentNullException.ThrowIfNull(action);
_entries.Add(new SagaStepEntry(
action.Process,
compensate is null ? null : compensate.Process));
return this;
}
///
public ISagaDefinition OnCompletion(Action callback)
{
ArgumentNullException.ThrowIfNull(callback);
CompletionCallback = (e, _) => { callback(e); return Task.CompletedTask; };
return this;
}
///
public ISagaDefinition OnCompletion(Func callback)
{
ArgumentNullException.ThrowIfNull(callback);
CompletionCallback = callback;
return this;
}
// ── Scope / fluent-chain DSL ──────────────────────────────────────────────
/// Adds a synchronous saga step with both action and compensation.
public SagaDefinition SagaStep(Action action, Action compensate)
{
Step(action, compensate);
return this;
}
/// Adds a synchronous saga step with no compensation.
public SagaDefinition SagaStep(Action action)
{
Step(action);
return this;
}
/// Adds an asynchronous saga step with both action and compensation.
public SagaDefinition SagaStep(
Func action,
Func compensate)
{
Step(action, compensate);
return this;
}
/// Adds an asynchronous saga step with no compensation.
public SagaDefinition SagaStep(Func action)
{
Step(action);
return this;
}
/// Sets the saga completion callback (synchronous).
public SagaDefinition OnSagaCompletion(Action callback)
{
OnCompletion(callback);
return this;
}
/// Sets the saga completion callback (asynchronous).
public SagaDefinition OnSagaCompletion(Func callback)
{
OnCompletion(callback);
return this;
}
/// Closes this saga scope and returns the parent route definition.
public IRouteDefinition EndSaga()
{
return (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndSaga() can only be called on a saga scope opened via RouteDefinition.Saga()."));
}
///
public IRouteDefinition End() => EndSaga();
// ── ProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
if (_entries.Count == 0)
throw new InvalidOperationException("Saga must have at least one step.");
var logger = context.GetService()?.CreateLogger();
return new SagaProcessor(_entries.ToArray(), CompletionCallback, logger);
}
}
/// A single saga step entry: forward action + optional compensation.
/// Forward action to execute.
/// Compensation to run on rollback (null = no compensation).
public sealed record SagaStepEntry(
Func Action,
Func? Compensate);