MediatR: Why should you use it?

  Рет қаралды 60,880

CodeOpinion

CodeOpinion

Күн бұрын

Пікірлер: 55
@sulton-max
@sulton-max 9 ай бұрын
When this video came out I just started learning APIs. And started using MediatR in my APIs last year The reasons I use it - clean architecture - I like to divide each request to query, command, events - transactions - although not many projects are focusing on handling these, MediatR is the best place for it I think - in-memory event bus - to ship dev apps fast, it's just the perfect solution I think, I use `IEventBusBroker` initially implemented only with `MediatR`, then can use whatever event bus I want
@david3552
@david3552 2 жыл бұрын
I am a neophyte to clean architecture and your chanel is a fantastic way to get a clear overview of complicated subjects. Thanks!
@CodeOpinion
@CodeOpinion 2 жыл бұрын
Thanks!
@mishamovdivar
@mishamovdivar 4 жыл бұрын
Thanks for sharing. One more reason i would add for using mediatr is that, you not only want to separate out the business logic from controller because that might be used by another client (webjob, etc) but to respect the Single Responsibility and Separation of Concerns which will bring, at least, code readability and testability benefits
@CodeOpinion
@CodeOpinion 4 жыл бұрын
Yes for sure, great point. If you use behaviors/middleware/pipeline, it makes separation and therefor testing nice. I should create a video on exactly that. Thanks for the comment!
@MaxLaurieHutchinson
@MaxLaurieHutchinson 4 жыл бұрын
@@CodeOpinion please do :)
@marna_li
@marna_li 2 жыл бұрын
I remember starting using MediatR 3-4 years ago. I just used the Requests and did not get what it was all about. I did not understand loosely-coupled monoliths, microservices, messaging or event-driven architecture. In hindsight these would be useful back then. I did not even know about this channel until last year, Now I feel like I have excelled a lot in just that short time.
@Imonfire328
@Imonfire328 2 жыл бұрын
I’ve been using mediatr almost exclusively for a year and the biggest feature for me is the middleware layer where I can assign user specific information I don’t want exposed in the view. Also each handler has a defined “contract” in the command request in what’s being changed or allowed to be modified on that request. Really easy to read, and makes the code difficult to screw up imo
@iamnoob7593
@iamnoob7593 Жыл бұрын
what do u mean not exposed in view ? its just backend code right?
@johnf7755
@johnf7755 4 жыл бұрын
I feel I achieve the decoupling you talk about here through, you know, interfaces, dependency injection, hexagonal architecture etc and the "pipeline" through generic handler functions if required. Language features and some good architecture. The code documents it's dependencies. I don't see that with MediatR. Is one of your arguments for it really that you need MediatR because you might want to call your business logic outside of a web context? It feels very cargo cult to me.
@CodeOpinion
@CodeOpinion 4 жыл бұрын
With anything that becomes popular, I think it's usage could become cargo cult. People just using it without any idea why. Hence why I'm trying to describe some reasons why I find the pattern useful. That doesn't mean those reasons apply to the context of the project anyone else working on. Converting HTTP requests to application requests is a big reason in the applications I generally live in. As you asked, yes, it's never just a web context for me. There are generally many different inputs to applications that I work in besides HTTP. It also becomes a gateway for a few more things such as using the same paradigm for in-process and out-of-process messaging handling. It also servers as a gateway for for vertical slice feature based style of writing and organizing code. I'll be getting more to these ideas in future videos for sure. I appreciate the comment.
@impeRAtoR28161621
@impeRAtoR28161621 Жыл бұрын
But what is difference to simple having business layer and API that references that business layer and thats it?
@vktop2
@vktop2 Жыл бұрын
Good video, can I use the Mediator pattern to decouple what I used to decouple with Interfaces? Regards
@iliyan-kulishev
@iliyan-kulishev 3 жыл бұрын
In my assessment of MediatR, I wouldn't bother using it if I didn't have a need for some kind of filtering. Otherwise I see nothing wrong of just referring to my handlers directly.
@CodeOpinion
@CodeOpinion 3 жыл бұрын
Agree, if you're not creating a pipeline for a request a lot of the benefit is lost.
@leepaulalexander
@leepaulalexander 2 ай бұрын
I haven't looked at mediatR but in your example throwing an exception to convey a not found doesn't sit well with me...for these reasons: performance, code clarity and finally separation of concerns.
@georgepinargote
@georgepinargote Жыл бұрын
Hi sir, How to use it like a vertical slice Gateway?
@sampsonorson
@sampsonorson 4 жыл бұрын
Nice tutorial. I remember you also talked about MediatR sometime ago.
@CodeOpinion
@CodeOpinion 4 жыл бұрын
Yes I have in the past, I think mainly on codeopinion.com
@codewithkashif
@codewithkashif 3 жыл бұрын
I have a quick question i.e. why people use MediatR with CQRS only?
@CodeOpinion
@CodeOpinion 3 жыл бұрын
Not sure. It doesn't imply or force you to use CQRS. You could have a request that changes state and also returns state. Hence why it's called an IRequest and not an ICommand/IQuery.
@codewithkashif
@codewithkashif 3 жыл бұрын
@@CodeOpinion yeah totally agreed with you and i was also on same notion. I just have seen over internet people mostly uses MediatR with CQRS only, that’s why i was little cautious. Thanks for your quick help!
@kapquidlat1122
@kapquidlat1122 3 жыл бұрын
Thanks for this very informative content. I can see some benefits to using MediatR but isn't this over-engineering? For MVC apps can't we just stick to DI, services and repositories for decoupling and separation of concerns? Or am I missing the point? Hope you can shed some light on this.
@CodeOpinion
@CodeOpinion 3 жыл бұрын
One of the major benefits are if you want to create a request pipeline. Check out the video I did on Pipes & Filters: kzbin.info/www/bejne/o6S7pYFnataLhac
@kapquidlat1122
@kapquidlat1122 3 жыл бұрын
@@CodeOpinion Thanks! I'm going to watch that next.
@iamnoob7593
@iamnoob7593 Жыл бұрын
@@CodeOpinion Thanks i wanted to know what is this request pipeline for
@SlashTagged
@SlashTagged 3 жыл бұрын
In regards to using a messaging consumer as an entry point, how would you handle the application logic? On one hand, you could put that logic in the "Application" project and have the Consumer project reference it, but then you introduce (informational) coupling b/c the consumer may only care about one function/handler from that dependency. On the other hand, you could put the logic in the Consumer project (only), but then you'll need you'll need to fiddle around in that project any time the business logic changes. Does it even make sense to extract out functionality like this to some shared project that both a messaging consumer AND web application (REST API) can use? Keep up the great work! These videos have been very helpful.
@CodeOpinion
@CodeOpinion 3 жыл бұрын
I don't separate them. I often think about message handlers being invoked from anywhere. So as mentioned it could be invoked from a web request or possibly from consuming a message. *_Often_* times the context of how it's being invoked doesn't matter. In some situations it does but in the vast majority (in my cases) it doesn't.
@DanielRodriguez-ds2qs
@DanielRodriguez-ds2qs 2 жыл бұрын
Just leaving a message here to show appreciation and for the youtube algorithm
@CodeOpinion
@CodeOpinion 2 жыл бұрын
Ha! Thanks 👍
@mana20
@mana20 2 жыл бұрын
Do you have a preference for Messaging and Message Broker Frameworks? I'm sure it depends on the project, but do you typically lean towards any specific ones?
@CodeOpinion
@CodeOpinion 2 жыл бұрын
If you're in the .NET space, NServiceBus, MassTransit, Brighter.
@brandonpearman9218
@brandonpearman9218 3 жыл бұрын
point 1 & 2: mediatr not needed at all to do these point 3 is an ok reason if you like that pipeline style, but not needed. After working with mediatr for 3 years now its quite annoying, Especially because it makes coding harder especially for junior or devs unfamiliar with mediatr. So here are some counters reasons 1. cant easily go to the implementation, like you could with a method (f12). 2. pipeline implementations often go unnoticed cause they are harder to find 3. lack of an interface dependency in the controller makes the flow more illusive 4. unneeded nuget package dependencies 5. isolation of handlers can be a good thing but can also create a lot of complexity when its a bad thing 6. usually get an explosion of classes because you need to keep adding a butt load of handlers (which was once a single class)
@CodeOpinion
@CodeOpinion 3 жыл бұрын
I can get behind these arguments. I don't necessarily disagree with any of them! I would say my biggest argument for using it is to get a taste of messaging. At around 4:50 is really what it boils down to. A lot of messaging libraries in the .NET space use a type routing, which is how MediatR works, so you'll still run into the same issues you described in your list if you use almost any of them.
@brandonpearman9218
@brandonpearman9218 3 жыл бұрын
Thanks for reply. Yeah agreed those down sides are acceptable when dealing with a queue but i see many projects which use mediatr and nservice bus so some handlers are from a queue and others are just there to be annoying. The reason they say is that mediatr = CQRS, as if it is not possible to do CQRS without it. Also i hear the reason, to enforce SRP, which they define one public method per class.
@vinipaivas
@vinipaivas 3 жыл бұрын
Completely agree with you guys. It seems like MediatR might be more useful for applications that are not web applications. Web apps have a pretty concise flow of code already while background process can have all sort of entry points which, depending on the dev, can get quite messy. I believe MediatR brings some standards and enforce better code separation in those scenarios.
@rainron2664
@rainron2664 3 жыл бұрын
Sir how did you configure that kind of fixed intellisense thing on the side of yout code? ive been seeing that from some tutorial vids. Im curious, and find it helpful. Thank you and more power 😊
@CodeOpinion
@CodeOpinion 3 жыл бұрын
The type definition? It's built into Rider. Really useful.
@puravupadhyay2961
@puravupadhyay2961 Жыл бұрын
I figured out that you're a Canadian by the way you said "About" 😀
@GR_PROF
@GR_PROF 4 жыл бұрын
What font is used in the editor ?
@CodeOpinion
@CodeOpinion 4 жыл бұрын
Just the default font in Rider which I believe is called "JetBrains Mono"
@GR_PROF
@GR_PROF 4 жыл бұрын
@@CodeOpinion Thanks
@jameshickey84
@jameshickey84 4 жыл бұрын
Good stuff 👍
@dimapetrichenko6190
@dimapetrichenko6190 10 ай бұрын
Nothing explained properly, how to connect that abstract behaviour to you concrete mediator
@adnanahmed316
@adnanahmed316 3 жыл бұрын
REPO LINK????
@CodeOpinion
@CodeOpinion 3 жыл бұрын
github.com/jbogard/MediatR
@adnanahmed316
@adnanahmed316 3 жыл бұрын
@@CodeOpinion love you
@yasser2768
@yasser2768 4 жыл бұрын
Holy Moly I will stick to Unit Of Work and Repositories
@rmbl349
@rmbl349 3 жыл бұрын
I don't see why anybody should use this. I tried multiple times over 3 teams and it was just more complicated.
@CodeOpinion
@CodeOpinion 3 жыл бұрын
If you're not using what Mediator calls behaviors to create a request pipeline, I'd agree in a lot of situations that it's not necessary. I've created a video about Pipes & Filters kzbin.info/www/bejne/o6S7pYFnataLhac
@megasuperlexa2
@megasuperlexa2 3 жыл бұрын
It is as unneeded as losing 30% of screen real estate on a fancy mic which sound exactly like builtin MacBook one (speechwise)
@CodeOpinion
@CodeOpinion 3 жыл бұрын
Thanks for your wonderful feedback 😆
Organize Code by Feature | Vertical Slices
9:06
CodeOpinion
Рет қаралды 17 М.
Intro to MediatR - Implementing CQRS and Mediator Patterns
1:21:50
IAmTimCorey
Рет қаралды 233 М.
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
Clean Architecture Example & Breakdown - Do I use it?
15:25
CodeOpinion
Рет қаралды 188 М.
10 Years of Software Architecture: Best And Worst Decisions
15:09
Using MediatR in .NET? Maybe replace it with this
11:49
Nick Chapsas
Рет қаралды 86 М.
Should you use the Repository Pattern? With CQRS, Yes and No!
10:27
RPC vs Messaging: When to use which?
9:10
CodeOpinion
Рет қаралды 28 М.
API Caching is Hard! Pitfalls you need to know
11:22
CodeOpinion
Рет қаралды 3,5 М.
CQRS Doesn't Have To Be Complicated | Clean Architecture, .NET 6
24:09
Milan Jovanović
Рет қаралды 119 М.
STOP dogmatic Domain Driven Design
7:18
CodeOpinion
Рет қаралды 136 М.
“.NET 9 Is Killing MediatR, MassTransit & Wolverine!”
11:59
Nick Chapsas
Рет қаралды 93 М.
8 await async mistakes that you SHOULD avoid in .NET
21:13
Nick Chapsas
Рет қаралды 317 М.
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН