4

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmad
  • 49
  • 4

3 Answers3

2

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

Mohammed Alwedaei
  • 601
  • 1
  • 7
  • 14
  • 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? – Ahmad Sep 19 '22 at 07:50
  • do you mean something like this [Mediatr with generic handler and query](https://stackoverflow.com/questions/57504374/mediatr-with-generic-handler-and-query)? – Mohammed Alwedaei Sep 19 '22 at 07:55
  • No suppose i have this method: ``` Public List GetCustomer() { //Complex Where Clause => 40 Line return CustomerList; } ``` I use this in `CreateGroupCommandHandler`: ``` public class CreateGroupCommandHandler { public async Task 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. – Ahmad Sep 19 '22 at 10:23
  • what about using notifications so it can use the corresponding handler? just like the 4th resource I have included in my answer – Mohammed Alwedaei Sep 19 '22 at 14:26
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.

klekmek
  • 533
  • 3
  • 11
  • 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? – Ahmad Sep 19 '22 at 10:27
  • 1
    @Ahmad You can add a request property of `IEnumerable` 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/commands – klekmek Sep 19 '22 at 10:49
  • Your means of service is external service? like `Email Sender`? @klekmek – Ahmad Sep 19 '22 at 11:21
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

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thank you, my quastion is: can we call a `Command` like 'CreateAccountCommand' in your picture, in a `NotificationHandler`? what about another `Command` ? @jeremy-thompson – Ahmad Sep 19 '22 at 06:01