Given the following interface:
public interface IMyProcessor
{
void Process();
}
I'd like to be able to register multiple implementations and have my DI container inject an enumerable of them into a class like this:
public class MyProcessorLibrary
{
private readonly IMyProcessor[] _processors;
public MyProcessingThing(IMyProcessor[] processors)
{
this._processors = processors;
}
public void ProcessAll()
{
foreach (var processor in this._processors)
{
processor.Process();
}
}
}
Is this possible? My current implementation of MyProcessorLibrary
looks up all the IMyProcessor
implementations statically, but I'd rather do it through a container if I can. I'm using Unity, but am curious if other containers support it.
Edit:
Thanks for the answers so far; to be clear I want to inject MyProcessorLibrary
into another class, and have it built as part of the wiring up of a tree of dependent objects, e.g.
public class MyProcessorRepository
{
public MyProcessorRepository(MyProcessorLibrary processorLibrary)
{
}
}
public class MyProcessorService
{
public MyProcessorService(MyProcessorRepository processorRepository)
{
}
}
var container = new UnityContainer();
// Register a bunch of IMyProcessors...
var service = container.Resolve<MyProcessorService>();