简体   繁体   English

CQRS 中的 MediatR 约定

[英]MediatR conventions in CQRS

I am using MediatR and CQRS in a real project using the Clean Architecture principles.我在使用 Clean Architecture 原则的实际项目中使用 MediatR 和 CQRS。

Is it correct to:是否正确:

  1. Call Command from NotificationHandler ?NotificationHandler调用Command

  2. Call Command in Command ?Command Command

  3. Call Query in Command ?Command中调用Query

If you have some resource for these principle, please share them with me.如果您对这些原则有一些资源,请与我分享。

If you have some resource for these principle please share with me.如果您对这些原则有一些资源,请与我分享。

A great resource is this video Intro to MediatR - Implementing CQRS and Mediator Patterns一个很好的资源是这个 Video Intro to MediatR - Implementation CQRS and Mediator Patterns

The Commands are for Insert, Update and Delete operations where as the Queries are for Reads:命令用于插入、更新和删除操作,而查询用于读取:

在此处输入图像描述

Mediator handlers, commands, queries and notifications should follow the Single responsibility principle (SRP).中介处理程序、命令、查询和通知应遵循单一职责原则 (SRP)。 They should do as little as possible, be atomical.他们应该尽可能少做,是原子的。 Calling other commands, queries or notifications within a handler is a bad habit as it creates coupling.在处理程序中调用其他命令、查询或通知是一个坏习惯,因为它会产生耦合。

MediatR is a simple mediator pattern implementation in .NET as it keeps things separated and contributes to Single Responsibility Principle . MediatR 是 .NET 中的一个简单中介模式实现,因为它保持事物分离并有助于单一责任原则

And CQRS stands for Command and Query Responsibility Segregation , which results in a pattern separating both read and write to maximize performance and scalability. CQRS 代表Command and Query Responsibility Segregation ,它产生了一种分离读写的模式,以最大限度地提高性能和可扩展性。

MediatR has 2 types of messages: MediatR 有两种类型的消息:

  1. Request and response messages that will dispatch a single handler将分派单个处理程序的请求和响应消息
  2. Notification messages that will dispatch multiple handlers将分派多个处理程序的通知消息

Requests describe your commands and query behavior as they are fairly simple and specific.请求描述您的命令和查询行为,因为它们相当简单和具体。

Handler is what you will need to manage/solve a request.处理程序是您管理/解决请求所需的。 Where each request has its own handler.每个请求都有自己的处理程序。

Commands are where you will perform Insert, Update, and Delete operations.命令是执行插入、更新和删除操作的地方。

Queries are where you will perform read operations only .查询是您将执行读取操作的地方。

Notifications is where you will manage/solve a single request by using multiple handlers.通知是您将使用多个处理程序管理/解决单个请求的地方。 So in this case you will create a notification message and create the corresponding handlers for your notification.因此,在这种情况下,您将创建一条通知消息并为您的通知创建相应的处理程序。

Remember that you should know the engineering functionalities of your application and based on it decide whether to use a request/response message or notification message.请记住,您应该了解应用程序的工程功能,并根据它决定是使用请求/响应消息还是通知消息。 And more importantly, do not try to mix and match these 2 types of messages as you wish because you will break the mean of these patterns.更重要的是,不要随意混合和匹配这两种类型的消息,因为你会破坏这些模式的均值。

So my question is why would you need to call a command from the notification handler?所以我的问题是为什么你需要从通知处理程序调用命令? and why do you want to mix 2 commands?为什么要混合 2 个命令? and why would you mix the read and writes of your application?为什么要混合应用程序的读取和写入?

If you understand the 2 types of messaging in MediatR you will know the answer to your question and for any future questions如果您了解 MediatR 中的两种消息传递类型,您将知道您的问题和任何未来问题的答案

Some great Resources:一些很棒的资源:

Use MediatR in ASP.NET or ASP.NET Core 在 ASP.NET 或 ASP.NET 内核中使用 MediatR

Intro to MediatR - Implementing CQRS and Mediator Patterns MediatR 简介 - 实现 CQRS 和中介者模式

Clean ASP.NET Core API using MediatR and CQRS |使用 MediatR 和 CQRS 清洁 ASP.NET 内核 API | Setup设置

This video talks about notifications: The 2 MediatR features people don't know about but should该视频讨论了通知:人们不知道但应该知道的 2 MediatR 功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM