public interface IMediator
{
Task Publish(T notification, CancellationToken cancellationToken = default) where T : class, INotification;
}
public class Mediator : IMediator
{
private IServiceProvider _serviceProvider;
private static ConcurrentDictionary> _handle = new ConcurrentDictionary>();
public Mediator(
IServiceProvider serviceProvider
)
{
_serviceProvider = serviceProvider;
}
public Task Publish(T notification, CancellationToken cancellationToken = default) where T : class, INotification
{
notification.IsNull("notification is null");
var notificationType = notification.GetType();
var func = _handle.GetOrAdd(notificationType, type =>
{
Func handle = null;
var handles = _serviceProvider.GetServices(typeof(INotificationHandler));
foreach (var item in handles)
{
var generic = item.GetType().GetInterface($"{nameof(INotificationHandler)}`1").GetGenericArguments().First();
if (!generic.Name.Equals(type.Name)) continue;
if (item is INotificationHandler h )
{
handle += (x, y) => {
return h.Handle((T)x,y);
};
}
}
return handle;
});
if (func == null) throw new Exception(message: $"对应的 INotificationHandler<{notificationType.Name}>的实现没有注入");
return func((INotification)notification, cancellationToken);
}
}
public interface INotification
{
}
public interface INotificationHandler where T : INotification
{
Task Handle(T notification, CancellationToken cancellationToken=default(CancellationToken));
}
public static class CheckNull
{
public static void IsNull(this T t, string message="")
{
if (t == null)
{
throw new ArgumentNullException(nameof(t), message);
}
}
}
service.AddSingleton();
service.AddSingleton, MsgMonitorEventHandler>();
service.AddSingleton, MsgMonitorNoticeHandler>();
public class
MsgMonitorNoticeHandler:INotificationHandler
{
public async Task Handle(MsgMonitorEvent notification, CancellationToken cancellationToken = default)
{
Console.WriteLine("测试赛");
await Task.CompletedTask;
}
}
public class MsgMonitorEvent:INotification
{
public List> MonitorDtos { get; set; }
}
- 使用和MediatR完全一样,这样做的目的是不想代码中有太多依赖,可以在任意版本中切换重新编译可立即使用,2.1 3.1 5 6