| | | 1 | | using Microsoft.EntityFrameworkCore.Infrastructure; |
| | | 2 | | using Microsoft.EntityFrameworkCore.Metadata; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 4 | | using Microsoft.EntityFrameworkCore.Metadata.Conventions; |
| | | 5 | | using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; |
| | | 6 | | using Microsoft.EntityFrameworkCore.Query; |
| | | 7 | | |
| | | 8 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds a model convention to one context: every root entity type with a soft-delete flag (<c>IDeletableEntity</c> or |
| | | 12 | | /// <c>[SoftDeleteFlag]</c>) gets the query filter <c>!flag</c>, combined with any filter the model already has. |
| | | 13 | | /// The model is built per internal service provider, so contexts without the extension keep an unfiltered model. |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class SoftDeleteFilterOptionsExtension : IDbContextOptionsExtension |
| | | 16 | | { |
| | | 17 | | private DbContextOptionsExtensionInfo? _info; |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | public DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo( this ); |
| | | 21 | | |
| | | 22 | | |
| | | 23 | | public void ApplyServices( IServiceCollection services ) => new EntityFrameworkServicesBuilder( services ).TryAdd<IC |
| | | 24 | | |
| | | 25 | | public void Validate( IDbContextOptions options ) { } |
| | | 26 | | |
| | | 27 | | |
| | | 28 | | private sealed class ExtensionInfo : DbContextOptionsExtensionInfo |
| | | 29 | | { |
| | | 30 | | public ExtensionInfo( IDbContextOptionsExtension extension ) : base( extension ) { } |
| | | 31 | | |
| | | 32 | | |
| | | 33 | | public override bool IsDatabaseProvider => false; |
| | | 34 | | |
| | | 35 | | public override string LogFragment => "using ZhEaIsNsAaBn soft-delete filter "; |
| | | 36 | | |
| | | 37 | | public override int GetServiceProviderHashCode() => 0; |
| | | 38 | | |
| | | 39 | | public override bool ShouldUseSameServiceProvider( DbContextOptionsExtensionInfo other ) => other is ExtensionIn |
| | | 40 | | |
| | | 41 | | public override void PopulateDebugInfo( IDictionary<string, string> debugInfo ) |
| | | 42 | | { |
| | | 43 | | ArgumentNullException.ThrowIfNull( debugInfo ); |
| | | 44 | | |
| | | 45 | | debugInfo[ "ZhEaIsNsAaBn:SoftDeleteFilter" ] = "1"; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | internal sealed class SoftDeleteConventionSetPlugin : IConventionSetPlugin |
| | | 51 | | { |
| | | 52 | | public ConventionSet ModifyConventions( ConventionSet conventionSet ) |
| | | 53 | | { |
| | | 54 | | ArgumentNullException.ThrowIfNull( conventionSet ); |
| | | 55 | | |
| | | 56 | | conventionSet.ModelFinalizingConventions.Add( new SoftDeleteQueryFilterConvention() ); |
| | | 57 | | |
| | | 58 | | return conventionSet; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | internal sealed class SoftDeleteQueryFilterConvention : IModelFinalizingConvention |
| | | 63 | | { |
| | | 64 | | #if NET10_0_OR_GREATER |
| | | 65 | | /// The named filter (EF 10): <c>IgnoreQueryFilters( [ SoftDeleteQueryFilterConvention.FilterKey ] )</c> ignores onl |
| | | 66 | | internal const string FilterKey = "ZhEaIsNsAaBn.SoftDelete"; |
| | | 67 | | #endif |
| | | 68 | | |
| | | 69 | | |
| | | 70 | | public void ProcessModelFinalizing( IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder |
| | | 71 | | { |
| | 4 | 72 | | ArgumentNullException.ThrowIfNull( modelBuilder ); |
| | | 73 | | |
| | 56 | 74 | | foreach ( var entityType in modelBuilder.Metadata.GetEntityTypes().Where( t => t.BaseType is null && !t.IsOwned( |
| | | 75 | | { |
| | 24 | 76 | | if ( AuditMap.For( entityType.ClrType ).SoftDeleteFlag is not { } flag || entityType.FindProperty( flag.Name |
| | | 77 | | |
| | | 78 | | |
| | 16 | 79 | | var entity = Expression.Parameter( entityType.ClrType, "e" ); |
| | 16 | 80 | | var filter = Expression.Lambda( Expression.Not( Expression.Property( entity, flag ) ), entity ); |
| | | 81 | | |
| | 16 | 82 | | Apply( entityType, filter ); |
| | | 83 | | } |
| | 4 | 84 | | } |
| | | 85 | | |
| | | 86 | | |
| | | 87 | | #if NET10_0_OR_GREATER |
| | | 88 | | private static void Apply( IConventionEntityType entityType, LambdaExpression filter ) |
| | | 89 | | { |
| | | 90 | | // EF 10 doesn't mix an unnamed filter with named ones: join an existing unnamed filter, else add ours by name. |
| | 8 | 91 | | if ( entityType.FindDeclaredQueryFilter( null ) is { Expression: { } existing } ) |
| | | 92 | | { |
| | 0 | 93 | | entityType.SetQueryFilter( And( existing, filter ) ); |
| | | 94 | | |
| | 0 | 95 | | return; |
| | | 96 | | } |
| | | 97 | | |
| | 8 | 98 | | entityType.SetQueryFilter( FilterKey, filter ); |
| | 8 | 99 | | } |
| | | 100 | | #else |
| | | 101 | | private static void Apply( IConventionEntityType entityType, LambdaExpression filter ) |
| | 8 | 102 | | => entityType.SetQueryFilter( entityType.GetQueryFilter() is { } existing ? And( existing, filter ) : filter ); |
| | | 103 | | #endif |
| | | 104 | | |
| | | 105 | | private static LambdaExpression And( LambdaExpression existing, LambdaExpression filter ) |
| | | 106 | | { |
| | 0 | 107 | | var body = ReplacingExpressionVisitor.Replace( filter.Parameters[ 0 ], existing.Parameters[ 0 ], filter.Body ); |
| | | 108 | | |
| | 0 | 109 | | return Expression.Lambda( Expression.AndAlso( existing.Body, body ), existing.Parameters ); |
| | | 110 | | } |
| | | 111 | | } |