您现在的位置是:亿华云 > 数据库
如何在 ASP.Net Core 中使用 MediatR
亿华云2025-10-04 03:29:13【数据库】0人已围观
简介本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。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 容器中,如下代码所示:
// 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 file Debug.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 database Debug.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
很赞哦!(35)
相关文章
- 4、域名传输时,取决于域名原始用户的邮箱是否有效,以及他是否将密码发送到此邮箱。
- Postman 是个好用的工具,不试一下?
- Deno :它比 Node.js 更好吗?
- 阿里云RDS受邀加入WebScaleSQL 成全球第五家公司成员
- 域名不仅仅是一个简单的网站。对于有长远眼光的公司来说,在运营网站之前确定一个优秀的域名对有长远眼光的公司来说是非常重要的。这对今后的市场营销、产品营销和企业品牌建设都具有十分重要的意义。优秀的域名是企业在市场竞争中获得持久优势的利器。
- 世界级的开源项目:TiDB 如何重新定义下一代关系型数据库
- 一篇带你了解Go vs. Ruby
- 5月数据库流行度排行榜 三NoSQL位列前十
- 3、不明先知,根据相关征兆预测可能发生的事件,以便提前做好准备,赶紧注册相关域名。;不差钱域名;buchaqian抢先注册,就是这种敏感类型。预言是最敏感的状态。其次,你应该有眼力。所谓眼力,就是善于从社会上时不时出现的各种热点事件中获取与事件相关的域名资源。眼力的前提是对域名领域的熟悉和丰富的知识。
- 给Hadoop新手的一封信