添加项目文件。
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user