Introduction to Dependency Injection Part 1

Pritom Purkayasta
3 min readJan 19, 2020

--

Nowadays dependency injection design pattern is one of the most frequent used and heard patterns that are used in any real time application. So the real question is what is this damn thing and why it should exist in the first place ?

To understand what is dependency injection, we have first understand what is tight/loose coupling in software design !

Tight/Loose Coupling:

Tight coupling means that your classess and instances are dependent on each other. So if you change class that is dependent on another class you have to change it from the chain where it is not dependent anymore. In that case if your application is small enough it doesn’t even matter but if your application has over 50 classes or more then it is a huge pain in the back. It is itself a bad design for any enterprise level app.

Loosly coupling means that your classes or instances are totally independent of each other. You can change however whenever you want and it will not have any major impact on another instances or classes. This nature allows developer to bring changes easily and maintain complex application.

As we live in the object oriented world dependency injection became a part of the standard developement process. So I am going to use C# to make us understand how to do it. I have created a standard console application and here is my folder structure as well as I will update structure as we start building.

startproject.png

There are three different types of DI pattern that can be implemented.

  1. Constructor Injection.
  2. Property Injection.
  3. Method Injection.

Constructor Injection:

To begin this journey we need to have data available. I have created a model class along side with some dummy data.

In order to access the data model I need to create a data access layer. But in order to restrain access to our class we need to have a interface that will inherited by that data access layer class. I have named it as IDataLayer and DataLayer.

Inside the interface we declare the method definition and we implement it to the derived class. The derived class (which is our case DataLayer) will be responsible to bring the data from database (in our case I have created the static method inside of our model class :3).

Now its time to create our bussiness logic layer class (which I named as ConstructorBased). The sole purpose of this class is to inject our DataLayer interface. Now this class will accept any concreate class instance which implements that interface.

Now its time to call our main method. In order to get it work we have to call our bussiness logic class (ConstructorBased.cs) and pass a DataLayer type of instance. Instead of creating the instance, we are passing it as a parameter to the constructor of ConstructorBased class.

This is called dependency injection through the constructor.

You can find this entire repository here.

Click to see the part 2 here.

--

--

Pritom Purkayasta
Pritom Purkayasta

Written by Pritom Purkayasta

---------------- NOT AN AI AGENT -------------------

No responses yet