当前位置:主页 > 查看内容

如何在 ASP.Net Core 中使用 MediatR

发布时间:2021-05-10 00:00| 位朋友查看

简介:本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。 MediatR 是一个 中介者模式 的.NET开源实现, 中介者模式 管控了一组对象之间的相互通讯并有效的减少了对象之间错综复杂的相互依赖,在 中介者模式 中,一个对象不需要直接……

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号

MediatR 是一个 中介者模式 的.NET开源实现, 中介者模式 管控了一组对象之间的相互通讯并有效的减少了对象之间错综复杂的相互依赖,在 中介者模式 中,一个对象不需要直接和另一个对象进行通讯,而是通过 中介者 进行转达,这篇文章将会讨论如何在 ASP.Net Core 中使用 MediatR 。

安装 MediatR

在 ASP.Net Core 中使用 MediatR 非常简单,你只需要通过 Nuget 安装如下两个包即可。

  • MediatR
  • MediatR.Extensions.Microsoft.DependencyInjection

当前最新的版本为 9.0.0,如下图所示:

配置 MediatR

一旦上面的两个 Nuget 包安装到项目之后,接下来就可以在 Startup 类中进行 MediatR 的配置了,做法就是在 ConfigureServices() 方法中将 MediaR 注入到 IServiceCollection 容器中,如下代码所示:

  1. // This method gets called by the runtime. Use this method to add services to the container. 
  2.         public void ConfigureServices(IServiceCollection services) 
  3.         { 
  4.             services.AddMediatR(typeof(Startup)); 
  5.             services.AddControllers(); 
  6.         } 

使用 MediaR 处理 通知事件

MediatR 支持两种消息模式。

  • Request / Response 模式
  • Notification 模式

这篇文章我们将会讨论 Notification,接下来创建一个实现 INotification 接口的类,如下代码所示:

  1. public class LogEvent : INotification 
  2.     { 
  3.         public string message; 
  4.         public LogEvent(string message) 
  5.         { 
  6.             this.message = message; 
  7.         } 
  8.     } 

为了能够处理 LogEvent 事件,还需再创建一个实现 INotificationHandler 接口的类,如下代码所示:

  1. public class FileNotificationHandler : INotificationHandler<LogEvent> 
  2.   { 
  3.       public Task Handle(LogEvent notification, CancellationToken cancellationToken) 
  4.       { 
  5.           string message = notification.message; 
  6.  
  7.           Log(message); 
  8.  
  9.           return Task.FromResult(0); 
  10.       } 
  11.  
  12.       private void Log(string message) 
  13.       { 
  14.           //Write code here to log message(s) to a text file 
  15.           Debug.WriteLine("Write code here to log message(s) to a text file"); 
  16.       } 
  17.   } 
  18.  
  19.   public class DBNotificationHandler : INotificationHandler<LogEvent> 
  20.   { 
  21.       public Task Handle(LogEvent notification, CancellationToken cancellationToken) 
  22.       { 
  23.           string message = notification.message; 
  24.  
  25.           Log(message); 
  26.  
  27.           return Task.FromResult(0); 
  28.       } 
  29.       private void Log(string message) 
  30.       { 
  31.           //Write code here to log message(s) to the database 
  32.           Debug.WriteLine("Write code here to log message(s) to the database"); 
  33.       } 
  34.   } 

依赖注入 IMediator

刚才我已经为了 LogEvent 创建了两个处理 handler 类,接下来就可以通过 依赖注入 的方式将其注入到 Controller 中,如下代码所示:

  1. [ApiController] 
  2.     [Route("[controller]")] 
  3.     public class WeatherForecastController : ControllerBase 
  4.     { 
  5.         private readonly ILogger<WeatherForecastController> _logger; 
  6.         private readonly IMediator _mediator; 
  7.  
  8.         public WeatherForecastController(IMediator mediator, ILogger<WeatherForecastController> logger) 
  9.         { 
  10.             this._mediator = mediator; 
  11.             this._logger = logger; 
  12.         } 
  13.     } 

最后我们可以在 Action 中通过 publish 发布消息,如下代码所示:

  1. [HttpGet] 
  2.         public IEnumerable<WeatherForecast> Get() 
  3.         { 
  4.             _mediator.Publish(new LogEvent("Hello World")); 
  5.         } 

值得注意的是,执行程序后将会调用上面的 publish 方法,继而触发 DBNotificationHandler 和 FileNotificationHandler 的 Handle 方法,如下图所示:

中介者模式 是一种行为式的设计模式,它可以有效地管控多个对象之间的交互方式并有效的减少交互双方的依赖关系,刚好 MediatR 就是这样一款成品的 中介者模式 的实现,关于 MediatR 的 request/response 模式,我会在后面的文章中和大家细说。

译文链接:https://www.infoworld.com/article/3393974/how-to-use-mediatr-in-aspnet-core.html


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/fdqxrfOVBSbQDPUXfsYl1g
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐