Forget Everything You Know About MediatR!
Вставка
- Опубліковано 25 чер 2024
- MediatR is one of the most popular library for .NET developers. It latest release, however, came out with some important breaking changes. In this video I go through those changes that might affect your day to day work if you want to upgrade to MediatR 12 or if you are starting a brand new project.
#dotnet #csharp #dotnetdeveloper #programming
Join this channel to get source code access and other perks:
/ @codewrinkles
Also follow me here (especially if you are a self taught developer):
✅My other channel: / @danpatrascutech
✅Facebook: / danpatrascutech
✅Instagram: / danpatrascutech
✅TikTok: / danpatrascutech
✅Newsletter: www.danpatrascu.tech/
Content
1. Intro: 00:00
2. The MediatR package: 00:51
3. MediatR service registration: 02:40
4. "Void" returning request handlers: 05:00
5. Parallel notification handler execution: 07:00
6. Upgrading to MediatR 12: 10:02
My setup:
Camera - Canon EOS M50 Mark II: amzn.to/3SJxS4d
Lav mic - Rode Lavalier GO Professional: amzn.to/3mmZS1B
Condenser mic - Shure SM7B: amzn.to/3JaqjQN
Audio console - Rodecaster PRO II: amzn.to/3KTVMIg
Laptop - Dell Latitude: amzn.to/3KV4SEW
Monitors - Benq 27 inch: amzn.to/3JbM6aU
Lights - 2x Godox SL-60W: amzn.to/3KV3qCj - Наука та технологія
I like the title of the video. It highlights precisely why you should not bother too much with remembering exact implementations and instead focus on truly understanding and grasping concepts. Concepts remain the same, but implementations change often. Thanks for another great tutorial, Dan!
I'm glad you enjoyed it.
MediatR is very nice, but there is always the question "Should I use it or not in production?"
I'm not sure how much you follow we on Twitter or LinkedIn, but I'm certainly that kind of guy that thinks adding a library to your project means addign technical debt. Which might be good or bad, depending how you plan to deal with this technical debt. Coming back to MediatR, I'd say it depends. In some projects I've used it. In some projects not. Where I find the most value in this library are pipeline behaviors, so if I think that in a project I'll make heavy use of this feature, then I prefer to use MediatR than to write similar functionality.
@@Codewrinkles mind explaining why you think adding third party libraries to an enterprise project is adding technical debt?
@@manilladrift one reason is you will have to maintain that package. I have the same opinion , if you can get by without depending on anything external i prefer that way, but this is more so a personal agreement than a general guideline.
There are a lot of reasons like intellectual property (customers want to have IP over the code of their apps), library lock-in, potential unpredictable costs, security vulnerabilities and so on
@@nove1398 you also need to seriously weigh up that you might have just transferred that technical debt. A developer must now build it and maintain it. This build might not actually offer any business value.
Very popular libraries are often battle tested by 100’s of thousands of developers.
As with everything, it depends 👍🏻
However, if you are not the one paying for the development, then i do not agree that “personal preference” is a valid reason. Licensing costs/restrictions, etc - absolutely.
Thanks for the video. This background music in my opinion fits most and don't interrupt the experience.
Thank you very much for watching and for the feedback.
Hi, i don't get the concept of the Request and the handler of it. Cause Mediatr has the Send function, passing a object, and 1 handler for handling that object. But why it is better compared to just calling a command/query directly?
Thanks. Amazing video.
I have a video suggestion idea if it's alright: could you do video on inheritance and entity framework in regards creating models and registering them in DbContext class. Also how would we view different derived model in a blazor component and how would we return the derived model classes in the data access layer of the project?
For example, do we need to do some sort of casting to view the derived class in a blazor server component?
Great suggestion. The topic of persistence strategy for inheritance trees is actually a very good one. I think the Blazor part would fit in a different video though. I'm considerin these ideas.Thanks for watching and commenting.
@@Codewrinkles Thank you so much for considering this. Looking forward to learning more c# from you 😊
Imagine stopping the video at "Forget everything you know about MediatR".
JK. Good video thanks!
is this new version more performant then the "mediator" package? (its a bit aside of goal of this vid sorry)
I'll be honest with you and I'll say that I haven't run any benchmark against it so far. But that's a good idea and maybe something for a dedicated video. Thanks for watchin and for the comment.
Does it also support notifications and behaviors?
MediatR’s performance overhead is negligible compared to things that you would do in handlers like I/O. (microseconds vs milliseconds)
If performance is a real consideration (which is IMO vastly overstated), then it is unlikely that MediatR would be a real candidate for a tool anyway.
I don't know why people need MediatR. I can do everything with DI and .net core features.
Can you remove music please
Thank you for the feedback. I will take this into consideration.
I actually like the music.
I've encountered an annoying problem when I created a project with dotnet 7 and used MediatR 12.0.1I configured MediatR in Program.cs as bellow :builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
and use it in the controller as bellow : public async Task AddCustomer([FromBody] CustomerInfo command) {
await _mediator.Send(new AddCustomerCommand(command)); return StatusCode(201); }
Apart from that I implementing Handler as bellow : private readonly ICustomerRepository _customerRepository; public AddCustomerCommandHandler(ICustomerRepository customerRepository) => _customerRepository = customerRepository; public async Task Handle(AddCustomerCommand request, CancellationToken cancellationToken) { await _customerRepository.AddCustomerAsync(request.customer); return; }
and Command as :public record AddCustomerCommand(CustomerInfo customer) : IRequest;
but encountered :No service for type 'MediatR.IRequestHandler has been registered.
I googled that error and tried several things to configured MediatR in Program.cs but the result was the same.Could you please help me ?
I had the same experience. I CANNOT get MediatR 12 to work!