modesttree / Zenject

Dependency Injection Framework for Unity3D
MIT License
2.5k stars 274 forks source link

MediatR configuration #21

Open slimshader opened 5 years ago

slimshader commented 5 years ago

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.

Timmoth commented 3 years ago

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.

slimshader commented 3 years ago

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?

Timmoth commented 3 years ago

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().AsTransient();

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

//Setup each of your IRequestHandlers (3) _container.BindInterfacesAndSelfTo().AsTransient(); _container.BindInterfacesAndSelfTo().AsTransient();

slimshader commented 3 years ago

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 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

Timmoth commented 3 years ago

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!