10

Via Windsor I register multiple implementation types to a single interface type :

public class WindsorInstaller : IWindsorInstaller
  {
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
      container.Register(
        Component.For<IMyType>()
          .ImplementedBy<MyTypeClass1>()
          .Named("MyTypeClass1")
          .LifestyleTransient());

      container.Register(
        Component.For<IMyType>()
          .ImplementedBy<MyTypeClass2>()
          .Named("MyTypeClass2")
          .LifestyleTransient());
    }
  }
}

and I have a consuming class :

public class MyConsumingClass : IMyConsumingClass
  {
    private readonly IList<IMyType> _myObjects;

    public MyConsumingClass(IEnumerable<IMyType> myObjects)
    {
      _myObjects = myObjects.ToList();
    }
}   

however at runtime I receive following exception :

Can't create component 'MyConsumingClass' as it has dependencies to be satisfied. 'MyConsumingClass' is waiting for the following dependencies: - 'System.Collections.Generic.IEnumerable`1[[IMyType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.
BaltoStar
  • 8,165
  • 17
  • 59
  • 91

3 Answers3

23

I think you may need to add a CollectionResolver to your container.

Try:

kernel.Resolver.AddSubResolver(new CollectionResolver(kernel));
Philip C
  • 1,819
  • 28
  • 50
shenku
  • 11,969
  • 12
  • 64
  • 118
8

I like @shenku's answer and I'd add this note as a comment, but I can't since I'm a newb. (<50 reputation). Don't mark me as an answer! :)

When you add the sub resolver consider adding the optional parameter for allowing empty collections to resolve correctly as well.

kernel.Resolver.AddSubResolver(new CollectionResolver(kernel, true));

Dan
  • 1,805
  • 2
  • 18
  • 21
Michael Socha
  • 1,748
  • 1
  • 16
  • 17
-1

What should be the value of IEnumerable myObjects ?

If it is evertime the same, you can register it like this:

ICollection<IMyType> someCollection= new Collection<IMyType>();
// add some elements

container.Register(Component.For<IEnumerable<IMyType>>().Instance(collection));

If it's changed in runtime, you should resolve it like:

ICollection<IMyType> someCollection= new Collection<IMyType>();
// add some elements
var myConsumingClass = container.Resolve<MyConsumingClass>(new {myObjects = someCollection});

BTW, if this code is not inside your entry point, you should use Typed Factory Facility insdead of container.resolve

shudima
  • 460
  • 1
  • 5
  • 9