Let’s get started on understanding Dependency Injection! 🤓

Bonnie Chen
BONNIE
Published in
4 min readMar 30, 2021

When developing a HUGE project, try to loose coupled components and eliminate the dependencies from the code are very important issues.

In the software engineering field, dependency injection(DI) is an important technique or design pattern used to implement Inverse of Control(IoC). Which is, its purpose is to develop a loosely coupled codes/systems.

Let’s take a look at the picture below.

image source: https://www.tutorialsteacher.com/ioc/introduction

Perhaps there are several terminologies could be confused or unfamiliar, but the important concept is “The design pattern — Dependency Injection(DI) follows the principle — Inversion of Control(IoC) and could implement on IoC Container framework.

If you think these terms are too hard to understand, wait… Let’s keep reading 👌

As we know the aim of dependency injection is to loose coupled codes. However, how to put it into practices is another lesson we should to learn. And, before putting it into practices, it values to understand the concept of dependency injection.

There are a lot of advantages that putting dependency injection into the software development. But we should not misunderstand that dependency injection benefits only which one advantage to the systems. It is closely related to several perspectives on software developments. For examples, it can be late binding, and it is great for unit testing. Additionally, we also have to know that dependency injection container is not mandatory to dependency injection. Even, dependency injection and other design patterns are easy to be mixed up.

In an abstract factory pattern or service location pattern, a component can request the service which it needs. Perhaps it makes us consider that the principle of service location pattern is same as dependency injection. Instead, they are the opposite principle on the dependency.

From the stack overflow link above, we can be clear about that the main difference of service location and dependency injection is “How the dependencies are located?”. In the service location, the client class will request the dependencies that it needs. However, in the dependency injection, the client class neither cares where the dependencies from nor knows. Because the dependencies are already injected into the client class as it constructs.

image source: https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.slideshare.net%2Fconanak99%2Fioc-and-mapper-in-c&psig=AOvVaw2Fk7cr-4jWrm6cktBuEKV-&ust=1617066336473000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCODO-eCn1O8CFQAAAAAdAAAAABAW

Take a glance at the picture above, I think this picture is a great example to express the dependency injection meaning! 😆 We can see some needles inject into the class. It means, where the service class comes from that is not a matter to the client class.

Additionally, there is a picture below which represents the relationship between the components on the dependency injection. Also, the picture link is a great explanation about it.

Furthermore, if googling dependency injection, there are lots of examples took a simple way to explain dependency injection. Some examples do not use the dependency injection container. In some materials, they would call it pure dependency injection(Pure DI), which is without container.

image source: https://dotnettutorials.net/lesson/dependency-injection-design-pattern-csharp/

How about the dependency injection looks like in the code ? Let’s take a really simple code to explain.

Service part

Client part

Injector part

From the code above, it is a typical constructor injection. IDialogWriter injects into Application, and Application does not need to interact with ApplicationDialogWriter directly. Just through interface to contact each other.

Perhaps this simple example might confused you or still can not realize the importance of dependency injection. However, when we develop a huge software, the component might be an object, a system architecture layer, or even a module, so the loosely coupled components are much more important to the developers. Moreover, a loosely coupled software have lots of advantages, like extensibility, parallel development, maintainability, testabilityetc. Which is, either in the development period or in the maintenance period are all benefit to the developers. 👍

In the next article, I will share about more detail on the difference of dependency injection and service locator pattern. 🤓🤓

Thanks for reading this article, if you like it, clap it! Or anything want to share with me, feel free to contact me via an e-mail!

--

--

Bonnie Chen
BONNIE
Editor for

I am a software development engineer. I like to code, think, exchange thinking with people, and also explore everything in this world :)