Blankman
Blankman

Reputation: 266988

How to make Castle Windsor container available in other classes etc?

My global.asax wires up all my castle registrations etc.

What if I need to ask Windsor to resolve an interface directly (other than having it autowire using property injection etc. ?

Is it possible to do:

container.Resolve<ISomeThing>();

My global.asax.cs has:

 private static IWindsorContainer _container = new WindsorContainer();

 private static void BootstrapContainer()
 {

        _container = new WindsorContainer()
            .AddFacility<TypedFactoryFacility>()
            .Install(FromAssembly.This());

        var controllerFactory = new WindsorControllerFactory(_container.Kernel);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
 }

I'm a bit confused how I can do this, and what kind of scope this access to Windsor will have? (I'm assuming it will only work in my asp.net application classes??)

Upvotes: 0

Views: 2683

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could make the container public and then access it from other classes but note that this is not good practice and defeats the purpose of inversion of control. The whole idea of IOC is that classes should not be responsible for fetching their dependent objects, they should be passed from the outside. And thus if you want to follow good practices the entire wiring of the DI container should be constrained to a single location.

Upvotes: 1

Related Questions