Skip to content

Commit

Permalink
refactor: await eventBus.Publish methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tnotheis committed Dec 19, 2024
1 parent e50cc35 commit 2330fab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public async Task<T> RunInTransaction<T>(Func<Task<T>> func, IsolationLevel isol
return await RunInTransaction(func, null, isolationLevel);
}

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new())
public override async Task<int> 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;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ public override int SaveChanges()
{
var entities = GetChangedEntities();
var result = base.SaveChanges();
PublishDomainEvents(entities);
PublishDomainEvents(entities).GetAwaiter().GetResult();

return result;
}
Expand All @@ -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<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new())
public override async Task<int> 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;
}
Expand All @@ -153,11 +153,11 @@ private List<Entity> GetChangedEntities() => ChangeTracker
.Select(x => (Entity)x.Entity)
.ToList();

private void PublishDomainEvents(List<Entity> entities)
private async Task PublishDomainEvents(List<Entity> entities)
{
foreach (var e in entities)
{
_ = _eventBus.Publish(e.DomainEvents);
await _eventBus.Publish(e.DomainEvents);
e.ClearDomainEvents();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ public async Task<T> RunInTransaction<T>(Func<Task<T>> func, IsolationLevel isol
return await RunInTransaction(func, null, isolationLevel);
}

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new())
public override async Task<int> 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;
}
Expand Down Expand Up @@ -151,7 +151,7 @@ public override int SaveChanges()
{
var entities = GetChangedEntities();
var result = base.SaveChanges();
PublishDomainEvents(entities);
PublishDomainEvents(entities).GetAwaiter().GetResult();

return result;
}
Expand All @@ -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<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new())
public override async Task<int> 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;
}
Expand Down Expand Up @@ -240,11 +240,11 @@ private List<Entity> GetChangedEntities() => ChangeTracker
.Select(x => (Entity)x.Entity)
.ToList();

private void PublishDomainEvents(List<Entity> entities)
private async Task PublishDomainEvents(List<Entity> entities)
{
foreach (var e in entities)
{
_eventBus.Publish(e.DomainEvents);
await _eventBus.Publish(e.DomainEvents);
e.ClearDomainEvents();
}
}
Expand Down

0 comments on commit 2330fab

Please sign in to comment.