-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : Add soft deletion support and global query filters (#1051)
Updated `AuditableEntity` and `ISoftDeletable` to include `Deleted` and `DeletedBy` properties for soft deletion tracking. Modified `FshDbContext` to apply a global query filter for `ISoftDeletable` entities, ensuring deleted entities are excluded from queries. Enhanced `AuditInterceptor` to handle soft deletions, including setting `Deleted` and `DeletedBy` properties and updating entity states. Added `AppendGlobalQueryFilter` extension method to facilitate the application of global query filters to entities implementing specific interfaces. Co-authored-by: Mukesh Murugan <[email protected]>
- Loading branch information
1 parent
e7b5514
commit df50461
Showing
5 changed files
with
74 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
public interface ISoftDeletable | ||
{ | ||
|
||
DateTimeOffset? Deleted { get; set; } | ||
Guid? DeletedBy { get; set; } | ||
} |
36 changes: 36 additions & 0 deletions
36
src/api/framework/Infrastructure/Persistence/AppendGlobalQueryFilterExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Query; | ||
|
||
namespace FSH.Framework.Infrastructure.Persistence; | ||
|
||
internal static class ModelBuilderExtensions | ||
{ | ||
public static ModelBuilder AppendGlobalQueryFilter<TInterface>(this ModelBuilder modelBuilder, Expression<Func<TInterface, bool>> filter) | ||
{ | ||
// get a list of entities without a baseType that implement the interface TInterface | ||
var entities = modelBuilder.Model.GetEntityTypes() | ||
.Where(e => e.BaseType is null && e.ClrType.GetInterface(typeof(TInterface).Name) is not null) | ||
.Select(e => e.ClrType); | ||
|
||
foreach (var entity in entities) | ||
{ | ||
var parameterType = Expression.Parameter(modelBuilder.Entity(entity).Metadata.ClrType); | ||
var filterBody = ReplacingExpressionVisitor.Replace(filter.Parameters.Single(), parameterType, filter.Body); | ||
|
||
// get the existing query filter | ||
if (modelBuilder.Entity(entity).Metadata.GetQueryFilter() is { } existingFilter) | ||
{ | ||
var existingFilterBody = ReplacingExpressionVisitor.Replace(existingFilter.Parameters.Single(), parameterType, existingFilter.Body); | ||
|
||
// combine the existing query filter with the new query filter | ||
filterBody = Expression.AndAlso(existingFilterBody, filterBody); | ||
} | ||
|
||
// apply the new query filter | ||
modelBuilder.Entity(entity).HasQueryFilter(Expression.Lambda(filterBody, parameterType)); | ||
} | ||
|
||
return modelBuilder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters