Using MediatR in .NET? Maybe replace it with this

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


Hello everybody I'm Nick and in this video I will show you how you can improve MediatR's performance without fundamentally changing your code, by using a source generated version of MediatR.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

I immediately when I was done with this video installed this nuget into a personal project and replaced MediatR. While performance wasn't a major impact for me. I wanted to be 100% certain I could switch it out without an gotchas Nick may have missed but so far everything is working flawless. Giving this repo a star for sure.

TheRicherthanyouguy
Автор

This is becoming my favorite youtube channel real quick! I usually have your videos running in the background while working. <3

darkclove
Автор

I was using this today, on a project I am working on, had some problems, mainly because DbContext is Scoped and the Handlers are Singletons. But this was easily fixed by adding the options of the AddMediator() method and changing ServiceLifetime to Scoped. Just in case someone has the same issue. Also note that in the non-pre-release version the options method was not available inside Program.cs. I did not attempt the alternate way. Great Video, and as always great advice.

tanglesites
Автор

I will check it in private projects but I'm not sure I would dare to implement it in my employee's projects because of maintenance of the new Mediator. Nevertheless kudos for the author and all the participants ;)

RZEPEX_SPOT
Автор

This is exactly what I've been waiting for in terms of the Mediator approach. I think I've seen this library before but just assumed it was a typo-squatting thing haha.

buriedstpatrick
Автор

Source Code generation all the way. Anything you can fix at compile time will help you at runtime.

obiwanjacobi
Автор

It is a great video and a great aspect! However, I would rather still use MediatR as a well-known and good-maintained package in customer projects. These micro-performance approaches do not play a big role in my opinion. I happened to use a well-known NuGet Package that contains a bug which I discovered after using it for 2 Months I had to refactor my code and report the bug and it wasn't easy. So, I'd rather stay on the safe side but it is still worth mentioning as another option and I might use it in my private code as well.

samysammour
Автор

Please anybody, could you explain what problem MediatR solves?
Are there opensource real world project with MediatR?
I would like to see how it looks in complecated projects.

volodymyrliashenko
Автор

I wonder how this affects when doing the k6 testings. Thank you Nick!

SEA_
Автор

I would love to see a video from you about source generators. I don't plan on implementing my own any time soon but I would like to know how they work such that I can debug better.

Edit: I found two of your source generator videos.

crazyfox
Автор

I just replicated this solution in .NET 8, while using transient instances of the 'new' mediator. While I saw slight improvements in execution time and memory allocation, it was marginal compared to what you are showing. Maybe it's time to revisit this topic? Also it would be awesome if you could share the code of the examples you make in your videos. Cheers!

cptnbangatron
Автор

Great video about performance "compaints"! 😄

TimmyBraun
Автор

I add ONE comment about how I like MediatR and you hit me with this... now I have to decide whether to continue to use MediatR or switch to Mediator. Thanks Nick 🤔🤔🤔

brianm
Автор

Hi Nick. You talked at one point about mediatr "you like to use it, when it does made sense". Do you have a video about when to use it or not? Or do you mention it in one of your courses. Thx for your great content!

quadralm
Автор

1st brother, all your tuts are great 👍

unclelhix
Автор

can you review sqlkata query builder, compare to dapper in term of performance?

andikasaputra
Автор

I did a few benchmarks using scoped handlers and got some surprising results: the original MediatR was actually better! The source gen mediator caches DI resolved handlers for faster lookups and defaults to using singleton lifetimes. This is great, but in reality we all use scoped handlers with scoped dependencies (so we can be context aware) and hit the mediator exactly once per request\scope - there is no benefit to caching DI results in this scenario and in-fact the caching seems to add some substantial overhead. In these benchmarks MediatR comes out on top at almost half the time and two-thirds the memory allocation. Be careful everyone!

chrisv
Автор

I mesured MediatR bottlenecks in our project and for long running services, the problems are more by gc pressure and less by reflection, because almost all reflecrion is done at startup and not in hotpaths (in request handlers in http servers)
Sometimes, object pooling solves most problems.

IceQub
Автор

Have you ever compared the memory and performance per request of MediatR to MassTransit.Mediator?

alexisfibonacci
Автор

I really like this library. It's trully a game changer!

antonmartyniuk