Dependency Inversion vs. Dependency Injection.

Dependency Inversion vs. Dependency Injection.

Thanks to the original writer and article :

https://betterprogramming.pub/straightforward-simple-dependency-inversion-vs-dependency-injection-7d8c0d0ed28e

Dependency Inversion

Dependency inversion is a design principle. In simple terms, it means we should not depend on low-level implementations, but rather rely on high-level abstractions. And that makes sense, as it allows us to be agnostic towards the implementation details. Whether we want to change a specific implementation or support other implementations, the code will stay intact, as it’s decoupled from the low-level implementation details.


To illustrate this concept, let’s assume you’re a software engineer. You have two dependencies, your code editor and your food. You don’t want to be stuck with VSCode for everything, nor do you want to eat pizza all day. Also, what happens if they change the pizza’s implementation details and it’s now terrible?

The solution to this frightening problem is to depend on high-level abstractions. You need?some?editor, and you need?some?food, but you rather keep your options open. If the pizza starts to suck, you can always use another food implementation and go for ramen, or even a burger.

Dependency Injection

Dependency injection is a design pattern that allows us to separate creation from use. It allows us to “inject” the required objects at run-time, without worrying about constructing them ourselves. It also tends to work hand in hand with the dependency inversion principle.

For example, we can depend on high-level abstract classes and inject a specific implementation at run-time, based on the required use case. This allows us to write more configurable and dynamic code. It even improves our ability to test our code, since it allows us to easily mock injected classes, so we can focus on testing our core logic.

To illustrate this concept, let’s assume that yet again you’re a software engineer, with the same dependencies — a code editor and food.

You sure as hell don’t want to prepare the pizza yourself, nor build the code editor yourself. You’re a busy software engineer, you don’t have time for that.

Luckily for you, you’ve got dependency injection by your side!

You don’t need to prepare the pizza or build the code editor — you can assume it is already created for you and injected exactly where you need it.

Also, you’re not stuck with pizza and VSCode for eternity. You can be food agnostic and let the configuration decide whether you’re going to eat (or be injected with, YIKES) pizza or ramen today.

So essentially dependency injection enables us to use dependency inversion and rely on high-level abstractions.

However, I still didn’t cover why it’s called “inversion”, what exactly is inverted here? Well, I did promise you a story! (don’t worry it’s short)

Why do we call it Dependency “Inversion”?

Well, it appears that in traditional software development, the best practice was to build software in a layered architecture, which enabled the construction of increasingly complex systems.

In this architecture, higher-level components depended directly upon lower-level components in order to achieve a complex task.

Preparing Pizza — The old way VS the “Inverted” way

To illustrate the “inversion”, take a look at the?

prepare pizza function below:

No alt text provided for this image

The high-level function is relying on two specific implementations to complete its task.

Now let’s take a look at an example using dependency injection and dependency inversion:

No alt text provided for this image

In this example, we rely on high-level abstractions, in order to complete our task. So we can make Pizza with a recipe we found online and prepare the pizza ourselves, giving grandma some much-needed rest.

要查看或添加评论,请登录

Talha A.的更多文章

  • What is Infrastructure as Code?

    What is Infrastructure as Code?

    Thanks TO : https://medium.com/technology-hits/introduction-to-terraform-76e5b18581fe https://medium.

    1 条评论
  • System Design: Load balancers

    System Design: Load balancers

    Thanks to the original creator: https://medium.com/geekculture/system-design-basics-load-balancer-5aa1c6b0f88d What is…

  • REST API Naming Standards & Best Practices

    REST API Naming Standards & Best Practices

    Thanks to the original writer and article: https://senoritadeveloper.medium.

    1 条评论
  • System Design Basics: API Gateway

    System Design Basics: API Gateway

    Thanks to the original article: https://medium.com/geekculture/system-design-basics-api-gateway-6e3387698f92 API…

    1 条评论
  • What is the Serverless architecture?

    What is the Serverless architecture?

    Thanks to the original article: https://medium.com/@raviyasas/serverless-for-beginners-cc72a41a7c9e| Technology is…

    1 条评论
  • To Perform Effective Code Reviews

    To Perform Effective Code Reviews

    10 Simple Code Review Tips for Effective Code Reviews Software code review is a process to ensure that the code meets…

    1 条评论
  • Most Useful Software Architecture Patterns

    Most Useful Software Architecture Patterns

    Layered Pattern (n-tier) The layered architecture pattern is one of the most common patterns. The idea behind a Layered…

    1 条评论
  • 15 fundamental tips on REST API design

    15 fundamental tips on REST API design

    Thanks to the original writer and article :…

    2 条评论

社区洞察