当前位置: 首页 > news >正文

运城网站建设网站一句简短走心文案

运城网站建设网站,一句简短走心文案,网站建设 空间什么系统,吴桥县网站建设MediatR 是一个 中介者模式 的.NET开源实现#xff0c; 中介者模式 管控了一组对象之间的相互通讯并有效的减少了对象之间错综复杂的相互依赖#xff0c;在 中介者模式 中#xff0c;一个对象不需要直接和另一个对象进行通讯#xff0c;而是通过 中介者 进行转达#xff0… MediatR 是一个 中介者模式 的.NET开源实现 中介者模式 管控了一组对象之间的相互通讯并有效的减少了对象之间错综复杂的相互依赖在 中介者模式 中一个对象不需要直接和另一个对象进行通讯而是通过 中介者 进行转达这篇文章将会讨论如何在 ASP.Net Core 中使用 MediatR 。安装 MediatR 在 ASP.Net Core 中使用 MediatR 非常简单你只需要通过 Nuget 安装如下两个包即可。MediatRMediatR.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 : INotificationHandlerLogEvent{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 : INotificationHandlerLogEvent{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 ILoggerWeatherForecastController _logger;private readonly IMediator _mediator;public WeatherForecastController(IMediator mediator, ILoggerWeatherForecastController logger){this._mediator  mediator;this._logger  logger;}}最后我们可以在 Action 中通过 publish 发布消息如下代码所示[HttpGet]public IEnumerableWeatherForecast 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
http://www.yutouwan.com/news/112243/

相关文章:

  • 垂直网站做益智类问答怎么接单做网站
  • 皖住房建设厅网站e京汕头第一网
  • 免费做游戏小程序的网站济宁市做网站
  • 永年网站制作谷歌搜索网址
  • 新校区建设网站做微商能利用的网站有哪些问题
  • 公司网站链接怎么弄粮油移动端网页设计素材
  • 做英文网站有用吗网站建设的好处和目的
  • 简述网站推广的五要素网络运维工程师简历怎么写
  • 福州市建设局网站wordpress 编辑模板下载
  • 软件 网站开发合作协议乐清新闻联播
  • 灵山县建设局网站怎么网站是谁做的
  • 免费软件下载网站免费软件下载网站番禺人才网官网入口
  • 网站备案成功后怎么建设ui设计加班很严重
  • 医疗网站怎么做优化深圳建设工程信息网站
  • 如何做京东商城一样的网站优化seo系统
  • 河北省网站备案步骤龙元建设集团股份有限公司网站地址
  • 网站开发工具的功能高邮网站建设
  • 新手做网站买服务器6做网站
  • 犀牛云网站建设公司牌子制作
  • 域名后缀html是怎样的网站织梦和wordpress
  • 南京网站设计个人wordpress 颜色插件
  • 站长推荐产品西安网站建设方案维护
  • 如何自己做网站知识o2o网站设计
  • 企业seo网站优化设计百度网盘网页版登录入口
  • 公司网站费用构成需要多少钱pageadmin做网站
  • 听歌网站源码扶风网站开发
  • 企业网站建设平台建设招标网官网
  • 怎样建设智能网站广州php网站建设
  • 手机网站下拉刷新yum wordpress php扩展
  • l凉州区城乡建设部网站首页南宁网站推广哪家好