I am using Castle Windsor 2.5 in my application. I have a service which is a composite that acts as a distributor for objects that implement the same interface.
public interface IService { void DoStuff(string someArg); }
public class ConcreteService1 : IService {
public void DoStuff(string someArg) { }
}
public class ConcreteService2 : IService {
public void DoStuff(string someArg) { }
}
public class CompositeService : List<IService>, IService
{
private readonly IService[] decoratedServices;
CompositeService(params IService[] decoratedServices) {
this.decoratedServices = decoratedServices;
}
public void DoStuff(string someArg) {
foreach (var service in decoratedServices) {
service.DoStuff(someArg);
}
}
}
The problem I have is that using config such as that shown below causes Windsor to report that "A cycle was detected when trying to resolve a dependency."
windsor.Register(
Component
.For<IService>()
.ImplementedBy<CompositeService>(),
Component
.For<IService>()
.ImplementedBy<ConcreteService1>(),
Component
.For<IService>()
.ImplementedBy<ConcreteService2>()
);
This cyclic behaviour seems to be at odds with the standard (non-collection based) behaviour that can be used to implenent the decorator pattern, where the component being resolved is ignored and the next registered component that implements the same interface is used instead.
So what I'd like to know is whether or not there is a way to make Windsor exclude the CompositeService
component when it's resolving the IService
services for the decoratedServices
property. The decoratedServices
property should contain two items: an instance of ConcreteService1
, and an instance of ConcreteService2
. I'd like to do this without explicitly specifying the dependencies.