Path: redb-route/redb.Route/Extensions/RouteDefinitionCamelDslExtensions.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/Extensions/RouteDefinitionCamelDslExtensions.cs
----
using System;
using System.Collections.Generic;
using redb.Route.Definitions;
using redb.Route.Expressions;
using redb.Route.Predicates;
namespace redb.Route.Abstractions;
///
/// Apache Camel parity overloads for :
///
/// nested-lambda configurators for every scope-opener EIP (Filter, Choice, Loop, etc.) — the configurator runs against the opened scope and the parent route is returned;
/// string-template (Simple language) overloads for expression-based methods;
/// convenience overloads that accept a registry name (idempotent repositories).
///
/// All overloads are pure facades over the canonical surface;
/// they do not introduce new behaviour at runtime.
///
public static class RouteDefinitionCamelDslExtensions
{
// ── helpers ───────────────────────────────────────────────────────────────
private static IRouteDefinition RunNested(TScope scope, Action configure)
where TScope : class, IRouteScope
{
ArgumentNullException.ThrowIfNull(scope);
ArgumentNullException.ThrowIfNull(configure);
configure(scope);
return scope.End();
}
// ── Filter (nested + string template) ─────────────────────────────────────
/// Apache Camel: opens a Filter scope, runs against it, then closes the scope.
public static IRouteDefinition Filter(this IRouteDefinition self, Func predicate, Action configure)
=> RunNested(self.Filter(predicate), configure);
/// Apache Camel: opens a Filter scope (expression predicate), runs , then closes the scope.
public static IRouteDefinition Filter(this IRouteDefinition self, IExpression expression, Action configure)
=> RunNested(self.Filter(expression), configure);
///
/// Apache Camel: Filter using a Simple language condition, either a template
/// ("${header.enabled}") or a boolean expression ("header.amount > 1000").
///
public static FilterDefinition Filter(this IRouteDefinition self, string simpleTemplate)
{
ArgumentNullException.ThrowIfNull(self);
var def = self.Filter(PredicateFactory.FromString(simpleTemplate));
def.SourceTemplate = simpleTemplate;
return def;
}
/// Apache Camel: Filter using a Simple condition plus a nested configurator.
public static IRouteDefinition Filter(this IRouteDefinition self, string simpleTemplate, Action configure)
=> RunNested(self.Filter(simpleTemplate), configure);
/// Apache Camel: Filter using an with nested configurator.
public static IRouteDefinition Filter(this IRouteDefinition self, IPredicate predicate, Action configure)
=> RunNested(self.Filter(predicate), configure);
// ── When / OrIfElse adapters ──────────────────────────────────────────────
///
/// Apache Camel: When using a Simple language condition, either a template
/// ("${header.flag}") or a boolean expression ("header.type == 'vip'").
///
public static WhenDefinition When(this ChoiceDefinition self, string simpleTemplate)
{
ArgumentNullException.ThrowIfNull(self);
var w = self.When(PredicateFactory.FromString(simpleTemplate));
w.SourceTemplate = simpleTemplate;
return w;
}
/// Apache Camel: When using a Simple language condition (from WhenDefinition).
public static WhenDefinition When(this WhenDefinition self, string simpleTemplate)
{
ArgumentNullException.ThrowIfNull(self);
var w = self.When(PredicateFactory.FromString(simpleTemplate));
w.SourceTemplate = simpleTemplate;
return w;
}
// ── Choice (nested) ───────────────────────────────────────────────────────
/// Apache Camel: opens a Choice scope, runs , then closes the scope.
public static IRouteDefinition Choice(this IRouteDefinition self, Action configure)
=> RunNested(self.Choice(), configure);
// ── Multicast (nested) ────────────────────────────────────────────────────
/// Apache Camel: opens a Multicast scope, runs , then closes the scope.
public static IRouteDefinition Multicast(this IRouteDefinition self, Action configure)
=> RunNested(self.Multicast(), configure);
/// Apache Camel parity: multicast to a static list of URIs.
public static IRouteDefinition Multicast(
this IRouteDefinition self,
System.Collections.Generic.IEnumerable uris,
bool parallelProcessing = false,
bool stopOnException = false,
Func? aggregationStrategy = null)
{
ArgumentNullException.ThrowIfNull(self);
ArgumentNullException.ThrowIfNull(uris);
var mc = self.Multicast()
.ParallelProcessing(parallelProcessing)
.StopOnException(stopOnException);
if (aggregationStrategy != null)
mc = mc.AggregationStrategy(aggregationStrategy);
foreach (var uri in uris)
mc.To(uri);
return mc.EndMulticast();
}
// ── Split (nested) ────────────────────────────────────────────────────────
/// Apache Camel: opens a Split scope, runs , then closes the scope.
public static IRouteDefinition Split(this IRouteDefinition self, Func> splitter, Action configure)
=> RunNested(self.Split(splitter), configure);
/// Apache Camel: opens a streaming (async) Split scope.
public static IRouteDefinition Split(this IRouteDefinition self, Func> asyncSplitter, Action configure)
=> RunNested(self.Split(asyncSplitter), configure);
/// Apache Camel: opens a streaming (async) Split scope with options.
public static IRouteDefinition Split(
this IRouteDefinition self,
Func> asyncSplitter,
Action configure,
bool stopOnException = false,
bool parallelProcessing = false)
{
var split = self.Split(asyncSplitter).StopOnException(stopOnException).Parallel(parallelProcessing);
return RunNested(split, configure);
}
/// Apache Camel: opens a Split scope (expression splitter), runs , then closes the scope.
public static IRouteDefinition Split(this IRouteDefinition self, IExpression expression, Action configure)
=> RunNested(self.Split(expression), configure);
// ── Loop (nested + string template) ───────────────────────────────────────
/// Apache Camel: opens a fixed-iteration Loop scope, runs , then closes it.
public static IRouteDefinition Loop(this IRouteDefinition self, int count, Action configure, bool copy = false, bool shareScope = true)
=> RunNested(self.Loop(count, copy, shareScope), configure);
/// Apache Camel: opens a conditional Loop scope, runs , then closes it.
public static IRouteDefinition Loop(this IRouteDefinition self, Func condition, Action configure)
=> RunNested(self.Loop(condition), configure);
/// Apache Camel: opens a count-by-factory Loop scope, runs , then closes it.
public static IRouteDefinition Loop(this IRouteDefinition self, Func countFactory, Action configure)
=> RunNested(self.Loop(countFactory), configure);
///
/// Apache Camel parity: opens a Loop scope that repeats while the condition string holds.
/// The counterpart of Loop(string), which takes an
/// iteration count rather than a condition. The condition is compiled the same way as in
/// Filter and When.
///
public static LoopDefinition LoopWhile(this IRouteDefinition self, string condition,
bool copy = false, bool shareScope = true)
{
ArgumentNullException.ThrowIfNull(self);
var predicate = PredicateFactory.FromString(condition);
return self.Loop(predicate, copy, shareScope);
}
/// Apache Camel parity: LoopWhile with a nested configurator that runs against the opened scope.
public static IRouteDefinition LoopWhile(this IRouteDefinition self, string condition,
Action configure, bool copy = false, bool shareScope = true)
=> RunNested(self.LoopWhile(condition, copy, shareScope), configure);
/// Apache Camel: LoopExpression with a nested configurator (IExpression overload).
public static IRouteDefinition LoopExpression(
this IRouteDefinition self,
IExpression expression,
Action configure,
bool copy = false,
bool shareScope = true)
{
ArgumentNullException.ThrowIfNull(self);
ArgumentNullException.ThrowIfNull(expression);
var loop = self.Loop(ex => expression.Evaluate(ex), copy, shareScope);
return RunNested(loop, configure);
}
// ── CircuitBreaker (nested) ───────────────────────────────────────────────
///
/// Apache Camel: opens a CircuitBreaker scope and runs to set
/// options (Threshold/ResetTimeout/FallBack/...). Returns the still-open CircuitBreaker
/// definition so subsequent chained calls (e.g. .Process(...), .To(...))
/// become the protected body. Close explicitly with EndCircuitBreaker() if you
/// need to chain steps outside the breaker.
///
public static IRouteDefinition CircuitBreaker(this IRouteDefinition self, Action configure)
{
ArgumentNullException.ThrowIfNull(configure);
var cb = self.CircuitBreaker();
configure(cb);
return cb;
}
// ── TryCatch (nested) ─────────────────────────────────────────────────────
/// Apache Camel: opens a TryCatch scope, runs , then closes the scope.
public static IRouteDefinition TryCatch(this IRouteDefinition self, Action configure)
=> RunNested(self.TryCatch(), configure);
// ── Throttle (nested) ─────────────────────────────────────────────────────
/// Apache Camel: opens a Throttle scope, runs , then closes the scope.
public static IRouteDefinition Throttle(this IRouteDefinition self, int maxPerPeriod, Action configure)
=> RunNested(self.Throttle(maxPerPeriod), configure);
/// Apache Camel parity: opens a Throttle scope with explicit period.
public static ThrottleDefinition Throttle(this IRouteDefinition self, int maxPerPeriod, TimeSpan period)
=> self.Throttle(maxPerPeriod).Period(period);
// ── Idempotent consumer (registry-name + nested overloads) ────────────────
/// Apache Camel idempotent consumer overload (keyExtractor first, then repository instance).
public static IdempotentConsumerDefinition IdempotentConsumer(
this IRouteDefinition self,
Func keyExtractor,
IIdempotentRepository repository,
bool skipDuplicate = true)
{
ArgumentNullException.ThrowIfNull(self);
return self.IdempotentConsumer(repository, keyExtractor, skipDuplicate);
}
/// Apache Camel idempotent consumer (keyExtractor + named registry).
public static IdempotentConsumerDefinition IdempotentConsumer(
this IRouteDefinition self,
Func keyExtractor,
string repositoryName,
bool skipDuplicate = true)
{
ArgumentNullException.ThrowIfNull(self);
return self.IdempotentConsumer(keyExtractor, repositoryName, skipDuplicate);
}
/// Apache Camel idempotent consumer overload with nested configurator (registry name).
public static IRouteDefinition IdempotentConsumer(
this IRouteDefinition self,
Func keyExtractor,
string repositoryName,
Action configure)
{
ArgumentNullException.ThrowIfNull(self);
return RunNested(self.IdempotentConsumer(keyExtractor, repositoryName), configure);
}
/// Apache Camel idempotent consumer overload with nested configurator (repository instance).
public static IRouteDefinition IdempotentConsumer(
this IRouteDefinition self,
Func keyExtractor,
IIdempotentRepository repository,
Action configure)
=> RunNested(self.IdempotentConsumer(keyExtractor, repository), configure);
/// Apache Camel idempotent consumer overload with nested configurator (repository, keyExtractor — canonical order).
public static IRouteDefinition IdempotentConsumer(
this IRouteDefinition self,
IIdempotentRepository repository,
Func keyExtractor,
Action configure)
=> RunNested(self.IdempotentConsumer(repository, keyExtractor), configure);
// ── Validate (string condition) ───────────────────────────────────────────
///
/// Apache Camel parity: validates the exchange against a condition string, throwing or
/// flagging when it does not hold. Uses the same condition compilation as Filter and When.
///
public static IRouteDefinition Validate(this IRouteDefinition self, string condition,
string errorMessage = "Validation failed", bool throwOnFailure = true)
{
ArgumentNullException.ThrowIfNull(self);
var predicate = PredicateFactory.FromString(condition);
return self.Validate(predicate, errorMessage, throwOnFailure);
}
// ── String-template expression aliases ────────────────────────────────────
// SetBodyExpression / SetHeaderExpression / SetPropertyExpression used to be declared here as
// extensions too. They were unreachable — the same names are members of IRouteDefinition, and a
// member always wins over an extension — and, until the template engines were unified, they
// led into a different dialect than the members did. Removed 2026-08-28.
//
// TransformExpression(string) went the same way in 4.0:
// the suffix means "takes an IExpression" everywhere else in the DSL, and a string form of the
// same thing is a second way to say it. Write Transform(Expr("${...}")).
/// Apache Camel: Throttle whose limit comes from an , evaluated on every message.
public static ThrottleDefinition ThrottleExpression(this IRouteDefinition self, IExpression expression, TimeSpan period)
{
ArgumentNullException.ThrowIfNull(self);
ArgumentNullException.ThrowIfNull(expression);
return self.Throttle(ex => expression.Evaluate(ex)).Period(period);
}
// ── OnException scope alias (RedeliveryDelay after pipeline downgrade) ────
///
/// Apache Camel alias for
/// surfaced on the parent facade (the scope-opener returns
/// the strongly-typed definition; subsequent extension calls downgrade the static type).
///
public static IRouteDefinition RedeliveryDelay(this IRouteDefinition self, TimeSpan delay)
{
if (self is OnExceptionDefinition oe) return oe.RedeliveryDelay(delay);
throw new InvalidOperationException(
$"RedeliveryDelay() must be called inside an OnException scope (got {self.GetType().Name}).");
}
// ── RichLog scope guards (only valid inside Log() scope) ──────────────────
/// Guard: Message is only valid inside a Log() scope.
public static IRouteDefinition Message(this IRouteDefinition self, string message)
=> throw new InvalidOperationException(
"Message() must be called inside a Log() scope.");
/// Guard: Message (factory) is only valid inside a Log() scope.
public static IRouteDefinition Message(this IRouteDefinition self, Func factory)
=> throw new InvalidOperationException(
"Message() must be called inside a Log() scope.");
/// Guard: Header reader is only valid inside a Log() scope.
public static IRouteDefinition Header(this IRouteDefinition self, string headerName)
=> throw new InvalidOperationException(
"Header() must be called inside a Log() scope.");
/// Guard: Property reader is only valid inside a Log() scope.
public static IRouteDefinition Property(this IRouteDefinition self, string propertyName)
=> throw new InvalidOperationException(
"Property() must be called inside a Log() scope.");
// ── String overloads for the remaining lambda-only EIPs ───────────────────
// The rule set by the 4.0 breaking bundle: the string form lives on the verb itself, never
// under an *Expression name. Every string compiles at declaration (StringExpression), so a
// malformed expression fails while the route is built, not on the first message.
///
/// Recipient List from a route-language expression. The expression may yield a collection of
/// URIs or a single delimited string ("${header.targets}" with "direct:a,direct:b").
///
public static IRouteDefinition RecipientList(
this IRouteDefinition self,
string recipientsExpression,
string delimiter = ",",
bool parallelProcessing = false,
bool stopOnException = false,
Func? aggregationStrategy = null)
{
ArgumentNullException.ThrowIfNull(self);
ArgumentException.ThrowIfNullOrWhiteSpace(recipientsExpression);
ArgumentException.ThrowIfNullOrEmpty(delimiter);
var expression = new StringExpression(recipientsExpression);
return self.RecipientList(
e => SplitUris(expression.Evaluate