31 lines
844 B
C#
31 lines
844 B
C#
using IM.DomainCommons;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace IM.Infrastructure.Efcore
|
|
{
|
|
public static class MediatorExtensions
|
|
{
|
|
public static async Task DispatchDomainEventsAsync(this IMediator mediator, DbContext db)
|
|
{
|
|
var domainEntities = db.ChangeTracker
|
|
.Entries<IDomainEvents>()
|
|
.Where(x => x.Entity.GetDomainEvents().Any());
|
|
|
|
var domainEvents = domainEntities
|
|
.SelectMany(s => s.Entity.GetDomainEvents())
|
|
.ToList();
|
|
|
|
domainEntities.ToList().ForEach(e =>
|
|
{
|
|
e.Entity.ClearDomainEvents();
|
|
});
|
|
|
|
foreach (var domainEvent in domainEvents)
|
|
{
|
|
await mediator.Publish(domainEvent);
|
|
}
|
|
}
|
|
}
|
|
}
|