Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windsor resolve IEnumerable<IMyType>

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.
like image 459
BaltoStar Avatar asked Mar 05 '14 02:03

BaltoStar


2 Answers

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

Try:

kernel.Resolver.AddSubResolver(new CollectionResolver(kernel));
like image 159
shenku Avatar answered Sep 17 '22 20:09

shenku


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));

like image 26
Michael Socha Avatar answered Sep 20 '22 20:09

Michael Socha