In the previous article, we discussed what Dependency Injection and Inversion of Control are and also discussed how DI is handled in projects without containers. Then we looked at a simple way to handle the dependencies by configuring the DLL in the application configuration and injecting that into our code without much effort.
Here first we will discuss how to use a StructureMap to do that. This article is not going to explain everything supported by a StructureMap (SM). This article is for understanding the basics of the use of SM for injecting dependencies.
A brief introduction to StructureMap
StructureMap is a DI/IOC tool exclusively for .NET programming. It helps in achieving looser coupling among classes and their dependencies, improve the testability of a class structure, and provide general flexibility mechanisms.
From StructureMap site
Consider using StructureMap if you:
- Require significant extensibility
- Simply want a generic configuration tool
- Want to support multiple deployment configurations
- Are using a Test-Driven Development philosophy or want to largely automate testing
- Want to isolate a troublesome subsystem or provide smooth migration paths away from legacy interfaces
- Need a great deal of configurable properties or plugin hot spots
Do not use StructureMap if an application or process requires a little flexibility. The abstraction and indirection afforded by StructureMap is unnecessary and even harmful in simpler systems or processes.
IOC implementation with StructureMap
Now let us see how to accomplish that using StructureMap. This is open source software available at:
http://sourceforge.net/projects/structuremap/. IOC is going to help you to unit test your code without a total implementation of your application.
You only need the DLL "StructureMap.dll" for this example. Add the reference to your project.
A simple implementation of StructureMap in our code copied from the StructureMap site.
In our example we always want any object that consumes "IClassB" to be given a concrete object of ClassB. To implement that, we need to add it to the ObjectFactory of StructureMap.
I am going to create a new static class that has a method to initialize the Object Factory as below:
public static class ContainerBootstrapper
{
public static void BootstrapStructureMap()
{
// Initialize the static ObjectFactory container
ObjectFactory.Initialize(x =>
{
x.ForRequestedType<IClassB>().TheDefaultIsConcreteType<ClassB>();
});
}
}
Now our program class looks like this:
static void Main(string[] args)
{
IClassA classA=new ClassA();//creating instance of class A
ContainerBootstrapper.BootstrapStructureMap();
var classB = StructureMap.ObjectFactory.GetInstance<IClassB>();
//assign the instance to the classB property of class A
classA.ClassB = classB;
//Call the method
classA.DoSomethingFromClassB();
Console.Read();
}

Manish SharmaPosted Aug 19, 2012, 3:09 AM
Thank you for providing information about Injection Techniques with Implementing StructureMap. It will be helpful to know about Injection Techniques.