I've some ITask concretes types defines in my TaskRegistry:
public class TaskResigstry : Registry
{
public TaskResigstry()
{
ForRequestedType<IBootstrapperTask>().TheDefaultIsConcreteType<StartTasks>();
ForRequestedType<ITask>().TheDefaultIsConcreteType<FirstTask>();
ForRequestedType<ITask>().AddConcreteType<SecondTask>();
ForRequestedType<ITask>().AddConcreteType<ThirdTask>();
}
}
And my StartTasks
public class StartTasks : IBootstrapperTask
{
public StartTasks(ITask[] tasks)
{
foreach(var task in tasks)
{
task.Run();
}
}
}
How can i inject the ITask[] constructor parameter using StructureMap ?
Thanks.
If you want to inject an Array there's a method for that in the fluent interface...
ForRequestedType<IBootStrapperTask>().TheDefault.Is.OfConcreteType<StartTasks>()
.TheArrayOf<ITask>().Contains(
y => {
y.OfConcreteType<Task1>();
y.OfConcreteType<Task2>();
y.OfConcreteType<Task3>();
});
If you want to go down the route of an accepting an IEnumerable<T> in your constructor as far as I can see things start to become a little more complicated. You can specify and build the constructor argument like so:-
ForRequestedType<IBootStrapperTask>().TheDefault.Is.OfConcreteType<StartTasks>()
.CtorDependency<IEnumerable<ITask>>().Is(i => {
i.Is.ConstructedBy(c => {
return new List<ITask> {
c.GetInstance<Task1>(),
c.GetInstance<Task2>(),
c.GetInstance<Task3>()
};
});
});
If you want all the registered types you could make a custom IBuildInterceptor that accesses all the registered types via the CreateInstanceArray method on the BuildSession but I get the feeling I might be going down the wrong road there.
I'd love to be corrected that this is a lot easier :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With