日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

如何在 ASP.Net Core 中使用 MediatR

發布時間:2023/12/4 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在 ASP.Net Core 中使用 MediatR 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 容器中,如下代碼所示:

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

使用 MediaR 處理 通知事件

MediatR 支持兩種消息模式。

  • Request / Response 模式

  • Notification 模式

這篇文章我們將會討論 Notification,接下來創建一個實現 INotification 接口的類,如下代碼所示:

public?class?LogEvent?:?INotification{public?string?message;public?LogEvent(string?message){this.message?=?message;}}

為了能夠處理 LogEvent 事件,還需再創建一個實現 INotificationHandler 接口的類,如下代碼所示:

public?class?FileNotificationHandler?:?INotificationHandler<LogEvent>{public?Task?Handle(LogEvent?notification,?CancellationToken?cancellationToken){string?message?=?notification.message;Log(message);return?Task.FromResult(0);}private?void?Log(string?message){//Write?code?here?to?log?message(s)?to?a?text?fileDebug.WriteLine("Write?code?here?to?log?message(s)?to?a?text?file");}}public?class?DBNotificationHandler?:?INotificationHandler<LogEvent>{public?Task?Handle(LogEvent?notification,?CancellationToken?cancellationToken){string?message?=?notification.message;Log(message);return?Task.FromResult(0);}private?void?Log(string?message){//Write?code?here?to?log?message(s)?to?the?databaseDebug.WriteLine("Write?code?here?to?log?message(s)?to?the?database");}}

依賴注入 IMediator

剛才我已經為了 LogEvent 創建了兩個處理 handler 類,接下來就可以通過 依賴注入 的方式將其注入到 Controller 中,如下代碼所示:

[ApiController][Route("[controller]")]public?class?WeatherForecastController?:?ControllerBase{private?readonly?ILogger<WeatherForecastController>?_logger;private?readonly?IMediator?_mediator;public?WeatherForecastController(IMediator?mediator,?ILogger<WeatherForecastController>?logger){this._mediator?=?mediator;this._logger?=?logger;}}

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

[HttpGet]public?IEnumerable<WeatherForecast>?Get(){_mediator.Publish(new?LogEvent("Hello?World"));}

值得注意的是,執行程序后將會調用上面的 publish 方法,繼而觸發 DBNotificationHandler 和 FileNotificationHandler 的 Handle 方法,如下圖所示:

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

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

總結

以上是生活随笔為你收集整理的如何在 ASP.Net Core 中使用 MediatR的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。