36 lines
No EOL
1,017 B
C#
36 lines
No EOL
1,017 B
C#
using Femto.Common.Domain;
|
|
using Femto.Modules.Media.Data;
|
|
using MediatR;
|
|
|
|
namespace Femto.Modules.Media.Infrastructure.PipelineBehaviours;
|
|
|
|
internal class DomainEventsPipelineBehaviour<TRequest, TResponse>(
|
|
MediaContext context,
|
|
IPublisher publisher) : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : notnull
|
|
{
|
|
public async Task<TResponse> Handle(
|
|
TRequest request,
|
|
RequestHandlerDelegate<TResponse> next,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var response = await next(cancellationToken);
|
|
|
|
var domainEvents = context.ChangeTracker
|
|
.Entries<Entity>()
|
|
.SelectMany(e =>
|
|
{
|
|
var events = e.Entity.DomainEvents;
|
|
e.Entity.ClearDomainEvents();
|
|
return events;
|
|
})
|
|
.ToList();
|
|
|
|
foreach (var domainEvent in domainEvents)
|
|
{
|
|
await publisher.Publish(domainEvent, cancellationToken);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
} |