CleverPatrick
CleverPatrick

Reputation: 9483

Castle Windsor Property Injection of IWindsorContainer

I am having problems injecting IWindsorContainer as a property.

I am using MVC3. I have created my own IView and ViewEngine. From within the View I dynamically build views based on the types registered in Windsor.

For one condition ( a "list" view) I want to display a list of all the IMyTypes registered with Windsor. In my global.asax I register the Windsor view like this:

container.Register(Component.For<IWindsorContainer>().Instance(container));

Then in my IView implementation, I declare a property like this:

public IWindsorContainer Container { get; set; }

The actual IView is in another component. When I get to the Render method, I want to do this:

IRuleDataDefinition[] ruleDatas = Container.ResolveAll<IRuleDataDefinition>();

But "Container" is always null. Is it because of the way I am creating the IView (I am just using new, it isn't registered with Windsor?) Is it something with the IWindsorContainer itself? Or do I just have everything wrong?

I've also read other people that say "if you are using Container.Resolve you are probably doing it wrong." So if I am doing this wrong, please let me know.


EDIT

Perhaps a better way to phrase the question is: What is the best way to do the equivalent of container.ResolveAll() when you don't have a reference to the container? I need to loop through all registered versions of IMyType.


EDIT THE 2ND

I got it to work by using Windsor for the entire dependency chain, which, of course, is what you are supposed to do, I learned.

Upvotes: 0

Views: 1093

Answers (1)

Crixo
Crixo

Reputation: 3070

You should not have a dependency on the Container... that's an anti-pattern and it smells like a service locator. What about a typed factory instead? Your typed factory may return a list of given components based on a common interface.

Upvotes: 3

Related Questions