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