Path: redb-route/redb.Route/Processors/ChoiceProcessor.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/Processors/ChoiceProcessor.cs ---- using redb.Route.Abstractions; namespace redb.Route.Processors; /// /// A single condition-action pair used by . /// When the predicate matches, the associated processor executes. /// public class WhenClause { /// Predicate to evaluate against the exchange. public Func Predicate => Condition.Matches; /// The condition of this branch, awaited through . public IPredicate Condition { get; } /// Processor to execute when the predicate is true. public IProcessor Processor { get; } /// Creates a when clause with a predicate and action. /// The condition to check. /// The processor to run when condition is met. public WhenClause(IPredicate condition, IProcessor processor) { Condition = condition ?? throw new ArgumentNullException(nameof(condition)); Processor = processor ?? throw new ArgumentNullException(nameof(processor)); } /// Creates a clause from a delegate, wrapped as a . public WhenClause(Func predicate, IProcessor processor) : this(new Predicates.LambdaPredicate(predicate ?? throw new ArgumentNullException(nameof(predicate))), processor) { } } /// /// Content-based router: evaluates when-clauses in order, /// executes the first matching branch. Falls through to Otherwise if no match. /// public class ChoiceProcessor : IProcessor { private readonly List _whenClauses = []; private IProcessor? _otherwise; /// Gets the list of when-clauses. public IReadOnlyList WhenClauses => _whenClauses; /// Gets the fallback processor (executed when no when-clause matches). public IProcessor? Otherwise => _otherwise; /// Adds a when-clause to the choice. /// Condition to evaluate. /// Processor to run if condition is true. /// This instance for fluent chaining. public ChoiceProcessor When(Func predicate, IProcessor processor) { _whenClauses.Add(new WhenClause(predicate, processor)); return this; } /// Adds a When branch guarded by a predicate instance. /// The condition of the branch. /// The processor to run when the condition holds. /// This processor for fluent chaining. public ChoiceProcessor When(IPredicate condition, IProcessor processor) { _whenClauses.Add(new WhenClause(condition, processor)); return this; } /// Sets the otherwise (fallback) processor. /// Fallback processor. /// This instance for fluent chaining. public ChoiceProcessor SetOtherwise(IProcessor processor) { _otherwise = processor ?? throw new ArgumentNullException(nameof(processor)); return this; } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { foreach (var clause in _whenClauses) { if (await clause.Condition.MatchesAsync(exchange).ConfigureAwait(false)) { await clause.Processor.Process(exchange, ct).ConfigureAwait(false); return; } } if (_otherwise != null) { await _otherwise.Process(exchange, ct).ConfigureAwait(false); } } }