Path: redb-route/redb.Route/Definitions/AggregateDefinition.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/AggregateDefinition.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Scope-opener definition for the Aggregator EIP.
/// Collects exchanges sharing the same correlation key, merges them via the aggregation strategy,
/// and forwards the completed aggregate to the body pipeline when the completion predicate is satisfied.
/// Inherits the leaf DSL from ;
/// child steps build the target pipeline executed on completed aggregates.
/// Close with .
///
public class AggregateDefinition : RouteDefinitionBase, IRouteScope
{
private readonly Func _correlationKey;
private readonly Func _aggregationStrategy;
private readonly Func _completionPredicate;
internal AggregateDefinition(
Func correlationKey,
Func aggregationStrategy,
Func completionPredicate)
{
_correlationKey = correlationKey ?? throw new ArgumentNullException(nameof(correlationKey));
_aggregationStrategy = aggregationStrategy ?? throw new ArgumentNullException(nameof(aggregationStrategy));
_completionPredicate = completionPredicate ?? throw new ArgumentNullException(nameof(completionPredicate));
}
// ── Navigation ─────────────────────────────────────────────────────────────
/// Closes the Aggregate scope and returns the parent route definition.
public IRouteDefinition EndAggregate()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndAggregate() called without a parent route."));
///
public IRouteDefinition End() => EndAggregate();
///
/// Optional inactivity timeout (Apache Camel completionTimeout): a group that receives
/// nothing for this long completes with what it has. Set by the string-overload sugar;
/// null keeps the predicate-only behaviour.
///
public TimeSpan? CompletionTimeout { get; internal set; }
///
/// Optional Apache Camel completionSize: a group completes once it has combined this many exchanges. Set by
/// the string-overload sugar; null keeps the predicate-only behaviour.
///
public int? CompletionSize { get; internal set; }
///
/// Apache Camel forceCompletionOnStop: a group still open when the context stops completes with what it has,
/// once the consumers have stopped and while the producers are still alive. Without it an open group is dropped on
/// stop, with a warning naming how many.
///
internal bool? ForceCompletionOnStopEnabled { get; private set; }
/// Completes the open groups when the context stops (Apache Camel forceCompletionOnStop).
public AggregateDefinition ForceCompletionOnStop(bool value = true)
{
ForceCompletionOnStopEnabled = value;
return this;
}
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
IProcessor target = NodePipeline.Body(context, Outputs);
return new AggregatorProcessor(_correlationKey, _aggregationStrategy, _completionPredicate, target, CompletionTimeout)
{
CompletionSize = CompletionSize,
ForceCompletionOnStop = ForceCompletionOnStopEnabled == true,
Logger = context.GetService()?.CreateLogger(),
// A group completed by the timeout or on stop runs after its exchanges left the route's error wrappers.
DetachedTarget = (context as Core.RouteContext)?.DetachedErrorScope?.WrapDetached(target),
};
}
}