Path: redb-route/redb.Route/Definitions/ChoiceDefinition.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/ChoiceDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Expressions;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Content-based router scope opener (Choice EIP).
/// Add branches with / .
/// Close with .
///
/// Inherits the entire leaf DSL from ;
/// however, leaf calls placed directly on a Choice scope are not part of the
/// standard EIP shape — use When / Otherwise branches instead.
///
///
public class ChoiceDefinition : RouteDefinitionBase, IRouteScope, ICompositeScope, IBranchingDefinition
{
private readonly List _whens = [];
private OtherwiseDefinition? _otherwise;
/// All registered When branches, in declaration order.
public IReadOnlyList Whens => _whens;
/// The Otherwise branch (null if not declared).
public OtherwiseDefinition? OtherwiseBranch => _otherwise;
///
/// The When/Otherwise branches — logical children that live outside , so a
/// generic tree-walk (validation) can reach steps nested inside a branch.
///
public IEnumerable Branches
{
get
{
foreach (var when in _whens) yield return when;
if (_otherwise is not null) yield return _otherwise;
}
}
// ── Branch builders ───────────────────────────────────────────────────────
/// Opens a When branch with a bool predicate.
public WhenDefinition When(Func predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
var when = new WhenDefinition(predicate, this);
_whens.Add(when);
return when;
}
/// Opens a When branch with an predicate.
public WhenDefinition When(IExpression expression)
{
ArgumentNullException.ThrowIfNull(expression);
var when = new WhenDefinition(Predicates.PredicateFactory.FromExpression(expression), this);
_whens.Add(when);
when.SourceExpression = expression;
return when;
}
/// Opens a When branch guarded by an ; the predicate is
/// stored as the branch condition and awaited per message.
public WhenDefinition When(IPredicate predicate)
{
ArgumentNullException.ThrowIfNull(predicate);
var when = new WhenDefinition(predicate, this);
_whens.Add(when);
when.SourcePredicate = predicate;
return when;
}
/// Apache Camel nested-lambda overload: opens a When branch with the given predicate,
/// populates it via , and returns this
/// so further When/Otherwise branches can be chained.
public ChoiceDefinition When(Func predicate, Action branch)
{
ArgumentNullException.ThrowIfNull(branch);
var w = When(predicate);
branch(w);
return this;
}
/// Nested-lambda overload accepting an predicate.
public ChoiceDefinition When(IExpression expression, Action branch)
{
ArgumentNullException.ThrowIfNull(branch);
var w = When(expression);
branch(w);
return this;
}
/// Nested-lambda overload accepting an , as does:
/// .When(Header("type").isEqualTo("order"), r => r.To(...)).
public ChoiceDefinition When(IPredicate predicate, Action branch)
{
ArgumentNullException.ThrowIfNull(branch);
var w = When(predicate);
branch(w);
return this;
}
/// Nested-lambda Otherwise overload.
public ChoiceDefinition Otherwise(Action branch)
{
ArgumentNullException.ThrowIfNull(branch);
branch(Otherwise());
return this;
}
/// Opens the Otherwise (fallback) branch.
/// Thrown if Otherwise was already set.
public OtherwiseDefinition Otherwise()
{
if (_otherwise != null)
throw new InvalidOperationException("Otherwise() can only be called once per Choice scope.");
_otherwise = new OtherwiseDefinition(this);
return _otherwise;
}
/// Closes this choice scope and returns the parent route definition.
public IRouteDefinition EndChoice()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndChoice() called without a parent route."));
///
public IRouteDefinition End() => EndChoice();
// ── IProcessorDefinition ──────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
var choice = new ChoiceProcessor();
foreach (var when in _whens)
{
IProcessor body = BuildPipeline(when.Outputs, context);
choice.When(when.Condition, body);
}
if (_otherwise != null)
{
choice.SetOtherwise(BuildPipeline(_otherwise.Outputs, context));
}
return choice;
}
private static IProcessor BuildPipeline(IList outputs, IRouteContext context)
=> NodePipeline.Body(context, outputs);
}
///
/// A single When branch inside a .
/// Inherits the leaf DSL from ; close with
/// , , or .
///
public class WhenDefinition : RouteDefinitionBase, IRouteScope, ICompositeScope, IConditionSource
{
internal readonly IPredicate Condition;
private readonly ChoiceDefinition _choice;
///
public IPredicate? SourcePredicate { get; internal set; }
///
public IExpression? SourceExpression { get; internal set; }
///
public string? SourceTemplate { get; internal set; }
internal WhenDefinition(IPredicate condition, ChoiceDefinition choice)
{
Condition = condition ?? throw new ArgumentNullException(nameof(condition));
_choice = choice;
Parent = choice;
}
internal WhenDefinition(Func predicate, ChoiceDefinition choice)
: this(new Predicates.LambdaPredicate(predicate ?? throw new ArgumentNullException(nameof(predicate))), choice)
{
}
// ── Navigation ────────────────────────────────────────────────────────────
/// Closes this When branch and returns the parent choice.
public ChoiceDefinition EndWhen() => _choice;
/// Opens another When branch on the parent choice.
public WhenDefinition When(Func predicate) => _choice.When(predicate);
/// Opens another When branch with an expression predicate.
public WhenDefinition When(IExpression expression) => _choice.When(expression);
/// Opens another When branch guarded by an .
public WhenDefinition When(IPredicate predicate) => _choice.When(predicate);
/// Opens the Otherwise fallback branch on the parent choice.
public OtherwiseDefinition Otherwise() => _choice.Otherwise();
/// Closes the entire Choice scope and returns the parent route definition.
public IRouteDefinition EndChoice() => _choice.EndChoice();
///
public IRouteDefinition End() => EndChoice();
// ── IProcessorDefinition ──────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
=> throw new InvalidOperationException(
"WhenDefinition is compiled via its parent ChoiceDefinition.CreateProcessor.");
}
///
/// The Otherwise (fallback) branch inside a .
/// Inherits the leaf DSL from ; close with
/// or .
///
public class OtherwiseDefinition : RouteDefinitionBase, IRouteScope, ICompositeScope
{
private readonly ChoiceDefinition _choice;
internal OtherwiseDefinition(ChoiceDefinition choice)
{
_choice = choice;
Parent = choice;
}
// ── Navigation ────────────────────────────────────────────────────────────
/// Closes this Otherwise branch and returns the parent choice.
public ChoiceDefinition EndOtherwise() => _choice;
/// Closes the entire Choice scope and returns the parent route definition.
public IRouteDefinition EndChoice() => _choice.EndChoice();
///
public IRouteDefinition End() => EndChoice();
// ── IProcessorDefinition ──────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
=> throw new InvalidOperationException(
"OtherwiseDefinition is compiled via its parent ChoiceDefinition.CreateProcessor.");
}