日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何在ASP.NetCore中使用MediatR

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

十年的宜興網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整宜興建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“宜興網(wǎng)站設(shè)計”,“宜興網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

 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 
  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 
  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 _logger; 
  6.         private readonly IMediator _mediator; 
  7.  
  8.         public WeatherForecastController(IMediator mediator, ILogger logger) 
  9.         { 
  10.             this._mediator = mediator; 
  11.             this._logger = logger; 
  12.         } 
  13.     } 

最后我們可以在 Action 中通過 publish 發(fā)布消息,如下代碼所示:

 
 
 
 
  1. [HttpGet] 
  2.         public IEnumerable 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


分享文章:如何在ASP.NetCore中使用MediatR
標題URL:http://www.dlmjj.cn/article/dhpjidg.html