June 7, 2013

Dependency Injection of Multiple Objects Same Interface using StructureMap


I have been reading Mark Seemann's excellent book on Dependency Injection & applying what I have been learning to StructureMap.

Often times I have multiple objects that implement the same interface. In the past I may have used an abstract factory to create the instance I needed at a given time based on some sort of key or identifier like so:





However I was interested in using an IOC container like StructureMap to get rid of the ugly switch statement with the factory object. I did so by registering each object in my container and giving it a name with the .Named() method like so:

If you would like to see more please check out a simple example project over at my GitHub called MultipleObjectDemo.

Fun With Strongly Typed Enums!

Nobody likes using plain old boring Enums in C# these days! So why not try using a cool strongly typed Enum instead.