Andrew
Andrew

Reputation: 3560

Why can my WindsorContainer not resolve IWindsorContainer?

This example is not using best practice by using IServiceLocator to wrap my Container but it has me a bit baffled.

I thought that the WindsorContainer could automatically resolve itself as IWindsorContainer?

var container = new WindsorContainer();

container.Register(Component.For<INeedWindsorContainer>()
    .ImplementedBy<GiveMeWindsorContainer>()
    .LifeStyle.Singleton);

Implementation of INeedWindsorContainer:

public class GiveMeWindsorContainer : INeedWindsorContainer
{
    IWindsorContainer _container;

    public GiveMeWindsorContainer(IWindsorContainer container)
    {
        _container = container;
    }
}

This could however does not work, because WindsorContainer does not know how to resolve IWindsorContainer!

Of course the immediate solution I came up with was:

var container = new WindsorContainer();

container.Register(
     Component.For<IWindsorContainer>()
        .Instance(container)
        .LifeStyle.Singleton,
     Component.For<INeedWindsorContainer>()
        .ImplementedBy<GiveMeWindsorContainer>()
        .LifeStyle.Singleton);

However this seems a bit odd to me, am I doing something wrong?

Upvotes: 3

Views: 2520

Answers (1)

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99730

Windsor can automatically resolve IKernel (the container core, which has most of the functions you'll need).

As you said yourself, it's generally not a good practice to pass the container itself. Most of the times you should use a factory, perhaps through the typed factory facility.

Upvotes: 7

Related Questions