When and How To Apply CQRS in an ASP.NET Core Application With EF Core

  Рет қаралды 6,426

Zoran Horvat

Zoran Horvat

2 ай бұрын

Check out Dometrain and use code ZORAN for 15% off any course ► dometrain.com/?coupon_code=ZORAN
Become a sponsor to access source code ► / zoranhorvat
Join Discord server with topics on C# ► codinghelmet.com/go/discord
Enroll course Beginning Object-Oriented Programming with C# ► codinghelmet.com/go/beginning...
Subscribe ► / @zoran-horvat
When the code that operates on storage becomes too complex due to mixed responsibilities regarding updates, validation, processes, and plain queries, we look for CQRS. This design principle states that we should segregate commands from queries and solve each using its own tools and design methods.
Despite what some programmers believe, CQRS does not require separate databases for commands and queries. It only requires separate models for commands and queries, and that is all you need to get going.
Once you understand that writing data requires one set of rules and models, whereas querying, often in many different shapes, requires an entirely different set of models, you can apply CQRS.
This video demonstrates how easy it is to introduce CQRS to an ASP.NET Core application with Entity Framework Core.
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️COPYRIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.
#csharp #dotnet #objectorientedprogramming

Пікірлер: 13
@zoran-horvat
@zoran-horvat 2 ай бұрын
Check out Dometrain and use code ZORAN for 15% off any course ► dometrain.com/?coupon_code=ZORAN
@johnnaysmith7218
@johnnaysmith7218 2 ай бұрын
Good explanation. I've used CQRS within one detabase for a while. Didn't even know that it is usually used for more then one database...
@seangwright
@seangwright 2 ай бұрын
I like using CQRS on all types of projects. Not because it helps me scale a solution, but because it's a good mental model for operations in a system and helps me keep a system organized, making it more comprehensible for others.
@zoran-horvat
@zoran-horvat 2 ай бұрын
That is my motivation as well. The types of projects I work on do not require a full-blown implementation of CQRS, and still I apply its striped-down variant all the time.
@johnnaysmith7218
@johnnaysmith7218 2 ай бұрын
Me too. Also, especially in API's I like to keep my calls simple, by calling C/Q directly in endpoint and keeping calls to a repo's methods, message queue or something else in a handler of a C/Q. That way my controllers remains clear.
@metallixbrother
@metallixbrother 2 ай бұрын
Thanks for another great video. You mentioned the possibility of separating domain and persistence model. Whilst I imagine that this would be relatively simple for queries (I believe that this would introduce another level of mapping), what would be the recommendation for commands, given that our domain models presumably lack tracking that EF Core provides?
@zoran-horvat
@zoran-horvat 2 ай бұрын
That is a very difficult question. You are right that the separation affects command. Queries already bring their own mapping, which is easy to introduce because the mapping is one-way. There are several methods of implementing separate models. The funniest idea I know of is to make the domain model observable, i.e. to implement the Observer pattern. The persistence layer would subscribe to updates and apply them to the database (or the DbContext). I am thinking of making a video with that idea.
@alfonsdeda8912
@alfonsdeda8912 2 ай бұрын
Hi Zoran, great video! Is good to use dbcontext directly in presentation layer, shouldn't I instead do a service/repository layer with all my queries and commands with cqrs in another layer and use dtos?
@zoran-horvat
@zoran-horvat 2 ай бұрын
If my custom DbContext implemented a custom interface with all methods defined as they are now, would that change anything? Vertical coupling within one feature is not such an issue, especially if it reduces the code size. I see much greater danger in horizontal coupling, such as my page displaying books also accessing prices - that is another component's responsibility. In the final version of the demo, the prices are obtained from an interface indeed, the implementation of which holds that same DbContext again, but who cares.
@alfonsdeda8912
@alfonsdeda8912 2 ай бұрын
My idea of separation was only to not use the dbcontext directly in razor page but instead use an interface, because in future maybe I won't use ef core but for example dapper and this would loose couple the page from the data manager, is this a wrong approach?
@zoran-horvat
@zoran-horvat 2 ай бұрын
@@alfonsdeda8912 Did you ever substitute one database provider with another in a component? Are you aware that repositories, transactions and a few other details are tightly bound to every provider to such a degree that you would have to change all the operations that update the database if you just switched the implementation? The difference between EF and Dapper is so great that there is no common interface that both can satisfy. Conversely, if you insisted on a common interface, that interface would remove what EF does best - any abstraction can only be as powerful as the worst of its implementers! All that only underlines my previous conclusion that vertical coupling is not a big deal. Many evangelists and authors will teach you that it is, putting an entirely needless burden of achieving 100% of persistence ignorance in your code. But what they don't tell you is that all you will have when you go to sleep is hands full of tired fingers and nothing more. The things that hurt your design lie elsewhere.
@alfonsdeda8912
@alfonsdeda8912 2 ай бұрын
@@zoran-horvat Thank you very much for your response, I should only inject a service interface with getBooks and other methods that the page needs, and in service implementation I would use ef core or other db providers if this changes, but maybe I misunderstood the sense of the video. Thank you very much for your time.
Combine Design Patterns To Reveal Their Greatest Power
14:21
Zoran Horvat
Рет қаралды 6 М.
Manage Nulls Like a Boss and Never Fail!
21:43
Zoran Horvat
Рет қаралды 10 М.
ИРИНА КАЙРАТОВНА - АЙДАХАР (БЕКА) [MV]
02:51
ГОСТ ENTERTAINMENT
Рет қаралды 1 МЛН
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 7 МЛН
Они убрались очень быстро!
00:40
Аришнев
Рет қаралды 2,9 МЛН
Master the Design of Functional Types in C#
17:53
Zoran Horvat
Рет қаралды 12 М.
How Do You Cancel an async Method? | Step-by-Step Tutorial
7:15
Zoran Horvat
Рет қаралды 6 М.
Background Jobs in ASP.NET Core
18:35
IAmTimCorey
Рет қаралды 45 М.
Learn To Love DDD-Style Strongly Typed IDs
9:04
Zoran Horvat
Рет қаралды 22 М.
Let C# Tuples Become Your Best Friends
11:51
Zoran Horvat
Рет қаралды 9 М.
Link Between Worlds
Retro For The First Time
Рет қаралды 3
Intro to MediatR - Implementing CQRS and Mediator Patterns
1:21:50
IAmTimCorey
Рет қаралды 216 М.
You May Have Never Learned This Lesson Right
13:02
Zoran Horvat
Рет қаралды 10 М.
wireless switch without wires part 6
0:49
DailyTech
Рет қаралды 3,4 МЛН
ПОКУПКА ТЕЛЕФОНА С АВИТО?🤭
1:00
Корнеич
Рет қаралды 1 МЛН
Карточка Зарядка 📱 ( @ArshSoni )
0:23
EpicShortsRussia
Рет қаралды 780 М.
КОПИМ НА АЙФОН В ТГК АРСЕНИЙ СЭДГАПП🛒
0:59
iPhone 12 socket cleaning #fixit
0:30
Tamar DB (mt)
Рет қаралды 32 МЛН