日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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

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

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

安裝 MediatR

在 ASP.Net Core 中使用 MediatR 非常簡(jiǎn)單,你只需要通過(guò) Nuget 安裝如下兩個(gè)包即可。

  • MediatR
  • MediatR.Extensions.Microsoft.DependencyInjection

當(dāng)前最新的版本為 9.0.0,如下圖所示:

 

配置 MediatR

一旦上面的兩個(gè) Nuget 包安裝到項(xiàng)目之后,接下來(lái)就可以在 Startup 類中進(jìn)行 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 模式

這篇文章我們將會(huì)討論 Notification,接下來(lái)創(chuàng)建一個(gè)實(shí)現(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)建一個(gè)實(shí)現(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)建了兩個(gè)處理 handler 類,接下來(lái)就可以通過(guò) 依賴注入 的方式將其注入到 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 中通過(guò) publish 發(fā)布消息,如下代碼所示:

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

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

 

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

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


網(wǎng)站名稱:如何在ASP.NetCore中使用MediatR
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/dhpjidg.html