Let's start with a question,
What is Dependency Injection(DI)?Advantages of De-coupled object or classes:
DI is an architectural design pattern that helps us to convert (inject) all the dependent (couple) object into the independent (decouple) object means DI allow use to write loosely couple code and modules.
- We can unit test an individual class that is dependent on other class or DB using the mock object.
- We can change the implementation of service class at any time without breaking the host code.
- We can be achieved extendibility by minimum modification and maintainability.
- We can write loosely couple module.
- Constructor
- Properties
- Method
Tightly Coupled module:
In this example, I have taken the DAL, BAL, UI Layer example. In which these modules lightly depended on each other as given below.UI ➨BAL ➨DAL
DAL:
namespace DIDemo.Couple.DAL { public class SQLDAL { public void Save(string Name) { Console.WriteLine("student {0}:data saved into sql db”, Name); } } public class MYSQLDAL { public void Save(string Name) { Console.WriteLine("student {0}:data saved into mysql db”, Name); } } }
BAL:
namespace DIDemo.Couple.BAL { using DIDemo.Couple.DAL; public class StudentBAL { SQLDAL objSQLDAL; MYSQLDAL objMYSQLDAL; public void Save(string Name,int DBType) { //we can see that this class tightly cupled with //SQLDAL and MYSQLDAL class //save into SQL if (DBType==1) { objSQLDAL = new SQLDAL(); objSQLDAL.Save(Name); } //save into MYSQL else if (DBType ==2) { objMYSQLDAL = new MYSQLDAL(); objMYSQLDAL.Save(Name); } } } }
Test Program:
namespace DI.Coupled { using DIDemo.Couple.BAL; class Program { static void Main(string[] args) { //Create the instence of object StudentBAL studentObj = new StudentBAL(); //Save into SQL db studentObj.Save("Student-1", 1); //Save into MYSQL db studentObj.Save("Student-1", 2); Console.ReadLine(); } } }
Description:
We can see that DAL is tightly coupled with BAL that’s why the modification is very difficult means if in future client required to save data into Oracle database then we need to create another class in Dal and create an object of Oracle class into BAL and write another if else code for Oral to save.
Now we can convert these coupled codes into decoupled code using Interface as below.
Loosely Coupled module:
Example of Dependency injection on the constructor.DAL:
namespace DIDemo.ImpDeCouple.DAL { public class SQLDAL : IDAL { public void Save(string Name) { Console.WriteLine("student {0}:data saved into sql db", Name); } } public class MYSQLDAL : IDAL { public void Save(string Name) { Console.WriteLine("student {0}:data saved into mysql db", Name); } } public interface IDAL { void Save(string Name); } }
BAL:
namespace DIDemo.ImpDeCouple.BAL { using DIDemo.ImpDeCouple.DAL; public class StudentBAL { IDAL objDAL; //Object is injected by somewhere else by other at run time. public StudentBAL(IDAL IDAL) { objDAL = IDAL; } public void Save(string Name) { objDAL.Save(Name); } } }
Test Program:
namespace DI.ImpDeCouple { using DIDemo.ImpDeCouple.DAL; using DIDemo.ImpDeCouple.BAL; class Program { static void Main(string[] args) { Console.WriteLine("DECOUPLED Example"); //Create the instence of SQLDAL StudentBAL studentObj = new StudentBAL(new SQLDAL()); //Save into SQL db studentObj.Save("Student-1"); //Create the instence of MYSQLDAL StudentBAL studentObj = new StudentBAL(new MYSQLDAL()); //Save into SQL db studentObj.Save("Student-1"); Console.ReadLine(); } } }
Description:
we can see that BAL is Loosely coupled with DAL that’s we can modify the DAL class at any time because BAL expects the Interface into the constructor and instance of the class MYSQL and SQL will be provided by any third party in this call test class.
But object creation is still happed into the program class in UI to remove this we should use any third party tool that injects the instance at runtime.
DI can be resolved by any container like:
Unity, Ninject, Castle Windsor, Structure Map, Autofac and Sping.Net
Implement Dependency injection using Unity resolver:
We need to install Unity using NuGet package manage and modify the UI layer code and all the code remain same.
Test Program:
namespace DI.ImpDeCouple { using DIDemo.ImpDeCouple.DAL; using DIDemo.ImpDeCouple.BAL; using Unity; class Program { static void Main(string[] args) { //Create the instence of object IUnityContainer uc = new UnityContainer(); uc.RegisterType<studentbal>(); uc.RegisterType<idal sqldal="">(); uc.RegisterType<idal mysqldal="">(); StudentBAL studentObj = uc.Resolve<studentbal>(); //Save into SQL db studentObj.Save("Student-1"); Console.ReadLine(); } } }
Description:
We can see that whole module is loosely coupled and we can easy test and modify any module very easily by small modification.
So, Guys, this is all about Dependency Injection in C#.
I Hope in this post covered all the points about how can we make our code loosely coupled which will be helpful to understand the concept Dependency Injection.
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
What abt di in method?
ReplyDeleteYou can refer my new post on the given URL.
DeleteImplementation of Dependency Injection Pattern using Methods in C#