Path: redb-route/redb.Route/Transactions/TransactionPolicy.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/Transactions/TransactionPolicy.cs
----
using System.Transactions;
namespace redb.Route.Transactions;
///
/// Defines the transactional behavior for a route or a segment of a route.
/// Wraps and with sensible defaults.
///
public sealed class TransactionPolicy
{
/// Default policy: , 30-second timeout.
public static readonly TransactionPolicy Default = new();
///
/// Policy that always creates a new transaction, suspending any ambient one.
///
public static readonly TransactionPolicy RequiresNew = new()
{
ScopeOption = TransactionScopeOption.RequiresNew
};
///
/// Policy that suppresses the ambient transaction so that the inner code runs
/// without any transaction context.
///
public static readonly TransactionPolicy Suppress = new()
{
ScopeOption = TransactionScopeOption.Suppress
};
///
/// Policy that requires an ambient transaction to already exist; throws
/// at scope creation time if there is none.
/// Use this to enforce that a route segment can never run outside of a transaction.
///
public static readonly TransactionPolicy Mandatory = new()
{
// System.Transactions has no Mandatory option directly; emulated via a custom
// ScopeOption marker checked in CreateScope (see implementation below).
ScopeOption = MandatoryScopeMarker
};
// Sentinel value outside the defined enum range used to mark Mandatory policies.
// System.Transactions.TransactionScopeOption only defines Required, RequiresNew, Suppress.
private const TransactionScopeOption MandatoryScopeMarker = (TransactionScopeOption)1000;
///
/// Determines whether to join an existing ambient transaction, create a new one,
/// or suppress it entirely. Default is .
///
public TransactionScopeOption ScopeOption { get; init; } = TransactionScopeOption.Required;
///
/// Transaction timeout. Default is 30 seconds. Use
/// for long-running scenarios (not recommended for production).
///
public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(30);
///
/// Transaction isolation level. Default is .
///
public IsolationLevel IsolationLevel { get; init; } = IsolationLevel.ReadCommitted;
///
/// Whether a block with this policy joins a transaction that is already running (Required, Mandatory) rather than
/// running a unit of work of its own (RequiresNew, Suppress).
///
internal bool JoinsRunningTransaction =>
ScopeOption == TransactionScopeOption.Required || ScopeOption == MandatoryScopeMarker;
///
/// Creates a based on this policy with .
///
/// A configured transaction scope.
///
/// Thrown when is the Mandatory marker and no ambient
/// is present.
///
internal TransactionScope CreateScope()
{
if (ScopeOption == MandatoryScopeMarker)
{
if (Transaction.Current is null)
{
throw new InvalidOperationException(
"TransactionPolicy.Mandatory requires an ambient System.Transactions.Transaction " +
"to be present, but Transaction.Current was null.");
}
// Join the existing ambient transaction. We deliberately omit TransactionOptions
// here because System.Transactions throws ArgumentException when the requested
// IsolationLevel differs from the ambient transaction's level. Mandatory honors
// the ambient transaction's isolation level by definition.
return new TransactionScope(
TransactionScopeOption.Required,
TransactionScopeAsyncFlowOption.Enabled);
}
var options = new TransactionOptions
{
Timeout = Timeout,
IsolationLevel = IsolationLevel
};
return new TransactionScope(
ScopeOption,
options,
TransactionScopeAsyncFlowOption.Enabled);
}
///
/// Parses a well-known policy name into a instance.
/// Supported values: "Required", "RequiresNew", "Suppress", "Mandatory".
///
/// Case-insensitive policy name.
/// Matching policy.
/// Thrown when the name is not recognized.
public static TransactionPolicy FromName(string name) =>
name?.Trim().ToUpperInvariant() switch
{
"REQUIRED" => Default,
"REQUIRESNEW" => RequiresNew,
"SUPPRESS" => Suppress,
"MANDATORY" => Mandatory,
_ => throw new ArgumentException($"Unknown transaction policy name: '{name}'. Expected: Required, RequiresNew, Suppress, Mandatory.", nameof(name))
};
}