I want to be able to retrieve all instances of a particular interface which have been configured in Structuremap under a given name.
I'm already using Structuremap within the project for constructor injection and it seems like the kind of thing I ought to be able to accomplish with it.
For example: I have an interface IProcessor which many concrete classes implement. Each concrete class has a name, with some classes sharing the same name. For a given name I want to be able to return all concrete instances of IProcessor.
IEnumerable<IProcessor> processors = ProcessorsByName("foo");
ProcessorsByName(string name)
{
// Some Structuremap magic here
// Ideally something like (which I know doesn't exist)
return ObjectFactory.GetAllNamedInstances<IProcessor>(name);
}
I know that I could call ObjectFactory.GetAllInstances<IProcessor>() and then enumerate through that testing each one to see if it's the right name, but that sounds wasteful.
// Possible solution
interface IProcessor
{
string Name { get; }
}
IEnumerable<IProcessor> processors = ProcessorsByName("foo");
ProcessorsByName(string name)
{
// Having a dog and barking yourself
return ObjectFactory.GetAllInstances<IProcessor>().Where(p => p.Name == name);
}