namespace Toolkit.Foundation; public class NotificationHandlerWrapper(INotificationHandler handler, IEnumerable> pipelineBehaviours) { private readonly IEnumerable> pipelineBehaviours = pipelineBehaviours.Reverse(); public async Task Handle(TMessage message, CancellationToken cancellationToken) { NotificationHandlerDelegate currentHandler = handler.Handle; foreach (IPipelineBehaviour behaviour in pipelineBehaviours) { NotificationHandlerDelegate previousHandler = currentHandler; currentHandler = async (args, token) => { await behaviour.Handle(args, previousHandler, token); }; } await currentHandler(message, cancellationToken); } }