32 lines
1.4 KiB
C#
32 lines
1.4 KiB
C#
// 摘自 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<IEnumerable<NotificationHandlerExecutor>, INotification, CancellationToken, Task> publish,
|
|
CancellationToken cancellationToken);
|
|
}
|
|
|
|
public class NotificationHandlerWrapperImpl<TNotification> : NotificationHandlerWrapper
|
|
where TNotification : INotification
|
|
{
|
|
public override Task Handle(INotification notification, IServiceProvider serviceFactory,
|
|
Func<IEnumerable<NotificationHandlerExecutor>, INotification, CancellationToken, Task> publish,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var handlers = ((IEnumerable<INotificationHandler<TNotification>>?)serviceFactory.GetService(typeof(IEnumerable<INotificationHandler<TNotification>>)) ?? Array.Empty<INotificationHandler<TNotification>>())
|
|
.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);
|
|
}
|
|
} |