Path: redb/redb.Core/Data/TransactionHooks.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Data/TransactionHooks.cs
----
using System;
using System.Transactions;
namespace redb.Core.Data
{
///
/// The transaction a context takes part in - its explicit redb transaction, else the ambient
/// - and a callback for its end. One place for what the cache publication and the
/// lazy-load rule both need (review after 4.0.0).
///
public static class TransactionHooks
{
/// The transaction object the context takes part in, or null outside any transaction.
public static object? TransactionOf(IRedbContext context)
{
ArgumentNullException.ThrowIfNull(context);
if (context.CurrentTransaction is { IsActive: true } explicitTransaction)
return explicitTransaction;
return Transaction.Current;
}
///
/// Runs with whether the transaction committed once the context's transaction
/// ends. Returns false, running nothing, when the context is in no transaction.
///
public static bool OnCompleted(IRedbContext context, Action onCompleted)
{
ArgumentNullException.ThrowIfNull(onCompleted);
switch (TransactionOf(context))
{
case IRedbTransaction explicitTransaction:
TransactionCompletion.Register(explicitTransaction, onCompleted);
return true;
case Transaction ambient:
ambient.TransactionCompleted += (_, e) =>
onCompleted(e.Transaction.TransactionInformation.Status == TransactionStatus.Committed);
return true;
default:
return false;
}
}
}
}