0

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.

CleverPatrick
  • 9,261
  • 5
  • 63
  • 86

1 Answers1

3

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.

Crixo
  • 3,060
  • 1
  • 24
  • 32
  • Have not looked at any of the facilities that come with Windsor. Looking at the sample, won't I have the same issue with needing to reference IKernel that I currently have needing to reference IWindsorContainer? – CleverPatrick Jun 15 '12 at 01:47
  • ...in this scenario, I literally want to loop through all the different IRuleDataDefinition types that are registered with Windsor. It doesn't seem like a typed factory is necessarily what I want. – CleverPatrick Jun 15 '12 at 01:56
  • Sorry but having a explicit reference to the container/kernel(In reality typed factory does that for you, but hidden from the coding point of view) is a very bad violation of IoC container principle. I wonder if you are attempting to do somenthing wrong designing wise. Pattern violation sounds often as a sign of designing mistake. – Crixo Jun 15 '12 at 16:22
  • It is very possible. I am still new to the IoC thing. It doesn't *seem* like it would be wrong, though (after all ResolveAll is a built-in method.) And, after working with it more, I realized that I really have to have the entire Dependency Chain registered and resolved via Windsor to have one of the items lower down work with Windsor. Once I did that, what I was trying worked. So, given that, I bet the TypedFactory would work as well (and is likely a better way to do it.) – CleverPatrick Jun 15 '12 at 19:10
  • Ok... so TypedFactory worked perfectly. Thanks! The real issue was, apparently, needing the entire dependency chain to be part of Windsor. – CleverPatrick Jun 15 '12 at 19:28
  • you are welcome. Windsor FTW... the mature breed of IoC container – Crixo Jun 16 '12 at 17:48