
Dependency Injection / DI
- By: Waleed Ur Rehman
- January 29, 2023
In modern programming and architecture dependency injection is key player. While conducting many interview I noticed that most of the people don't know actually what is DI and what can be achieved through DI.
Before explaining what is dependency injection Let me explain what is dependency and how we can overcome its effects while working with our architecture let’s start with dependency.
What is dependency in Software Development / Software Component / Libraries?
Actually this is very simple concept and is very useful during designing the software architecture. Like we can see in any software that each component need some other component of software so this is called dependency. Let’s see through example
When we start building a software or application we create it in layers like there might be three layers or as many as per need. So let’s say there is a “Business Logic Layer (BLL)” and a “Data Access Layer (DAL)” now BLL want some data from database so BLL have to call some methods that are in DAL. So BLL is dependent on DAL.
Is Dependency good or bad?
Let’s see if it is bad or good. Well I think dependency is not good in most of cases like if we have to change something in our code and it is already using in some other component or layer then we might break something while making change.
If we are not using proper way for handling dependency, we can’t introduce abstraction between two layers or components. Let’s take an example.
Let’s say we are creating a software that can read data from MSSQL. And now there is another requirement that client want to configure MongoDB as well. So while changing in our DAL layer we can break existing functionality.
Can we remove dependency from our code?
No. We can’t remove dependency from our code but good news is we can write code in such a way so our component might be less dependent on each other this is called Loosely Coupled. Before we move forward let me introduce some terms.
- Loosely Coupled: two or more than two components are loosely coupled if they are dependent on each other but if you change in any of one you won’t break any thing we can achieve this using abstraction.
- Tightly Coupled: if two or more than two components are dependent on each other in such a way if you change in any one component then we have change in other component as well.
How to write Loosely-Coupled code?
For creating loosely-coupled component or layers we have to create some type of abstraction between them and most common way for that is “Interfaces”.
What is interface?
Interface is a special type through which we can force any class to implement some methods we can’t create object of an interface but we can use it for referring or holding object reference of that class which is implementing that interface. Let’s take an example.
Let’s say there are two classes in our code one for querying from MSSQL and other from MongoDB and I want same methods for both class so I can create an interface in that interface there will be only methods name that I want in both classes no I will inherit both classes from this interface. So I have to give implementation of that methods in both classes otherwise there will be compile time error.
How interface will make our application loosely-coupled?
Well now in our other components we will not use our class variables instead we will use our interface variables that will refer object from that classes. Let’s say first we were using MSSQL class object refer by interface variable now I can just change MSSQL class object to MongoDB class object our component code don’t know about these classes it only knows the interface and its method now in future we can add support of Oracle or MySql using that interface.
Now we can discuss what is dependency injection.
What is Dependency injection?
Let’s say there is a component A that is dependent on some other component B and so while writing my code I have to resolve that dependency so that component A can complete its work. Meaning we have to provide some mechanism that will provide or inject what component need is. Let’s take a simple example.
There are two classes class1 and class2. And class1 is dependent on class2 so whenever I will create class1’s object I have to provide class2 object to class1 and for that we have to create some mechanism through which we will pass class2 object to class1. This mechanism is called dependency resolver.
What is Dependency Resolver?
Usually we have a container or mechanism were we register our dependencies and whenever we need any dependent object we just as to it.
Type of Dependency Injection
There are 3 types of dependency injection
- Constructer Injection: When we inject dependency through Constructer is called Constructer Injection. (commonly used)
- Method Injection: When dependency is injected by method is called Method Injection.
- Property Injection: When dependency is injected by any property is called Property Injection.
Comments / 0