Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,32 @@ protected virtual void OnBeforeSaveChanges()
{
if (Options != null)
{
foreach (var filter in Options.SaveChangesFilters)
OnBeforeSaveChangesByFilters();
DomainEventEnqueueAsync(ChangeTracker).ConfigureAwait(false).GetAwaiter().GetResult();
}
}

protected virtual async Task OnBeforeSaveChangesAsync()
{
if (Options != null)
{
OnBeforeSaveChangesByFilters();
await DomainEventEnqueueAsync(ChangeTracker);
}
}

protected virtual void OnBeforeSaveChangesByFilters()
{
foreach (var filter in Options.SaveChangesFilters)
{
try
{
try
{
filter.OnExecuting(ChangeTracker);
}
catch (Exception ex)
{
throw new Exception("An error occured when intercept SaveChanges() or SaveChangesAsync()", ex);
}
filter.OnExecuting(ChangeTracker);
}
catch (Exception ex)
{
throw new Exception("An error occured when intercept SaveChanges() or SaveChangesAsync()", ex);
}
DomainEventEnqueueAsync(ChangeTracker).ConfigureAwait(false).GetAwaiter().GetResult();
}
}

Expand Down Expand Up @@ -151,10 +165,10 @@ protected virtual async Task DomainEventEnqueueAsync(ChangeTracker changeTracker
/// <param name="acceptAllChangesOnSuccess"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public sealed override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
public sealed override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
OnBeforeSaveChanges();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
await OnBeforeSaveChangesAsync();
return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
}

Expand Down