When publishing notifications, it is possible to pass MediatR an instance of INotificationPublisher
that controls the way the handlers are invoked:
_container = new Container(cfg =>
{
cfg.For<IMediator>().Add(s => new Mediator(s, new CustomNotificationPublisher()))
}
class CustomNotificationPublisher : INotificationPublisher
{
async Task INotificationPublisher.Publish(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification, CancellationToken cancellationToken)
{
foreach (var handler in handlerExecutors)
{
await handler.HandlerCallback(notification, cancellationToken).ConfigureAwait(false);
}
}
}
This it no special case: it’s the implementation of the composite we already saw in handling notifications without MediatR.
Answer
A MediatR Custom Notification Publisher
With the OOP approach: