Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IWindsorContainer as a parameter to a class

I have a class that I want to have access to my IOC container (Windsor), however I don't want to keep a static IWindsorContainer property hanging around - I would prefer to have the container inject itself into any classes that require an IWindsorContainer as a constructor dependency.

I've pulled this off with Unity, but when I try the same thing with the Windsor container it tells me that IWindsorContainer is not registered with the container.

I don't think I can just register IWindsorContainer => WindsorContainer, because that will cause the container to create a new (or different) instance of itself to pass to my class, and that instance won't have all my other types registered with it. I also don't see a way to construct the container, register all the types in it, and then register that instance of itself against IWindsorContainer - all of the registration methods only take types for service and implementation - never an actual concrete instance.

like image 702
Terence Lewis Avatar asked Mar 23 '09 13:03

Terence Lewis


2 Answers

Generally you don't want to inject the container into your application components.

See these questions (this question is almost a duplicate of them):

  • Usage of IoC Containers; specifically Windsor
  • NInject: Where do you keep your reference to the Kernel?
  • IoC, Where do you put the container?

BTW: you get IKernel injection for free, and you can register IWindsorContainer:

container.Register(Component.For<IWindsorContainer>().Instance(container));
like image 169
Mauricio Scheffer Avatar answered Oct 24 '22 21:10

Mauricio Scheffer


Generally, like mausch said, think twice before you pass your container to your component. Do you really need it to have access to the container?

To pull dependencies from the container use typed factories.

like image 35
Krzysztof Kozmic Avatar answered Oct 24 '22 22:10

Krzysztof Kozmic