namespace Toolkit.Foundation; public class NotificationHandlerWrapper(INotificationHandler handler, IEnumerable> pipelineBehaviours) where TNotification : INotification { private readonly IEnumerable> pipelineBehaviours = pipelineBehaviours.Reverse(); public async Task Handle(TNotification notification, CancellationToken cancellationToken) { NotificationHandlerDelegate currentHandler = handler.Handle; foreach (IPipelineBehavior behavior in pipelineBehaviours) { NotificationHandlerDelegate previousHandler = currentHandler; currentHandler = async (args, token) => { await behavior.Handle(args, previousHandler, token); }; } await currentHandler(notification, cancellationToken); } }