How to avoid cyclic behaviour with Castle Windsor's CollectionResolver?

I know Unity but I believe your problem is a general one and relates to all DI systems. Most DI tools have the facility to explicitly name your dependency. This is particularly useful if you are implementing the same interface.

I know Unity but I believe your problem is a general one and relates to all DI systems. Most DI tools have the facility to explicitly name your dependency. This is particularly useful if you are implementing the same interface.So I believe your problem is not so much circular-ness (if there is such a word) of dependency but the multiple-ness of it.

As such I would explicitly name the dependencies as below: windsor. Register( Component .For() . ImplementedBy("CompositeService"), Component .For() .

ImplementedBy("ConcreteService1"), Component .For() . ImplementedBy("ConcreteService2") ); But I cannot see how your DI framework can handle the constructor of your CompositeService. Params is a tough one.

Here is what I would do: 1) I remove params 2) I change constructor to IEnumerable decoratedServices 3) I create another class and implement IEnumerable which is just a factory will return to me list of defined types which I initialise using their names.

Damian explicitly stated "I'd like to do this without explicitly specifying the dependencies. " The fact that Windsor reports cycle has nothing to do with params and is just a safety fuse. In other words Windsor is being more strict than it probably should.

– Krzysztof Koźmic Sep 17 '10 at 12:29 I cannot see how you can do that without explicitly specifying it. But the point of my solution is that such definition is moved to a factory. At the end of the day someone somewhere has to have the list of these names - at least it is how it is in the Unity world.

– Aliostad Sep 17 '10 at 12:37 Thanks for your reply, @Aliostad. In this question, I'm relying on Windsor to resolve the composite's dependencies by type. Windsor is pretty clever though - it already knows how to prevent cyclic dependencies when implementing the Decorator Pattern (i.e.

A single dependency of the same type); my question is really about getting the same behaviour when resolving multiple services rather than just one. See the workaround in my answer below. – Damian Powell Sep 17 '10 at 14:37 you'd be surprised :) – Krzysztof Koźmic Sep 17 '10 at 0:05.

Here's my current workaround. I'm not overly familiar with Windsor's internals so there could be glaring errors with this solution so use it at your own risk! Public class NonCyclicCollectionResolver : ISubDependencyResolver { private readonly IKernel kernel; private readonly bool allowEmptyCollections; public NonCyclicCollectionResolver( IKernel kernel, bool allowEmptyCollections = false ) { this.

Kernel = kernel; this. AllowEmptyCollections = allowEmptyCollections; } public bool CanResolve( CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency ) { if (dependency. TargetType == null) return false; var itemType = dependency.TargetType.

GetCompatibileArrayItemType(); if (itemType == null) return false; if (!allowEmptyCollections) { return GetOtherHandlers(itemType, model. Name).Any(); } return true; } public object Resolve( CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency ) { var itemType = dependency.TargetType. GetCompatibileArrayItemType(); var handlers = GetOtherHandlers(itemType, model.Name); var resolved = handlers .

Select(h => kernel. Resolve(h.ComponentModel.Name, itemType)) .ToArray(); var components = Array. CreateInstance(itemType, resolved.

Length); resolved. CopyTo(components, 0); return components; } private IEnumerable GetOtherHandlers( Type serviceType, string thisComponentName ) { return kernel . GetHandlers(serviceType) .

Where(h => h.ComponentModel.Name! = thisComponentName); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions