// 摘自 MediatR 14.2.0,许可声明与修改记录见 LICENSE.md 和 UPSTREAM.md。 namespace MAX.Notification.Wrappers; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; public abstract class NotificationHandlerWrapper { public abstract Task Handle(INotification notification, IServiceProvider serviceFactory, Func, INotification, CancellationToken, Task> publish, CancellationToken cancellationToken); } public class NotificationHandlerWrapperImpl : NotificationHandlerWrapper where TNotification : INotification { public override Task Handle(INotification notification, IServiceProvider serviceFactory, Func, INotification, CancellationToken, Task> publish, CancellationToken cancellationToken) { var handlers = ((IEnumerable>?)serviceFactory.GetService(typeof(IEnumerable>)) ?? Array.Empty>()) .GroupBy(static x => x.GetType()) .Select(static g => g.First()) .Select(static x => new NotificationHandlerExecutor(x, (theNotification, theToken) => x.Handle((TNotification)theNotification, theToken))); return publish(handlers, notification, cancellationToken); } }