StructureMap and injecting IEnumerable<T>

Posted by GiddyUpHorsey on Stack Overflow See other posts from Stack Overflow or by GiddyUpHorsey
Published on 2011-01-12T04:49:29Z Indexed on 2011/01/12 4:53 UTC
Read the original article Hit count: 300

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?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about dependency-injection