| | | 1 | | using Microsoft.EntityFrameworkCore.Infrastructure; |
| | | 2 | | using ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Microsoft.EntityFrameworkCore; |
| | | 6 | | |
| | | 7 | | /// <summary>Auditing and the soft-delete filter for contexts registered without the Core builder.</summary> |
| | | 8 | | public static class AuditingOptionsExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds an <see cref="AuditInterceptor{TId}"/> to this context. With the Core builder use <c>WithAuditing<TId> |
| | | 12 | | /// which takes the user provider from DI. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="TId">The user key type.</typeparam> |
| | | 15 | | /// <param name="optionsBuilder">The context options builder.</param> |
| | | 16 | | /// <param name="configure">Sets the system user (required), hard-delete types and the clock.</param> |
| | | 17 | | /// <param name="currentUser">The current user (singleton / ambient); <see langword="null"/> always stamps the syste |
| | | 18 | | /// <returns>The same builder.</returns> |
| | | 19 | | /// <exception cref="ArgumentNullException"><paramref name="optionsBuilder"/> or <paramref name="configure"/> is <se |
| | | 20 | | /// <exception cref="InvalidOperationException"><paramref name="configure"/> didn't set a system user.</exception> |
| | | 21 | | /// <example><code>options.UseSqlServer( cs ).UseAuditing<int>( o => o.SystemUser( 1, "system" ), currentUs |
| | | 22 | | public static DbContextOptionsBuilder UseAuditing< TId >( this DbContextOptionsBuilder optionsBuilder, Action<AuditO |
| | | 23 | | where TId : struct, IEquatable<TId> |
| | | 24 | | { |
| | 0 | 25 | | ArgumentNullException.ThrowIfNull( optionsBuilder ); |
| | | 26 | | |
| | 0 | 27 | | return optionsBuilder.AddInterceptors( new AuditInterceptor<TId>( AuditOptionsFrom( configure ), currentUser ) ) |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Hides soft-deleted rows: every root entity with a soft-delete flag (<c>IDeletableEntity</c> or <c>[SoftDeleteFla |
| | | 32 | | /// filter <c>!flag</c>, joined with any filter it already has. <c>IgnoreQueryFilters()</c> shows them again. With t |
| | | 33 | | /// <c>WithSoftDeleteFilter()</c>. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="optionsBuilder">The context options builder.</param> |
| | | 36 | | /// <returns>The same builder.</returns> |
| | | 37 | | /// <exception cref="ArgumentNullException"><paramref name="optionsBuilder"/> is <see langword="null"/>.</exception> |
| | | 38 | | public static DbContextOptionsBuilder UseSoftDeleteFilter( this DbContextOptionsBuilder optionsBuilder ) |
| | | 39 | | { |
| | 4 | 40 | | ArgumentNullException.ThrowIfNull( optionsBuilder ); |
| | | 41 | | |
| | 4 | 42 | | var extension = optionsBuilder.Options.FindExtension<SoftDeleteFilterOptionsExtension>() ?? new SoftDeleteFilter |
| | | 43 | | |
| | 4 | 44 | | ( (IDbContextOptionsBuilderInfrastructure) optionsBuilder ).AddOrUpdateExtension( extension ); |
| | | 45 | | |
| | | 46 | | |
| | 4 | 47 | | return optionsBuilder; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | |
| | | 51 | | /// <summary>Runs <paramref name="configure"/> on new options and checks them.</summary> |
| | | 52 | | internal static AuditOptions<TId> AuditOptionsFrom< TId >( Action<AuditOptions<TId>> configure ) |
| | | 53 | | where TId : struct, IEquatable<TId> |
| | | 54 | | { |
| | 20 | 55 | | ArgumentNullException.ThrowIfNull( configure ); |
| | | 56 | | |
| | 20 | 57 | | var options = new AuditOptions<TId>(); |
| | | 58 | | |
| | 20 | 59 | | configure( options ); |
| | 20 | 60 | | options.Validate(); |
| | | 61 | | |
| | 16 | 62 | | return options; |
| | | 63 | | } |
| | | 64 | | } |