namespace Khaemhat.Data.Execution; /// /// What the data layer needs from the running application: the clock for audit timestamps, the current user for /// created_by/updated_by, and the retry policy. The web host builds it from ICurrentUser (M2). /// public sealed class DataAccessContext { private readonly Func _currentUser; /// Creates a context. /// The clock. /// Returns the current user name, or . /// The retry policy for transient errors outside transactions. public DataAccessContext(TimeProvider clock, Func currentUser, TransientRetryPolicy? retryPolicy = null) { ArgumentNullException.ThrowIfNull(clock); ArgumentNullException.ThrowIfNull(currentUser); Clock = clock; _currentUser = currentUser; RetryPolicy = retryPolicy ?? TransientRetryPolicy.Default; } /// System clock, no user, default retries. public static DataAccessContext Default { get; } = new(TimeProvider.System, static () => null); /// The clock. public TimeProvider Clock { get; } /// The retry policy. public TransientRetryPolicy RetryPolicy { get; } /// The current user name, or . public string? CurrentUser => _currentUser(); /// The current UTC time, truncated to microseconds (the precision of PostgreSQL and of SQLite storage). public DateTime UtcNow => Truncate(Clock.GetUtcNow().UtcDateTime); /// Truncates a timestamp to microseconds. /// The timestamp. /// The truncated timestamp. public static DateTime Truncate(DateTime value) => new(value.Ticks - (value.Ticks % 10), value.Kind); }