From 363e2be11cb26962d94cc36a0aa871f5a2863d6f Mon Sep 17 00:00:00 2001 From: zhenlei520 Date: Wed, 18 May 2022 14:50:20 +0800 Subject: [PATCH 1/2] chore: Optimize savechange filter --- src/BuildingBlocks/MASA.BuildingBlocks | 2 +- .../MasaDbContext.cs | 34 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/BuildingBlocks/MASA.BuildingBlocks b/src/BuildingBlocks/MASA.BuildingBlocks index 44285eb4f..c29423b13 160000 --- a/src/BuildingBlocks/MASA.BuildingBlocks +++ b/src/BuildingBlocks/MASA.BuildingBlocks @@ -1 +1 @@ -Subproject commit 44285eb4fcb9603c839d1b159824245fd0fbd80b +Subproject commit c29423b13379a7e69c1d0f31980f03feae3ac4c9 diff --git a/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs b/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs index 4ec61b4aa..55df35d0c 100644 --- a/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs +++ b/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs @@ -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(); } } From 6f24cf89e263e90856d68ca47984e57432d1c113 Mon Sep 17 00:00:00 2001 From: zhenlei520 Date: Wed, 18 May 2022 15:16:07 +0800 Subject: [PATCH 2/2] refactor(EntityFramework): Refactor SaveChangeAsync --- .../Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs b/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs index 55df35d0c..40192da15 100644 --- a/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs +++ b/src/Data/Masa.Contrib.Data.EntityFrameworkCore/MasaDbContext.cs @@ -165,10 +165,10 @@ protected virtual async Task DomainEventEnqueueAsync(ChangeTracker changeTracker /// /// /// - public sealed override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) + public sealed override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { - OnBeforeSaveChanges(); - return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + await OnBeforeSaveChangesAsync(); + return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } }