自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 ASP.Net Core 中使用 MediatR

開發(fā) 前端
MediatR 是一個 中介者模式 的.NET開源實現(xiàn), 中介者模式 管控了一組對象之間的相互通訊并有效的減少了對象之間錯綜復(fù)雜的相互依賴,在 中介者模式 中,一個對象不需要直接和另一個對象進行通訊,而是通過 中介者 進行轉(zhuǎn)達,這篇文章將會討論如何在 ASP.Net Core 中使用 MediatR 。

[[385016]]

本文轉(zhuǎn)載自微信公眾號「碼農(nóng)讀書」,作者碼農(nóng)讀書。轉(zhuǎn)載本文請聯(lián)系碼農(nóng)讀書公眾號。

 MediatR 是一個 中介者模式 的.NET開源實現(xiàn), 中介者模式 管控了一組對象之間的相互通訊并有效的減少了對象之間錯綜復(fù)雜的相互依賴,在 中介者模式 中,一個對象不需要直接和另一個對象進行通訊,而是通過 中介者 進行轉(zhuǎn)達,這篇文章將會討論如何在 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,接下來創(chuàng)建一個實現(xiàn) INotification 接口的類,如下代碼所示:

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

為了能夠處理 LogEvent 事件,還需再創(chuàng)建一個實現(xiàn) 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

剛才我已經(jīng)為了 LogEvent 創(chuàng)建了兩個處理 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 發(fā)布消息,如下代碼所示:

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

值得注意的是,執(zhí)行程序后將會調(diào)用上面的 publish 方法,繼而觸發(fā) DBNotificationHandler 和 FileNotificationHandler 的 Handle 方法,如下圖所示:

 

中介者模式 是一種行為式的設(shè)計模式,它可以有效地管控多個對象之間的交互方式并有效的減少交互雙方的依賴關(guān)系,剛好 MediatR 就是這樣一款成品的 中介者模式 的實現(xiàn),關(guān)于 MediatR 的 request/response 模式,我會在后面的文章中和大家細說。

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

責任編輯:武曉燕 來源: 碼農(nóng)讀書
相關(guān)推薦

2021-03-17 09:45:31

LazyCacheWindows

2021-02-02 16:19:08

Serilog日志框架

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-01-31 22:56:50

FromServiceASP

2021-02-03 13:35:25

ASPweb程序

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache緩存框架

2021-01-28 22:39:35

LoggerMessa開源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-02-07 17:29:04

監(jiān)視文件接口

2021-06-22 16:59:56

微軟.NETC# 軟件開發(fā)

2021-01-26 14:57:00

中間件應(yīng)用模塊化

2021-04-12 07:03:10

輕量級模塊化框架

2021-01-04 05:44:54

框架日志

2017-10-20 08:52:11

內(nèi)存緩存并發(fā)模式Linux

2022-08-01 08:00:00

開發(fā)工具跟蹤偵聽器

2009-02-05 14:02:46

SmtpMail發(fā)送郵件ASP.NET

2021-04-14 07:35:12

Json格式化日期

2021-11-01 14:52:38

ElasticSear索引SQL

2009-03-30 10:34:03

ASP.NETMySQL
點贊
收藏

51CTO技術(shù)棧公眾號