Path: redb-route/redb.Route/Abstractions/IExchange.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/Abstractions/IExchange.cs
----
namespace redb.Route.Abstractions;
///
/// Represents a message exchange in the route pipeline.
/// Contains In message (always present), optional Out message (lazy),
/// exchange pattern, route-level properties, and error state.
/// Supports both C# idiomatic properties and Java-style methods.
///
public interface IExchange : IAsyncDisposable
{
// ── C# idiomatic API ──
/// Primary message flowing through the pipeline. Always non-null.
IMessage In { get; set; }
///
/// Response message from request-reply or explicit .Respond().
/// Null when InOnly (default). Lazy — not allocated until needed.
///
IMessage? Out { get; set; }
/// Current exchange pattern. Default = InOnly.
ExchangePattern Pattern { get; set; }
/// Whether Out is populated (non-null).
bool HasOut => Out != null;
///
/// Route-level metadata (RouteId, TRANSACT_ACTION, etc.).
/// Does NOT travel to brokers — use In.Headers for that.
///
IDictionary Properties { get; }
/// Gets a property value with type conversion.
/// Target type for the property value.
/// Property key.
///
/// The converted value; default when the property is missing or has no conversion to
/// (as Camel's getProperty(name, type)).
///
/// The value cannot be parsed as .
T? GetProperty(string key);
/// Current exception if processing failed.
Exception? Exception { get; set; }
/// Whether the current exception has been handled by error handling logic.
bool ExceptionHandled { get; set; }
/// Identifier of the route this exchange belongs to.
string? RouteId { get; set; }
///
/// The route context currently processing this exchange (Apache Camel's
/// exchange.getContext()). null for an exchange that was never handed to a route.
///
///
/// Stamped by the route wrapper on entry and restored on exit, so inside a direct: or
/// vm: sub-route it is the inner route's context — Camel's reading of
/// "current" — and after the sub-route returns, the caller sees its own context again. This is
/// what lets a processor reach measurements and services without capturing the context into a
/// lambda: (IEndpointStatistics)e.Context.GetEndpoint("direct:x").
///
IRouteContext? Context => null;
/// Unique identifier of this exchange instance, generated at creation.
string ExchangeId { get; }
/// Whether processing of this exchange has been stopped.
bool IsStopped { get; }
/// Stops further processing of this exchange in the pipeline.
void Stop();
///
/// Structural copy of the exchange (In, Out, Properties containers + a fresh DI scope). The
/// message body reference and property/header values are shared (shallow) — intentional
/// and relied upon across the framework (e.g. Splitter aggregation). For an isolated deep copy
/// use .
///
IExchange Clone();
///
/// Deep, isolated snapshot for checkpoint/replay. Unlike it deep-copies
/// the message body (In and Out) so the captured state is frozen and immune to later
/// in-place mutation of the payload. Properties/headers follow (values
/// shared) as they carry immutable route metadata.
///
/// No DI scope is created at capture: a snapshot is dormant data taken on every marker
/// pass and usually never replayed, so minting a scope here would leak one per message. A
/// snapshot that is never replayed owns nothing to dispose; a replay mints (and disposes) a
/// scope on demand.
///
/// Throws if a body cannot be safely deep-copied.
///
IExchange Snapshot();
// ── DI Scope ──
///
/// Optional scoped service provider bound to this exchange's lifetime.
/// Null when running without DI (tests, standalone). Processors can resolve
/// scoped services (DbContext, IRedbService, etc.) from this provider.
///
IServiceProvider? ServiceProvider => null;
///
/// Creates a child exchange with the given message, inheriting Properties, RouteId,
/// Pattern, and scope factory. A new DI scope is created for the child.
///
IExchange CreateChild(IMessage message)
{
var child = Clone();
child.In = message;
return child;
}
///
/// Creates a child exchange that shares the parent's DI scope (no new scope).
/// Use for sequential processing where children should reuse the same DB connection
/// and participate in the parent's transaction.
/// The child does NOT own the scope and will not dispose it.
///
IExchange CreateLinkedChild(IMessage message) => CreateChild(message);
///
/// Creates a deep copy of the exchange that shares the parent's DI scope (no new scope).
/// Like but without creating a new DI scope.
/// Use for sequential fan-out (Multicast, ScatterGather, RecipientList) where
/// each target should reuse the same DB connection and transaction.
///
IExchange CloneLinked() => Clone();
///
/// A copy that goes on as an exchange of its own, as Apache Camel's ExchangeHelper.createCorrelatedCopy
/// makes one for seda, multicast, recipient list, enrich and on-completion: everything copies, a
/// new , and the CamelCorrelationId property
/// () set to this exchange's id, the immediate parent's, as in Camel.
/// A fresh DI scope, as .
///
IExchange CorrelatedCopy() => throw new NotSupportedException(
$"{GetType().Name} does not implement CorrelatedCopy: an exchange that crosses to another unit of work needs a new id.");
///
/// sharing this exchange's DI scope, as : for a copy processed
/// in sequence within the caller's unit of work (a sequential multicast or recipient list).
///
IExchange CorrelatedCopyLinked() => throw new NotSupportedException(
$"{GetType().Name} does not implement CorrelatedCopyLinked: an exchange that crosses to another unit of work needs a new id.");
///
/// Releases DI scopes (main scope + named __redb_scope:* entries) and the resources registered with
/// , without disposing the exchange body or headers. For an exchange
/// no route owns (a Split or Multicast branch) it also ends the unit of work, running the completions registered with
/// .
/// Call after processing to free DB connections early while keeping message data alive for aggregation.
/// Idempotent — safe to call multiple times.
///
ValueTask ReleaseScopes() => default;
/// Disposes the exchange and its associated DI scope (if any).
ValueTask IAsyncDisposable.DisposeAsync() => default;
// ── Java-style API (kept forever, default interface methods) ──
/// Java-style: returns In message.
IMessage getIn() => In;
/// Java-style: returns Out message.
IMessage? getOut() => Out;
/// Java-style: returns property value by key.
object? getProperty(string key) => Properties.TryGetValue(key, out var v) ? v : null;
/// Java-style: returns property value cast to .
T? getProperty(string key) => GetProperty(key);
/// Java-style: sets property value.
void setProperty(string key, object? value) => Properties[key] = value;
/// Java-style: returns true if route processing is stopped.
bool isStop() => IsStopped;
/// Java-style: stops route processing (alias for Stop).
void RouteStop() => Stop();
/// Java-style: deep copy (alias for Clone).
IExchange copy() => Clone();
}