| | | 1 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 2 | | |
| | | 3 | | /// <summary>Settings for <see cref="AuditInterceptor{TId}"/> and the audited bulk operations.</summary> |
| | | 4 | | /// <typeparam name="TId">The user key type.</typeparam> |
| | | 5 | | public sealed class AuditOptions< TId > |
| | | 6 | | where TId : struct, IEquatable<TId> |
| | | 7 | | { |
| | 24 | 8 | | private readonly HashSet<Type> _hardDelete = []; |
| | 24 | 9 | | private TimeProvider _timeProvider = TimeProvider.System; |
| | | 10 | | |
| | | 11 | | |
| | | 12 | | /// <summary>The clock; audit times are <c>TimeProvider.GetLocalNow().DateTime</c>. Default <see cref="TimeProvider. |
| | | 13 | | /// <exception cref="ArgumentNullException">The value is <see langword="null"/>.</exception> |
| | | 14 | | public TimeProvider TimeProvider |
| | | 15 | | { |
| | 184 | 16 | | get => _timeProvider; |
| | | 17 | | set |
| | | 18 | | { |
| | 4 | 19 | | ArgumentNullException.ThrowIfNull( value ); |
| | | 20 | | |
| | 4 | 21 | | _timeProvider = value; |
| | 4 | 22 | | } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | internal TId? SystemUserId { get; private set; } |
| | | 26 | | internal string? SystemUserName { get; private set; } |
| | | 27 | | |
| | | 28 | | |
| | | 29 | | /// <summary>The user stamped when <see cref="ICurrentUserProvider{TId}"/> has no user (or none is registered). Requ |
| | | 30 | | /// <param name="id">The system user's key.</param> |
| | | 31 | | /// <param name="name">The system user's name.</param> |
| | | 32 | | /// <returns>These options.</returns> |
| | | 33 | | /// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, empty or white space.</ex |
| | | 34 | | public AuditOptions<TId> SystemUser( TId id, string name ) |
| | | 35 | | { |
| | 20 | 36 | | ArgumentException.ThrowIfNullOrWhiteSpace( name ); |
| | | 37 | | |
| | 16 | 38 | | SystemUserId = id; |
| | 16 | 39 | | SystemUserName = name; |
| | | 40 | | |
| | 16 | 41 | | return this; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Deletes <typeparamref name="TEntity"/> (and types derived from it) for real even when it has a soft-delete flag |
| | | 46 | | /// (<c>IDeletableEntity</c> or <c>[SoftDeleteFlag]</c>). |
| | | 47 | | /// </summary> |
| | | 48 | | /// <typeparam name="TEntity">The entity type.</typeparam> |
| | | 49 | | /// <returns>These options.</returns> |
| | | 50 | | public AuditOptions<TId> HardDelete< TEntity >() |
| | | 51 | | where TEntity : class |
| | | 52 | | { |
| | 8 | 53 | | _hardDelete.Add( typeof( TEntity ) ); |
| | | 54 | | |
| | 8 | 55 | | return this; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | |
| | 48 | 59 | | internal bool IsHardDelete( Type entityType ) => _hardDelete.Any( type => type.IsAssignableFrom( entityType ) ); |
| | | 60 | | |
| | | 61 | | internal void Validate() |
| | | 62 | | { |
| | 32 | 63 | | if ( SystemUserId is null || SystemUserName is null ) |
| | | 64 | | { |
| | 4 | 65 | | throw new InvalidOperationException( "Auditing needs a system user for saves without a current user: call o. |
| | | 66 | | } |
| | 28 | 67 | | } |
| | | 68 | | } |