Dispatching Domain Events right before DbContext SaveChanges() so side effects from additional Domain Event Handlers are included within the same transaction
This commit is contained in:
parent
a8733ac271
commit
cb4da9864d
@ -237,10 +237,18 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure
|
|||||||
|
|
||||||
public async Task<int> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
|
public async Task<int> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
|
// Dispatch Domain Events collection.
|
||||||
|
// Choices:
|
||||||
|
// A) Right BEFORE committing data (EF SaveChanges) into the DB will make a single transaction including
|
||||||
|
// side effects from the domain event handlers which are using the same DbContext with "InstancePerLifetimeScope" or "scoped" lifetime
|
||||||
|
// B) Right AFTER committing data (EF SaveChanges) into the DB. will make multiple transactions.
|
||||||
|
// You will need to handle eventual consistency and compensatory actions in case of failures.
|
||||||
|
await _mediator.DispatchDomainEventsAsync(this);
|
||||||
|
|
||||||
|
|
||||||
|
// After executing this line all the changes performed thought the DbContext will be commited
|
||||||
var result = await base.SaveChangesAsync();
|
var result = await base.SaveChangesAsync();
|
||||||
|
|
||||||
// Dispatch the Domain Events collection right after saving/committing data into the database
|
|
||||||
await _mediator.DispatchDomainEventsAsync(this);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user