Search code examples
castle-windsorwindsor-3.0

Is Windsor able to have collection of concrete class' instances registered?


If I have interface and implementing classes like

public interface IA {}
public class X : IA {}
public class Y : IA {}

then this registering is fine

var w = new WindsorContainer();
w.Register(Component.For<IA>().ImplementedBy<X>());
w.Register(Component.For<IA>().ImplementedBy<Y>());

same as

var w = new WindsorContainer();
w.Register(Component.For<IA>().Instance(new X()));
w.Register(Component.For<IA>().Instance(new Y()));

But if I try to register concrete classes as

var w = new WindsorContainer();
var x1 = new X();
var x2 = new X();
w.Register(Component.For<X>().Instance(x1));
w.Register(Component.For<X>().Instance(x2));

it throws an exception: Castle.MicroKernel.ComponentRegistrationException : Component X could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.

If it is intended limitation - why? Is there any way to achieve collection resolve without adding interface that is not always necessary?


Solution

  • Did you want to modify the existing component instead? If not, make sure you specify a unique name.

    Component.For<X>().Instance(x1).Named("x1")