zoukankan      html  css  js  c++  java
  • MediatR

    1.MediatR是什么?

    微软官方eshopOnContainer开源项目中使用到了该工具,
    
    mediatR 是一种中介工具,解耦了消息处理器和消息之间耦合的类库,支持跨平台 .net Standard和.net framework
    
    https://github.com/jbogard/MediatR/wiki 这里是原文地址。其作者就是Automapper的作者。
    
    功能要是简述的话就俩方面:
    
    request/response 请求响应
    
    pub/sub 发布订阅

    2.使用

    nuget: install-package MediatR
    
    MediatR没有其他的依赖项,您需要配置一个工厂委托,用来实例化所有处理程序、管道的行为,和前/后处理器。

     

    3.Autofac完整的IOC注入示例:

    // uncomment to enable polymorphic dispatching of requests, but note that
    // this will conflict with generic pipeline behaviors
    // builder.RegisterSource(new ContravariantRegistrationSource());
    
    // mediator itself
    builder
      .RegisterType<Mediator>()
      .As<IMediator>()
      .InstancePerLifetimeScope();
    
    // request handlers
    builder
      .Register<SingleInstanceFactory>(ctx => {
        var c = ctx.Resolve<IComponentContext>();
        return t => c.TryResolve(t, out var o) ? o : null;
      })
      .InstancePerLifetimeScope();
    
    // notification handlers
    builder
      .Register<MultiInstanceFactory>(ctx => {
        var c = ctx.Resolve<IComponentContext>();
        return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
      })
      .InstancePerLifetimeScope();
    
    // finally register our custom code (individually, or via assembly scanning)
    // - requests & handlers as transient, i.e. InstancePerDependency()
    // - pre/post-processors as scoped/per-request, i.e. InstancePerLifetimeScope()
    // - behaviors as transient, i.e. InstancePerDependency()
    builder.RegisterAssemblyTypes(typeof(MyType).GetTypeInfo().Assembly).AsImplementedInterfaces(); // via assembly scan
    //builder.RegisterType<MyHandler>().AsImplementedInterfaces().InstancePerDependency(); // or individually

    4.ASP.NET CORE 使用 IOC注入:

    引用 MediatR nuget:install-package MediatR
    
    引用IOC扩展 nuget:installpackage MediatR.Extensions.Microsoft.DependencyInjection
    
    使用方式:
    
    services.AddMediatR(typeof(MyHandler));
    
    或
    
    services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);
    
    目的是为了扫描Handler的实现对象并添加到IOC的容器中

     

    5.参考示例

    5.1 请求响应(request/response),三步:

    步骤一:创建一个消息对象,需要实现IRequest,或IRequest<>接口,表明该对象是处理器的一个对象
    
    public class Ping : IRequest<string> { }
    
    步骤二:创建一个处理器对象
    
    public class PingHandler : IRequestHandler<Ping, string> { public Task<string> Handle(Ping request, CancellationToken cancellationToken) { return Task.FromResult("Pong"); } }
    
    步骤三:最后,通过mediator发送一个消息
    
    var response = await mediator.Send(new Ping()); Debug.WriteLine(response); // "Pong"

     

    说明:如果某些情况下,如果你的消息发送不需要返回响应结果的话,可以使用AsyncRequestHandler<TRequest>

    参考实现:

    public class OneWay : IRequest { } public class OneWayHandlerWithBaseClass : AsyncRequestHandler<OneWay> { protected override Task Handle(OneWay request, CancellationToken cancellationToken) { // Twiddle thumbs } }

     

    或者需要异步实现可以使用 RequestHandler 

    参考实现:

    public class SyncHandler : RequestHandler<Ping, string> { protected override string Handle(Ping request) { return "Pong"; } }

     

    5.1.1 Request的类型说明,比较幼稚了,,

    IRequest<T> 有返回值
    
    IRequest 无返回值
    
     
    
    IRequestHandler<T> 该对象的实现对象返回一个 Task 对象
    
    AsyncRequestHandler<T> 该对象的子对象(继承)返回一个 Task 对象
    
    RequestHandler<T> 该对象的子对象(继承) 无返回值
    
     
    
    IRequestHandler<T,U> 该对象的实现对象返回一个 Task<U> 对象
    
    RequestHandler<T,U> 该对象的子对象(继承)返回一个 U 对象

     

    5.2 Publishing,依旧三步走

    步骤一:创建一个用于通知的消息对象,实现INotification接口
    
    public class Ping : INotification { }
    
    步骤二:创建通知的处理器对象
    
    public class Pong1 : INotificationHandler<Ping> { 
      
      public Task Handle(Ping notification, CancellationToken cancellationToken) { Debug.WriteLine("Pong 1"); return Task.CompletedTask; } }

      public class Pong2 : INotificationHandler<Ping> { public Task Handle(Ping notification, CancellationToken cancellationToken) { Debug.WriteLine("Pong 2"); return Task.CompletedTask; } } 三步骤:最终使用mediator发布你的消息 await mediator.Publish(new Ping());

     

    5.3 其他:见github作者wiki参考示例

     

     

     

     

     

     

  • 相关阅读:
    静态与动态数据分析的测试方法
    网址无法访问,显示服务器拒绝了请求
    stf浏览器端删除离线设备
    STF日志提示 Not found ; no service started
    通过adb命令获取apk的安装路径
    js页面打开方式
    windows和mac回退及撤销快捷键
    单元测试规范
    Git :fatal: refusing to merge unrelated histories解决
    mysql详解4:分组
  • 原文地址:https://www.cnblogs.com/tanmingchao/p/9681975.html
Copyright © 2011-2022 走看看