How to Implement the CQRS Pattern in Clean Architecture (from scratch)

preview_player
Показать описание

CQRS, which stands for Command Query Responsibility Segregation, is a popular architectural pattern. In this video, I will show you how to implement CQRS. We'll create custom command and query abstractions required for CQRS. And I'll show you how to implement a use case with a command. I'll also discuss the importance of the CQS principle, which is the precursor to CQRS.

Join my weekly .NET newsletter:

Read my Blog here:

CQRS Pattern With MediatR:

Chapters
0:00 What is CQRS?
1:05 CQS Principle
1:55 Logical CQRS architecture
4:03 Adding custom CQRS abstractions
9:09 Implementing a Command use case
Рекомендации по теме
Комментарии
Автор

Thank you so much. I just started learning about CQRS Pattern and your clean code helped me a lot.

sniperharshgaming
Автор

Thanks for the great content! I have a suggestion. Could you please make a video about deciding aggregate root. The way you think and the logic you apply in such a scenario. Simply how do you decide? I saw your previous video regarding this. But it would be great if you can explain the decision making part more than coding part in this case. Thanks Milan 😊

kasunjalitha
Автор

Ma svaka ti cast milane super Video stvarno si dobro objasnio💪🏻💯. Greetings from germany🇩🇪

mitar
Автор

A premium quality video. Well done indeed.

LordErnie
Автор

That's awesome, Thank you Milan. See you in tommorow morning's @newsletter

BirhanNega
Автор

Great video as always and i have a question. Could you make a video about request localization?

flobuilds
Автор

@milan is there anywhere I can see the full implementation of your result class. I think it's such a clean way of writing and resulting results. I'd like to do the same in both personal and professional projects.

uu
Автор

Nice video! I have two questions for you. In the video, you mentioned using Dapper for queries. Now, with EF8 SqlQuery, which one do you think is best to use: SqlQuery or Dapper? Another question is, how do you set up the 'Add => New From Template' option?

marcelotieghi
Автор

Thank you for the great content, Milan.
Have you a git for your examples? Often, while watching the video, linked questions appear. And, imho, it is more useful to look at the code to understand where it is located, referenced, and implemented related things rather than search for them on the video.
Thanks

dmitryye
Автор

Thanks. Would you do an Event Sourcing video please?

rorycawley
Автор

Hello, thanks for your video, always i have had this question, the business logic in clean architecture go in domain layer? or in application layer, i have seen others videos where the business logic is in the application layer and not in the domain. thanks.

oscarjuliangilbernal
Автор

Hi Milan, appreciate your video and insights! Wondering if implementing CQRS is beneficial for optimizing response times in a rating system, especially when dealing with large amounts of data, to prevent incorrect reads or potential deadlocks? Thanks!

Omarahmed-fsjv
Автор

thanks for the video, and I think that mentioning db read and write(db recaplication) in this topic is redundant, because CQRS is not related to it, and your example also did not mention anything about db recaplication, in fact you have no cqrs between your application and db (most of time we don't need it because efcore support unit of work), you only have it between your api and application. CQRS is about using different models for read and write, not about db recaplication, even you have db recaplication in your project, you don't need CQRS becausse thoses read and write db are still using the same model (entity)

biwjfhy
Автор

Thanks for the video (the guy who asked about domain services…). So is it “ok”/valid to inject the domain service class directly in a handler? Why can’t you create an interface for the domain service (follower service)? Just curious about best practices in ddd using domain services.

rxzzrtt
Автор

Great video! I have a question. Suppose I am using Dapper for the write side as well as the read side while implementing DDD and clean architecture, including the CQRS pattern. I can easily replace a DbContext implementation with my standard class library using Dapper that contains methods for saving and loading data from the database, then call those methods from the repository implementations in the infrastructure layer. It seems like nearly everything else (DDD, CQRS, repositories, etc.) all would remain just as you have it, but does UnitOfWork become extraneous in that scenario as an EF Core-specific item? Or would I still use the UOW even with Dapper on the write side?

simonreiff
Автор

Is it an anti-pattern to call queries inside command handlers ?
For example, you want to fetch some entity from the db to update it inside a command handler. It seems like we can avoid duplicating querying logic by using a query inside a command handler instead of injecting repository or raw sql. The query handler will already have an implementation where it calls repository or raw sql.

rsmetalhead
Автор

Thank you for sharing your knowledge. After watching this video I have 2 questions:
1)In my project generally we follow this: Create generic repo, if needed extend repo for specific class. And then use those repo withing Services layer and call them on controller. But here I see you used commands and queries which are relying on Services.
Instead can't we have simply 2 set of services for read and write? (My question might not be the best but I am just trying to understand what is with to add another layer of calls)
2)Let say once I get clarity on my 1st question then is it good practice to call repository and services both on handler or is better only use services on command and no direct access to repository?
TIA @MillanJovanovicTech

GrishmaDoshi
Автор

Hi again! Another question: If u call on another entity in the domain service; does that become an application service? Or can you still call another domain in another domain service?

rxzzrtt
Автор

Thank you Milan for high quality content. I have two questions, need your help to understand Mediator and CQRS pattern.
1. About CQRS - It is good idea to use this pattern if you want to use 2 databases, one for write and another one for read operations.
My questions are : 1. There is high chance of delay in data sync bwt write to read database, how to deal with it?
2. If I want to use single database for both read and write, Is it worth using this pattern?
3. If I want to fetch data based on multiple filters rather getbyId or getbyemail. Is this fine? and how to achieve it?
4. How to handle if I have to fetch data from multiple tables in one go..

NavneetKumar-fsmc
Автор

Hi Milan! One question! Why FollowingService (where have the logic) are in Domain Layer?? Should be in Application Layer, no? 🤔