MediatR conventions in CQRS
Asked Answered
N

3

6

I am using MediatR and CQRS in a real project using the Clean Architecture principles.

Is it correct to:

  1. Call Command from NotificationHandler?

  2. Call Command in Command?

  3. Call Query in Command?

If you have some resource for these principle, please share them with me.

Neuroglia answered 19/9, 2022 at 4:38 Comment(0)
G
1

Mediator handlers, commands, queries and notifications should follow the Single responsibility principle (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.

Guardian answered 19/9, 2022 at 6:58 Comment(3)
Thanks for your answer, i completely undrstand idea behind this principle. I have another question. If i have a method with 30-40 line code that will be return Customer List and need this method in multiple Command Handler . what can i do? copy/pase this method in all Command Handlers?Neuroglia
@Neuroglia You can add a request property of IEnumerable<Customer> to your Command and use that property in your handler. Also, it is fine to call services, data contexts from within your Handler, just don't call other handlers/commandsGuardian
Your means of service is external service? like Email Sender? @GuardianNeuroglia
B
1

MediatR is a simple mediator pattern implementation in .NET as it keeps things separated and contributes to Single Responsibility Principle.

And CQRS stands for Command and Query Responsibility Segregation, which results in a pattern separating both read and write to maximize performance and scalability.

MediatR has 2 types of messages:

  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? 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

Some great Resources:

Use MediatR in ASP.NET or ASP.NET Core

Intro to MediatR - Implementing CQRS and Mediator Patterns

Clean ASP.NET Core API using MediatR and CQRS | Setup

This video talks about notifications: The 2 MediatR features people don't know about but should

Bookstore answered 19/9, 2022 at 7:19 Comment(4)
Thanks for your answer, I have a complex query(Suppose get customers by many filter) and i need this customers in multiple CommandHandler. I don't want to repeat this complex query in all CommandHandlers, What can i do? repeat this complex query?Neuroglia
do you mean something like this Mediatr with generic handler and query?Bookstore
No suppose i have this method: ``` Public List<Customer> GetCustomer() { //Complex Where Clause => 40 Line return CustomerList; } ``` I use this in CreateGroupCommandHandler: ``` public class CreateGroupCommandHandler { public async Task<Unit> Handle() { var customers= GetCustomer(); // insterCustomerGroup } } ``` and i use this method GetCustomer in multiple CommandHandler. I unstrand your answer thank you, But what is your suggest in this situation.Neuroglia
what about using notifications so it can use the corresponding handler? just like the 4th resource I have included in my answerBookstore
W
0

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

The Commands are for Insert, Update and Delete operations where as the Queries are for Reads:

enter image description here

Wrathful answered 19/9, 2022 at 4:44 Comment(1)
Thank you, my quastion is: can we call a Command like 'CreateAccountCommand' in your picture, in a NotificationHandler? what about another Command ? @jeremy-thompsonNeuroglia

© 2022 - 2025 — McMap. All rights reserved.