网站建设子栏目怎么弄,网站后台模板 php,广告平台网站有哪些,asp网站如何运行MediatR[1] 是参考中介者模式实现的一个 .NET 工具类库#xff0c;支持在进程内以单播或多播的形式进行消息传递#xff0c;通过使用 MediatR 可实现消息的发送和处理充分解耦。在介绍 MediatR 之前#xff0c;先简单了解下中介者模式。中介者模式主要是指定义一个中介对象来… MediatR[1] 是参考中介者模式实现的一个 .NET 工具类库支持在进程内以单播或多播的形式进行消息传递通过使用 MediatR 可实现消息的发送和处理充分解耦。在介绍 MediatR 之前先简单了解下中介者模式。中介者模式主要是指定义一个中介对象来调度一系列对象之间的交互关系各对象之间不需要显式的相互引用降低耦合性。如下对比图普通模式与中介者模式的区别实际上从 MediatR 源代码中可以看出它本身也并非标准中介者模式的实现所以这里简单了解即可。接下来将先介绍 MediatR 的两种消息传递方式的使用方式然后再分析其具体实现。创建一个 .NET Core Web API 项目并安装 MediatR.Extensions.Microsoft.DependencyInjection NuGet 包已含 MediatR NuGet 包然后在 ConfigureServices 中注册服务。// 扫描 Startup 所在程序集内实现了 Handler 的对象并添加到 IoC 容器中
services.AddMediatR(typeof(Startup));
可通过查看 MediatR.Extensions.Microsoft.DependencyInjection[2] 说明了解 AddMediatR 具体包含了哪些服务的注册以及各注册对象的生命周期基本通过以上一行代码就已经把 MediatR 相关的服务全部注册到 IoC 容器中。单播消息传递单播消息传递主要涉及 IRequest消息类型 和 IRequestHandler消息处理 两个接口。定义接口 IRequest 的实现类string 指定消息处理方法的返回值类型如下public class GenericRequest : IRequeststring
{public string Name { get; set; }
}
定义接口 IRequestHandler 的实现类GenericRequest 指定此 Handler 要处理的消息类型string 指定消息处理方法的返回值类型与 IRequest 指定的泛型类型一致另外需实现 Handle 方法如下public class GenericRequestHandler : IRequestHandlerGenericRequest, string
{public Taskstring Handle(GenericRequest request, CancellationToken cancellationToken){return Task.FromResult($This is {request.Name});}
}
在 Controller 中进行调用测试private readonly IMediator _mediator;public MediatorController(IMediator mediator)
{_mediator mediator;
}[HttpGet]
public async Taskstring GenericRequest()
{var result await _mediator.Send(new GenericRequest{Name GenericRequest});return result;
}
另外针对不同的代码实现方式有其他的 request-types[3] 可选本质上还是基于 IRequest 和 IRequestHandler 的扩展。多播消息传递多播消息传递主要涉及 INotification消息类型 和 INotificationHandler消息处理 两个接口另外多播消息传递是无返回值的。定义接口 INotification 的实现类如下public class GenericNotification : INotification
{public string Name { get; set; }
}
定义接口 INotificationHandler 的实现类GenericNotification 指定此 Handler 要处理的消息类型另外需实现 Handle 方法这里将为此消息类型定义两个 NotificationHandler 实现类如下public class GenericANotificationHandler : INotificationHandlerGenericNotification
{public Task Handle(GenericNotification notification, CancellationToken cancellationToken){Console.WriteLine($A {notification.Name});return Task.CompletedTask;}
}
public class GenericBNotificationHandler : INotificationHandlerGenericNotification
{public Task Handle(GenericNotification notification, CancellationToken cancellationToken){Console.WriteLine($B {notification.Name});return Task.CompletedTask;}
}
在 Controller 中进行调用测试[HttpGet]
public async Task GenericNotification()
{await _mediator.Publish(new GenericNotification{Name GenericNotification});
}
原理分析建议阅读下源码代码量少且结构清晰基本理解没什么难度通过前面的介绍可以了解在 MediatR 中面向开发者的核心接口主要是 IRequestIRequestHandler 、INotificationINotificationHandler、IMediator 。如下 IMediator 的实现类 Mediator 中的定义public class Mediator : IMediator
{private readonly ServiceFactory _serviceFactory;private static readonly ConcurrentDictionaryType, object _requestHandlers new ConcurrentDictionaryType, object();private static readonly ConcurrentDictionaryType, NotificationHandlerWrapper _notificationHandlers new ConcurrentDictionaryType, NotificationHandlerWrapper();
}
首先定义了 ServiceFactory 对象它代表当前应用程序的 IoC 容器在应用初始化阶段进行了注入如 MediatR.Extensions.Microsoft.DependencyInjection 已包含了对应的 ServiceFactory 注册[4]。由于 ServiceFactory 可自定义所以开发中也完全可以选择其他的含 IoC 容器功能的框架如 Autofac、Castle Windsor、DryIoc 等。另外定义 _requestHandlers 和 _notificationHandlers 分别保存单播和多播消息对象类型对应的 HandlerWrapper 对象HandlerWrapper 的主要是对 ServiceFactory 对象的传递最终通过 ServiceFactory 从 IoC 容器中获取对应消息类型的 Handler 对象。MeidatR 还支持为单播消息定义消息处理的 Pipeline如通过实现 IRequestPreProcessor 、IRequestPostProcessor 在消息处理前后自定义处理行为通过实现 IRequestExceptionHandler、IRequestExceptionAction 在异常时自定义处理行为这些实现类也是通过 ServiceFactory 从 IoC 容器中获取。以下是单播消息处理的核心代码public override TaskTResponse Handle(IRequestTResponse request, CancellationToken cancellationToken, ServiceFactory serviceFactory)
{TaskTResponse Handler() GetHandlerIRequestHandlerTRequest, TResponse(serviceFactory).Handle((TRequest) request, cancellationToken);return serviceFactory.GetInstancesIPipelineBehaviorTRequest, TResponse().Reverse().Aggregate((RequestHandlerDelegateTResponse) Handler, (next, pipeline) () pipeline.Handle((TRequest)request, cancellationToken, next))();
}
首先从 ServiceFactory 获取 IPipelineBehavior然后通 Linq 的 Reverse 方法进行顺序颠倒最后通过 Aggregate 进行委托传递并执行所以最终执行顺序是 RequestPreProcessorBehavior → Handler → RequestPostProcessorBehavior这里的实现可能较难理解核心是 Aggregate 的使用。总结MediatR 在实现上核心是通过保存消息请求对象与消息处理对象的关系配合 IoC 容器实现的消息传递解耦。在实际应用中通过 MediatR 多播消息传递可以使代码实现逻辑上更加简洁另外也有较多的文章介绍了通过 MediatR 实现 CQRS、EventBus 等。参考资料[1]MediatR: https://github.com/jbogard/MediatR[2]MediatR.Extensions.Microsoft.DependencyInjection: https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection[3]request-types: https://github.com/jbogard/MediatR/wiki#request-types[4]ServiceFactory 注册: https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection/blob/master/src/MediatR.Extensions.Microsoft.DependencyInjection/Registration/ServiceRegistrar.cs#L219