From 2330fab3a4f82a463b92fdf72597e8413fa146e1 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Thu, 19 Dec 2024 17:16:08 +0100 Subject: [PATCH] refactor: await eventBus.Publish methods --- .../Database/AbstractDbContextBase.cs | 20 +++++++++---------- .../Persistence/Database/DevicesDbContext.cs | 20 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/BuildingBlocks/src/BuildingBlocks.Infrastructure/Persistence/Database/AbstractDbContextBase.cs b/BuildingBlocks/src/BuildingBlocks.Infrastructure/Persistence/Database/AbstractDbContextBase.cs index 92193f4594..df80e27575 100644 --- a/BuildingBlocks/src/BuildingBlocks.Infrastructure/Persistence/Database/AbstractDbContextBase.cs +++ b/BuildingBlocks/src/BuildingBlocks.Infrastructure/Persistence/Database/AbstractDbContextBase.cs @@ -84,11 +84,11 @@ public async Task RunInTransaction(Func> func, IsolationLevel isol return await RunInTransaction(func, null, isolationLevel); } - public override Task SaveChangesAsync(CancellationToken cancellationToken = new()) + public override async Task SaveChangesAsync(CancellationToken cancellationToken = new()) { var entities = GetChangedEntities(); - var result = base.SaveChangesAsync(cancellationToken); - PublishDomainEvents(entities); + var result = await base.SaveChangesAsync(cancellationToken); + await PublishDomainEvents(entities); return result; } @@ -124,7 +124,7 @@ public override int SaveChanges() { var entities = GetChangedEntities(); var result = base.SaveChanges(); - PublishDomainEvents(entities); + PublishDomainEvents(entities).GetAwaiter().GetResult(); return result; } @@ -133,16 +133,16 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess) { var entities = GetChangedEntities(); var result = base.SaveChanges(acceptAllChangesOnSuccess); - PublishDomainEvents(entities); + PublishDomainEvents(entities).GetAwaiter().GetResult(); return result; } - public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new()) + public override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new()) { var entities = GetChangedEntities(); - var result = base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); - PublishDomainEvents(entities); + var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + await PublishDomainEvents(entities); return result; } @@ -153,11 +153,11 @@ private List GetChangedEntities() => ChangeTracker .Select(x => (Entity)x.Entity) .ToList(); - private void PublishDomainEvents(List entities) + private async Task PublishDomainEvents(List entities) { foreach (var e in entities) { - _ = _eventBus.Publish(e.DomainEvents); + await _eventBus.Publish(e.DomainEvents); e.ClearDomainEvents(); } } diff --git a/Modules/Devices/src/Devices.Infrastructure/Persistence/Database/DevicesDbContext.cs b/Modules/Devices/src/Devices.Infrastructure/Persistence/Database/DevicesDbContext.cs index 07868d07db..be4b996d77 100644 --- a/Modules/Devices/src/Devices.Infrastructure/Persistence/Database/DevicesDbContext.cs +++ b/Modules/Devices/src/Devices.Infrastructure/Persistence/Database/DevicesDbContext.cs @@ -112,11 +112,11 @@ public async Task RunInTransaction(Func> func, IsolationLevel isol return await RunInTransaction(func, null, isolationLevel); } - public override Task SaveChangesAsync(CancellationToken cancellationToken = new()) + public override async Task SaveChangesAsync(CancellationToken cancellationToken = new()) { var entities = GetChangedEntities(); - var result = base.SaveChangesAsync(cancellationToken); - PublishDomainEvents(entities); + var result = await base.SaveChangesAsync(cancellationToken); + await PublishDomainEvents(entities); return result; } @@ -151,7 +151,7 @@ public override int SaveChanges() { var entities = GetChangedEntities(); var result = base.SaveChanges(); - PublishDomainEvents(entities); + PublishDomainEvents(entities).GetAwaiter().GetResult(); return result; } @@ -160,16 +160,16 @@ public override int SaveChanges(bool acceptAllChangesOnSuccess) { var entities = GetChangedEntities(); var result = base.SaveChanges(acceptAllChangesOnSuccess); - PublishDomainEvents(entities); + PublishDomainEvents(entities).GetAwaiter().GetResult(); return result; } - public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new()) + public override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new()) { var entities = GetChangedEntities(); - var result = base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); - PublishDomainEvents(entities); + var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + await PublishDomainEvents(entities); return result; } @@ -240,11 +240,11 @@ private List GetChangedEntities() => ChangeTracker .Select(x => (Entity)x.Entity) .ToList(); - private void PublishDomainEvents(List entities) + private async Task PublishDomainEvents(List entities) { foreach (var e in entities) { - _eventBus.Publish(e.DomainEvents); + await _eventBus.Publish(e.DomainEvents); e.ClearDomainEvents(); } }