Building a Mediator library in .NET from scratch

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

Hello everybody I'm Nick and in this video I am going to show you how you could implement your own version of the popular MediatR library by Jimmy Bogard. This won't be an 100% implementation but rather a "get you started" approach and I am going to talk you through my thought process. You can then hopefully use those learning in your own libraries as well.

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

Social Media:

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

Bro you're doing great work. Please don't stop teaching us.

shakhzodniyozov
Автор

So glad you're making a video on variance. I remember reading about it in "C# in depth" and not understanding a single thing

arditb
Автор

Hey Nick, it would be great if you can explain how to improve that part of reflection in another video. Thanks for all, I am not a English speaker but I can understand you very clearly.

leandrowitzke
Автор

Exact explanation about mediator pattern. Explained cleanly. ✌

neeleshbodgal
Автор

I implemented a similar solution couple weeks back using delegates. Good to know another approach using mediators. The service injection part using assembly scanning was very interesting and I plan to try that out in next opportunity. Thanks for this info.

excalibur
Автор

Your are an animal. Amazing presentation and insight into Mediator. Thank you.

beetzr
Автор

I actually built the same thing in my current company, because i did not know about MediatR library, but used a generic proxy to resolve the handlers instead of reflection.

SelmirAljic
Автор

Awesome content as usual. Looking forward to your co/contravariant video!!!!

phyberoptyk
Автор

You have great insight and can explain it well, but what has helped me with every programming job so far: If something sounds to complex for what it is, it usually is..

Wizatek
Автор

Great video Nick. I think its great how you show the breakdown of how this can be structured from scratch, in such clear concise sections. Can you show how one could possibly add multiple handlers for one request type? Assuming something like Notification in MediatR, from scratch. Specifically how you could modify/construct the dependency inject portion for multiple associations.

littlekujo
Автор

I think you could use some improvement. Maybe handlers are in another assembly than requests? Little change required for such situation. I will be very happy to see how to optimize that code.

duszekmestre
Автор

I have done it like this:

interface IHandler { public Task<object> HandleAsync(object request); }
abstract class IHandler<TRequest, TResponse>: IHandler where TRequest : IRequest<TResponse>
{
public async Task<object> HandleAsync(object request)
{
return (await
}

protected abstract Task<TResponse> HandleAsync(TRequest request);
}

Then you can cast the handler to just IHandler, and cast the return value to TResponse. This works; however, I haven't benchmarked it yet. It should be fast because there's no reflection? However, there is a delegate pattern going on which is an extra function call. Especially, since it uses await, it could be somewhat slow.
Should be heaps faster than reflection, though

barmetler
Автор

Having spent a lot of time in large project doing a lot of mediation style processing - how would you break down more of the reflection problem presented through the method invocation? I can see cause for function delegates, but I don’t see how that then plays out when the handlers are scoped or transient without then using reflection. Very interested in deep diving more to learn about what can and can’t be achieved.

kyokosaya
Автор

Hi Nick, this is great. What do you think about using this sort of library with repository pattern?

stefangabor
Автор

Great video Nick. Thanks for the Help. :) Nick I have one question Might Sound Silly but bothering me a lot in fact. Why .Net core doesn't support Sync context and what exactly It mean ??

kiranshetty
Автор

My, my intuition says all this reflection and all this weird way of finding Irequest and Ihandler, it says its all wrong, or too much.But i gotta deal with the fact that C# can handle it.
It makes me want to pull my hair out but its intereseting on the other hand.

ZeroSleap
Автор

Hi Nick. Good video. What IDE are you using?

ianrandell
Автор

I am using mediator for all http server access, RPC is no bueno...

aegis_helion
Автор

Liked this before I've even watched.

Eamo-
Автор

I just discovered I have been watching your videos without liking and sharing. I apologize 😒

chimaokoli