Path: redb-route/redb.Route/Definitions/MiscDefinition.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/MiscDefinition.cs ---- using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Processors; using redb.Route.Transactions; namespace redb.Route.Definitions; /// /// Leaf definition that begins an imperative transaction scope on the exchange. /// public sealed class BeginTransactionDefinition : ProcessorDefinition { private readonly TransactionPolicy? _policy; /// Creates a begin-transaction definition. /// Transaction policy (null = default Required). public BeginTransactionDefinition(TransactionPolicy? policy = null) { _policy = policy; } /// public override IProcessor CreateProcessor(IRouteContext context) { var logger = context.GetService()?.CreateLogger(); return new BeginTransactionProcessor(_policy, logger); } } /// /// Leaf definition that commits the imperative transaction scope and all deferred actions. /// public sealed class CommitTransactionDefinition : ProcessorDefinition { /// public override IProcessor CreateProcessor(IRouteContext context) { var logger = context.GetService()?.CreateLogger(); return new CommitTransactionProcessor(logger); } } /// /// Leaf definition that rolls back the imperative transaction scope and all deferred actions. /// public sealed class RollbackTransactionDefinition : ProcessorDefinition { /// public override IProcessor CreateProcessor(IRouteContext context) { var logger = context.GetService()?.CreateLogger(); return new RollbackTransactionProcessor(logger); } } /// /// Leaf definition for .RollbackAll(), Camel's markRollbackOnly(): marks the unit of work for rollback, /// rolls back the deferred sends and stops the route. The enclosing .Transacted() block then rolls the database /// back without an exception, and the consumer does not acknowledge the message. /// public sealed class RollbackAllDefinition : ProcessorDefinition { /// public override IProcessor CreateProcessor(IRouteContext context) { var logger = context.GetService()?.CreateLogger(); return new DelegateProcessor(async (exchange, ct) => { exchange.MarkRollbackOnly(); // An imperative block is rolled back here: the route stops, so its CommitTransaction never runs. if (exchange.Properties.ContainsKey(BeginTransactionProcessor.ScopePropertyKey)) await new RollbackTransactionProcessor(logger).Process(exchange, ct).ConfigureAwait(false); else await TransactedActions.RollbackAll(exchange, logger, ct).ConfigureAwait(false); exchange.Stop(); }); } } /// /// Leaf definition that sets the exchange pattern. /// public sealed class SetPatternDefinition : ProcessorDefinition { private readonly ExchangePattern _pattern; /// Creates a set-pattern definition. public SetPatternDefinition(ExchangePattern pattern) { _pattern = pattern; } /// public override IProcessor CreateProcessor(IRouteContext context) => new DelegateProcessor(exchange => exchange.Pattern = _pattern); } /// /// Leaf definition that creates an Out response by applying a factory to the exchange. /// public sealed class RespondDefinition : ProcessorDefinition { private readonly Func _factory; /// Creates a respond definition. public RespondDefinition(Func factory) { ArgumentNullException.ThrowIfNull(factory); _factory = factory; } /// public override IProcessor CreateProcessor(IRouteContext context) => new DelegateProcessor(exchange => { var responseBody = _factory(exchange); exchange.Out = exchange.In.Clone(); exchange.Out.Body = responseBody; }); } /// /// Leaf definition that resolves a DI service and invokes a method on it (service activator/bean). /// public sealed class BeanDefinition : ProcessorDefinition { private readonly Type _serviceType; private readonly Func _method; /// Service type resolved from DI. public Type ServiceType => _serviceType; /// Normalized delegate invoked with (service, exchange, ct). public Func Method => _method; /// Creates a bean definition. /// Service type to resolve from the exchange's ServiceProvider. /// Normalized delegate: (service, exchange, ct) → Task. public BeanDefinition(Type serviceType, Func method) { ArgumentNullException.ThrowIfNull(serviceType); ArgumentNullException.ThrowIfNull(method); _serviceType = serviceType; _method = method; } /// public override IProcessor CreateProcessor(IRouteContext context) { var serviceType = _serviceType; var method = _method; return new DelegateProcessor(async (exchange, ct) => { var provider = exchange.ServiceProvider ?? throw new InvalidOperationException( $"Bean<{serviceType.Name}> requires a ServiceProvider on the exchange. " + "Ensure the route context is configured with DI."); var service = provider.GetRequiredService(serviceType); await method(service, exchange, ct).ConfigureAwait(false); }); } } /// /// Leaf definition that wraps a forward-only body with a seekable /// -backed cache, enabling multiple re-reads. /// Non-Stream bodies pass through unchanged. /// Equivalent to Apache Camel's streamCaching() inline DSL step. /// public sealed class StreamCachingDefinition : ProcessorDefinition { private readonly long? _spoolThreshold; /// Creates a stream caching definition. /// Bytes threshold before spooling to disk (null = default 128 KB). public StreamCachingDefinition(long? spoolThreshold = null) { _spoolThreshold = spoolThreshold; } /// public override IProcessor CreateProcessor(IRouteContext context) { var globalOptions = Extensions.RouteContextExtensions.GetStreamCacheOptions(context); var options = _spoolThreshold.HasValue ? new Configuration.StreamCacheOptions { SpoolThreshold = _spoolThreshold.Value, TempDirectory = globalOptions.TempDirectory, } : globalOptions; return new Processors.StreamCachingTransformer(options); } }