What is Dependency Injection and Implementation of Dependency Injection Pattern in C sharp
Dependency Injection
Dependency injection is a software design pattern that allows the removal of hard-coded dependencies and makes it possible to change them, whether at run-time or compile-time. It promotes loose coupling of components by passing dependencies to an object, rather than having an object instantiate its own dependencies.
What is Dependency Injection and Implementation of Dependency Injection Pattern in C#?
Sometime Classes in Object-oriented design have some dependent classes and sometime we have to the create structure containing components that are dependent upon other classes, known as dependencies. In such situations, you may decide to instantiate a class's dependencies from within the dependent class, using the new keyword.
There are many useful Dependency Injection Container tools as below.
Castle Windsor
StructureMap
Spring.NET
Autofac
Unity
MEF
Some more DI container frameworks are Ninject, Hiro, Funq and LinFu,OCInject. We will conver these in step by step in next posts
As an example, consider the code below. This defines a class, named "RechargeClient", that holds the details of recharge for any mobile and we can recharge any mobile no. We can set PhoneNo, Amount of recharge and provider name which we want to recharge and call methode “Rechage”. “Recharge” Method recharge phone number using “PayIntegra” object.
PayIntegra is class that call a third party web service to recharge phone number. Operation of PayIntegra class is out of scope in this post.
If we see in below code than we can see that “RechargeClient” is having dependency on “PayIntegra” object. This “objRecharge” is a dependency of “RechargeClient” and is initialised when an object is instantiated.
Public class PayIntegra { public string DoRecharge(string phoneNo,double amount,string providor) { return "Result for Recharge api based on parameters"; } } public class RechargeClient { PayIntegra objRecharge=new PayIntegra(); public string PhoneNo{get;set;} public double Amount{get;set;} public string Provider{get;set;} public string Recharge() { return objRecharge.DoRecharge(this.PhoneNo,this.Amount,this.Provider); } }
One of the key problems with the above code is the instantiation of the PayIntegra object from within the RechargeClient class. As the dependency is initialised within the containing class, the two classes are tightly coupled and we are doing all recharges from PayIntegra. But now we have to add a new API to recharge in project named “LapuOne”.
Public class LapuOne { public string DoRecharge(string phoneNo,double amount,string providor) { return"Result for Recharge api based on parameters"; } }
As object of PayIntegra is initiated inside RechargeClient so we have to change code if we want to use LapuOne class inside RechargeClient.
Dependency injection reduces the problem of tight coupling by removing the instantiation of dependencies from a dependent class. Instead, pre-constructed objects are passed to a dependent object at some time before they are required.
In this post we will show solution for above problem using dependency injection and we will consider below type of dependencyInjection.
Constructor Injection
Property Injection
Method Injection
Each provides a solution to class's dependencies using a different manner.
Complete solution can be found on http://dotneta2z.com


















