Explain Dependency Injection to me like I'm a 5-year-old

Explain Dependency Injection to me like I'm a 5-year-old

ยท

3 min read

Table Of Content

  • Introduction

  • Definition Of Terms

  • Explanation Of Dependency Injection

  • Pros Of Dependency Injection

  • Cons Of Dependency Injection

Introduction

This article is what I learned from researching and understanding what Dependency Injection is and I'm so excited to share my knowledge with you.

Recently, I started building Backend Applications using NestJs Framework. There was one feature about NestJs that intrigued me and confused me at the same time and that was DEPENDENCY INJECTION. So, I embarked on the journey of trying to understand what it meant and how to use it better. In the next section, we would look at defining key terms.

Definition Of Terms

Key terms to be defined are DEPENDENCY and INJECTION

Dependency: something or someone that depends on something/someone else. In software engineering, it is an object or class that depends on another object's functionality.

Injection: An act of administering or injecting a liquid. In Software Engineering, it is said to be when an entity is inserted into another entity

Let's jump into explaining the target of this article, which is Dependency Injection a.k.a. "DI" in the next section.

Explanation Of Dependency Injection(DI)

In the previous section, we defined what Dependency and Injection are.

Now, let's look at an illustration to broaden our imaginative minds.

Let's say we have three(3) classes: Classes A, B, and C.

Class A has a functionality/method that is needed by Class C. For us to be able to use the functionality from Class A in Class C, we would need to instantiate or create an object of Class A and use it in Class C. Hmmm...How about we give that job of creating the object(s) of Class A to Class B and giving it to Class C? ๐Ÿค”

We can see the division of labor now, right?

Now that is a simple idea of what Dependency Injection is.

It is just a technique in Software Engineering, where the dependencies of an object are supplied or injected into it by another entity.

Woow

gif gotten from giphy.com

We will look at the advantages of this programming approach in the next section. Please stay with me.

keep reading

gif gotten from giphy.com

Pros Of Dependency Injection

Here are some benefits of this technique

  • It helps when performing unit testing

  • It Encourages Decoupling

  • Reduced code since the dependency is done by another class or entity

  • It aids code maintainability

  • Because there is a division of labor, flexibility, and extensibility are made possible.

However, with all these, you would think there isn't a downside to it. Well, what doesn't have a downside?

shrug.gif

Here are some disadvantages(cons) of this technique

Cons Of Dependency Injection(DI)

  • It can make code readability to be difficult because you will have to trace functions to different files

  • It does take a lot of time to learn and understand what it is

  • It can be a source of harm to the project if the developer fails to understand it.

With these few points of mine, I hope I have been able to pass on one or two pieces of information. ๐Ÿ˜…

Don't forget to like and share this article.

Thanks for reading, and please subscribe to my newsletter.

๐ŸŒ Let's connect

ย