Day 11 of #100DaysOfCode

Dependency Injection for Beginners

Why you must know and use it as early as possible in your projects.

Dhananjay Trivedi
3 min readSep 28, 2019

Dependency Injection is a very popular design pattern and the term literally means injecting your dependencies. Learning this will help you make your project codebase more modular, clean and maintainable.

It will definitely take your mind off the pain.

First, let's understand what are dependencies?

Dependency is just another object your class requires to function.

For example:

  • You are fetching data from database in some class (let’s say repository class), you will be using a database object to perform the operations. Hence the database here is a dependency in your class.
  • You may have a UI class which requires the ViewModel object to observe data from and update the UI accordingly. The view model here is a dependency for your UI class.

What does ‘Injecting’ a dependency mean?

Let’s go back to the example of using the database in our class. Usually, we approach the problem by instantiating the database object in our class. Then we call various methods to perform our required operations.

Our class’ only responsibility should be to do the operations on the database without worrying about instantiating it.

Only if we have a way to Push or Inject the initialized database object into our class, would be cool, right? That’s what dependency injection is all about!

We shouldn’t instantiate dependencies using the ‘new’ operator from inside our class. We should take them as a constructor parameter or a setter in our class.

But why should we be Injecting the dependencies?

Great Question!

First, let's understand the ‘D’ of SOLID Design Principles which stands for
Dependency Inversion Principle
.

Let’s quickly understand it with an example: If you are the CEO of McDonald's, your responsibility would be to manage the Shareholders, make the strategic acquisitions, deciding how to approach different markets and many other high-level decisions.

Similarly, you won’t be worrying about driving the truck for making delivers nor about maintaining the website and neither worry about the accounting. You get the idea!

Your job is to delegate managers responsible for each lower-level tasks. Those managers can even have different managers to delete their tasks. If you get the example, you can summarize it as:

“High-level objects should not depend on low-level implementations”

Similarly, Dependency Injection helps in decoupling class’s construction logic with its dependencies’ construction logic. We can use various classes and interface to do so. Popularly, people have an interface that defines the condition for a class to become a dependency for our class. All dependencies have to abide by this interface.

This way we keep our code clean and maintainable, which is the end goal we all should aim for.

Hmm… More problems now

  • How to keep track of all the dependencies required by different classes?
  • How to initialize all these dependencies outside the depending class?

Enters, Dependency Injection Container. It's a class which maps and maintains the dependencies required by each of your dependent classes. It also contains the logic of how to initialize these dependencies if they haven't been initialized yet.

When a dependent class asks for dependency, the container class checks whether if the dependency is initialized already? If it has, it will just use that one else it will create the dependency, store it and then return it to you.

It takes care of even managing complex dependencies and your class just have to call them using one method. This makes our dependency management so modular that you can easily replace any of the dependencies with just updating the container class and your dependent classes won’t even know about it. That’s Monica clean!

If you have been following some design patterns like MVC, MVP, MVVM, MVI then certainly there will be a certain level of good abstraction in your codebase. Maybe all you need is the Dependency Injection container class which just injects various dependencies and make things better for you.

There you go, Now go & apply!

You just learned Dependency Injection. Go ahead and explore libraries for Dependency Injection for your tech-domain and apply what you just learned to make this worth your time! It’s an investment you will never regret.

Thanks for reading so far. If you learned something new, give claps (you know you can give up to 50? ) and show your support for my #100DaysOfCode!

--

--

Dhananjay Trivedi
Dhananjay Trivedi

Written by Dhananjay Trivedi

Android | Flutter | App Developer | Product Management | Prompt Engineer who loves writing and sharing knowledge

No responses yet