-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
129590f
commit 1b87dcb
Showing
5 changed files
with
86 additions
and
52 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using EFCore.AuditBase.Interceptors; | ||
|
||
namespace EFCore.AuditBase.Extensions; | ||
|
||
public static class OptionsBuilderExtensions | ||
{ | ||
public static DbContextOptionsBuilder UseAuditBaseValidatorInterceptor(this DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.AddInterceptors(new AuditPropertyValidationInterceptor()); | ||
|
||
return optionsBuilder; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/EFCore.AuditBase/Interceptors/AuditPropertyValidationInterceptor.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,57 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace EFCore.AuditBase.Interceptors; | ||
|
||
internal class AuditPropertyValidationInterceptor : SaveChangesInterceptor | ||
{ | ||
public override InterceptionResult<int> SavingChanges(DbContextEventData eventData, | ||
InterceptionResult<int> result) | ||
{ | ||
if (eventData.Context is not null) | ||
{ | ||
ValidateAuditMethodUsage(eventData.Context); | ||
} | ||
|
||
return base.SavingChanges(eventData, result); | ||
} | ||
|
||
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, | ||
InterceptionResult<int> result, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (eventData.Context is not null) | ||
{ | ||
ValidateAuditMethodUsage(eventData.Context); | ||
} | ||
|
||
return base.SavingChangesAsync(eventData, result, cancellationToken); | ||
} | ||
|
||
private static void ValidateAuditMethodUsage(DbContext context) | ||
{ | ||
var entries = context.ChangeTracker | ||
.Entries<AuditEntityBase>() | ||
.Where(e => e.State == EntityState.Modified); | ||
|
||
foreach (var entry in entries) | ||
{ | ||
var entityName = entry.Entity.GetType() | ||
.Name; | ||
|
||
if (entry.State is not EntityState.Modified) | ||
{ | ||
continue; | ||
} | ||
|
||
var originalVersion = entry.OriginalValues[nameof(AuditEntityBase.Version)] as int?; | ||
var currentVersion = entry.CurrentValues[nameof(AuditEntityBase.Version)] as int?; | ||
|
||
if (originalVersion == currentVersion) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Entity '{entityName}' must be updated using MarkAsUpdated method. Missing or incorrect audit fields for update."); | ||
} | ||
} | ||
} | ||
} |