添加serilog日志支持和部分其他efcore代码
This commit is contained in:
27
Infrastructure/MediatorExtension.cs
Normal file
27
Infrastructure/MediatorExtension.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Domain.SeedWork;
|
||||
using MediatR;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure
|
||||
{
|
||||
static class MediatorExtension
|
||||
{
|
||||
public static async Task DispatchDomainEventsAsync(this IMediator mediator, AppDbContext ctx)
|
||||
{
|
||||
var domainEntities = ctx.ChangeTracker
|
||||
.Entries<Entity>()
|
||||
.Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any());
|
||||
|
||||
var domainEvents = domainEntities
|
||||
.SelectMany(x => x.Entity.DomainEvents)
|
||||
.ToList();
|
||||
|
||||
domainEntities.ToList()
|
||||
.ForEach(entity => entity.Entity.ClearDomainEvents());
|
||||
|
||||
foreach (var domainEvent in domainEvents)
|
||||
await mediator.Publish(domainEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user