StructureMap and injecting IEnumerable<T>
- by GiddyUpHorsey
I'm new to StructureMap and have some existing code that I'm working with that uses StructureMap 2.5.4.
There is a class that is constructed using StructureMap that has a constructor that takes IEnumerable<TCar> as a parameter.
The registry has the following code.
Scan(x =>
{
x.TheCallingAssembly();
x.WithDefaultConventions();
x.AddAllTypesOf<ICar>();
}
);
ForRequestedType<IEnumerable<ICar>>().TheDefault.Is.ConstructedBy(
x => ObjectFactory.GetAllInstances<ICar>());
I'm writing a unit test and have obtained a nested container off the ObjectFactory and have injected an instance using the Inject method. One of the instances of ICar should receive the injected type in its constructor. However it wasn't working and I tracked that down to the ObjectFactory.GetAllInstances() call which doesn't use my nested container.
How can I get this to work?
I also read about StructureMap autowiring arrays and IEnumerable instances but I couldn't get it to work.
Is there a better way to rewrite the above registry code so that an instance of IEnumerable<TCar> will be created and use the injected type from my nested container?