Zenject icon indicating copy to clipboard operation
Zenject copied to clipboard

MediatR configuration

Open slimshader opened this issue 6 years ago • 5 comments

Hey, how would MediatR Container configuration look for Extenject? I lookned into Ninject example (as I recall Zenject/Extenject) were based on it but it doesnt really help much.

slimshader avatar Jul 05 '19 09:07 slimshader

For those who stumble across this: _container.BindInterfacesAndSelfTo<Mediator>().AsTransient(); _container.BindInterfacesAndSelfTo<MediatrPassThroughHandler>().AsTransient(); _container.Bind<ServiceFactory>().FromMethod(ctx => (t) => ctx.Container.TryResolve(t)).AsTransient(); Worked for me.

Timmoth avatar Oct 17 '20 15:10 Timmoth

For those who stumble across this: _container.BindInterfacesAndSelfTo<Mediator>().AsTransient(); _container.BindInterfacesAndSelfTo<MediatrPassThroughHandler>().AsTransient(); _container.Bind<ServiceFactory>().FromMethod(ctx => (t) => ctx.Container.TryResolve(t)).AsTransient(); Worked for me.

What is MediatrPassThroughHandler?

slimshader avatar Oct 19 '20 16:10 slimshader

Sorry I should have explained

From the Mediatr Wiki:

MediatR has no dependencies. You will need to configure a single factory delegate (2), used to instantiate all handlers (3), pipeline behaviors, and pre/post-processors.

You will need to configure two dependencies: first, the mediator itself (1). The other dependency is the factory delegate, ServiceFactory (2).


//Setup the Mediator (1)
_container.BindInterfacesAndSelfTo<Mediator>().AsTransient();

//Setup the ServiceFactory (2)
 _container.Bind<ServiceFactory>().FromMethod(ctx => (t) => ctx.Container.TryResolve(t)).AsTransient();

//Setup each of your IRequestHandlers (3)
 _container.BindInterfacesAndSelfTo<StartApplicationHandler>().AsTransient();
 _container.BindInterfacesAndSelfTo<ShowWindowHandler>().AsTransient();

Timmoth avatar Oct 19 '20 16:10 Timmoth

Sorry I should have explained

From the Mediatr Wiki:

MediatR has no dependencies. You will need to configure a single factory delegate (2), used to instantiate all handlers (3), pipeline behaviors, and pre/post-processors. You will need to configure two dependencies: first, the mediator itself (1). The other dependency is the factory delegate, ServiceFactory (2).

//Setup the Mediator (1)
_container.BindInterfacesAndSelfTo<Mediator>().AsTransient();

//Setup the ServiceFactory (2)
 _container.Bind<ServiceFactory>().FromMethod(ctx => (t) => ctx.Container.TryResolve(t)).AsTransient();

//Setup each of your IRequestHandlers (3)
 _container.BindInterfacesAndSelfTo<StartApplicationHandler>().AsTransient();
 _container.BindInterfacesAndSelfTo<ShowWindowHandler>().AsTransient();

Ah, this I know how to configure :) I was hoping for generic configuration as per MediatR container examples that resolve IRequestHandler<T> automatically without the need to bind them manually (and each time new one is added)

EDIT: I also think you want them .AsSingle(), especially Mediator and ServiceFactory bindings EDIT2: I would suggest to not do BindInterfacesAndSelfTo<> for handlers and use BindInterfacesTo<> (without the "Self") part, as this exposes implementation class while it should only be used via interfaces

slimshader avatar Oct 19 '20 16:10 slimshader

Honestly I gave up on trying to auto register my handlers, it is certainly NOT straight forward but with a little reflection magic its do-able. I will look into it again when I get the chance!

Timmoth avatar Oct 19 '20 17:10 Timmoth