IWindsorContainer as parameter for class

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

I shot this with Unity, but when I try to do the same with the Windsor container, it tells me that the IWindsorContainer is not registered in the container.

I don’t think I can just register IWindsorContainer => WindsorContainer, because it will force the container to create a new (or another) instance itself for transfer to my class, and this instance will not have all my other types registered with it. I also don’t see a way to build a container, register all types in it, and then register this instance myself in relation to IWindsorContainer - all registration methods accept only types for maintenance and implementation - they are never a specific concrete instance.

+7
inversion-of-control castle-windsor
source share
3 answers

As a rule, you do not want to enter the container into your application components.

See these questions (this question almost duplicates them):

  • Using IoC containers; especially windsor
  • NInject: Where do you keep your kernel link?
  • IoC, where do you put the container?

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

 container.Register(Component.For<IWindsorContainer>().Instance(container)); 
+8
source share

As a rule, as Mausch said, think twice before handing your container to your component. Do you really need it to access the container?

To pull dependencies from a container, use typed factories .

+1
source share

I was tempted to do this recently. I wanted to be able to create multiple instances of the service throughout the life of the object. Registering a factory will be better in that the need for dependency is more clear. But setting up a factory for the service seemed a pain.

In the end, I used something like code in this article: http://mikehadlow.blogspot.com/2010/01/10-advanced-windsor-tricks-1a-delegate.html . Suppose you have a class that should instantiate IService. This article describes a method that allows your class to be dependent on Func, and you will automatically get a factory specific to this class.

0
source share

All Articles