2017-03-14 18:02:28 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Ordering.Infrastructure
|
|
|
|
|
{
|
2017-04-17 12:28:12 +02:00
|
|
|
|
static class MediatorExtension
|
2017-03-14 18:02:28 +01:00
|
|
|
|
{
|
2017-03-17 18:36:34 -07:00
|
|
|
|
public static async Task DispatchDomainEventsAsync(this IMediator mediator, OrderingContext ctx)
|
2017-03-14 18:02:28 +01:00
|
|
|
|
{
|
2017-04-17 12:28:12 +02:00
|
|
|
|
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.DomainEvents.Clear());
|
2017-03-14 18:02:28 +01:00
|
|
|
|
|
|
|
|
|
var tasks = domainEvents
|
|
|
|
|
.Select(async (domainEvent) => {
|
|
|
|
|
await mediator.PublishAsync(domainEvent);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|