In autoFac, I can register multiple implementation of an interface. When autofac instantiates my object, all instances are passed to the constructor.
From autofac’s documentation: here
For example, when Autofac is injecting a constructor parameter of type
IEnumerable it will not look for a component that supplies
IEnumerable. Instead, the container will find all
implementations of ITask and inject all of them.
Is this functionality available in StructureMap?
For my classes:
public interface IFoo
{
}
public class Foo1 : IFoo
{
}
public class Foo2 : IFoo
{
}
public class UsingFoo
{
public UsingFoo(IEnumerable<IFoo> allFoos)
{
foreach (var foo in allFoos)
{
}
}
}
How do I register my implementations, so that when UsingFoo is instantiated, the constructor will be passed all implementations of IFoo?
In StructureMap you can do:
That will register all types of
IFooThen when you resolve
UsingFoo, they will be injected.Edit:
I just quickly wrote this up in a console app:
Edit:
You made me doubt myself, so I double checked.
It works fine.
Here’s a working example I quickly wrote in a console app:
The output is: