Dependency injection is a design pattern which is used for developing loosely coupled applications.If one class depends on another class then this creates tight coupling between the classes.Dependency injection is used make application components such as classes loosely coupled.Dependency injection containers are used to automatically provide the dependencies to the required classes.
For example in the following code ,classEmp creates an instance of ClassOrg
class classEmp { public classEmp getDetails(int empId) { classOrg org=new ClassOrg(); org.getEmpDetails(empId); } }
In the above code there is tigh coupling between the classes classEmp and ClassOrg.This is because classEmp creates an instance of ClassOrg.The consequence of this is that if there is change in class ClassOrg this would effect class classEmp.If the signature of the getEmpDetails method is changed this would impact class classEmp.
To solve this dependency injection is used.To supply the dependencies such as the classOrg above dependency injection containers are used.
StrucutureMap is one of the commonly used Dependency injection containers.To use StructureMap you need to configure the container.This means registering the dependencies in the container.This allows StructureMap to find the dependencies when required.
For example you register the dependencies in the container as:
var container = new Container(); container.Configure(x => { x.For<IStudent>().Use<Student>(); x.For<ICollege>().Use<College>(); });
Above code registers two dependencies in the container.This means that when client asks for type IFoo then provide Foo and when the client asks for a type IBar then supply Bar instead.
Now if you define a class as
public class Student { public string Name{get;set;} } public class Details { IStudent student; public Details(IStudent student) { this.student=student; } }
then structuremap will automatically inject the appropriate constructor dependencies when the details class is called.
StructureMap is an open source framework commonly used with .NET.
If you want to used StructureMap in your application then you can follow the below steps:-
1.Add a reference to StructureMap.You can execute the following command at the package manager console to add a reference to nuget:
install-package structuremap
2.You need to initialize StructureMap before it can inject dependencies.You can initialize StructureMap as:
var container = new Container(); container.Configure(x => { x.For<IStudent>().Use<Student>(); });
3.Now you can provide dependencies where required.Structuremap will autmatically create objects of the appropriate types and inject when required as in this example:
public Details(IStudent student) { this.student=student; }
Here StructureMap will automatically resolve the dependency for the interface IStudent .It will create an object of Student type and pass it to the Details method.
You can also explicitly retrieve an object from the container as:
var obj = container.GetInstance<IStudent>();
Leave a Reply