Windsor Castle CollectionResolver: Why Does It Not Work on Resolve Calls?

I added CollectionResolver as a subtask of my Windsor kernel, and it will correctly inject dependency collections for allowed objects. That is, if I have

 class X { public X(IComponent[] components) { ... } } container.Register(/* lots of IComponents */); container.Register(Component.For<X>()); 

the components argument to the constructor is correctly built when I allow it

 container.Resolve<X>() 

but instead, I just wanted to get a list of the components themselves,

 container.Resolve<IComponent[]>() 

I get a ComponentNotFound exception complaining that I have not registered any components for IComponent[] . I find this asymmetry to be contradictory, because I'm not sure why the kernel should act differently when it resolves the dependencies found in the constructors / properties, compared to when they resolve the dependencies that its user would like to resolve .

+7
source share
2 answers

The explicit division for Resolve / ResolveAll is explained by the internal and uninteresting implementation details in the container. The collection replicator is an auxiliary depenendcy , and as such it works only for dependencies.

I agree that this is not very intuitive. Feel free to register your ticket in the Windsor event log.

+8
source

I assume that the reason for this behavior is that Castle uses the collection / array notation in the constructor or in the injection of properties as an indicator that all implementations should be returned to the collection. Otherwise, you will need a special interface or attribute to say so. And then you have to explicitly refer to Castle assemblies everywhere.

However, when resolving directly from the container, you are actually specifying the type to be resolved, and you can be more expressive (using the API), and you can explicitly call ResolveAll . Therefore, I assume that it was a compromise in design. Implementing IoC containers is not so simple because you are often connected by language constructs.

0
source

All Articles