Straightforward & Simple: Dependency Inversion VS Dependency Injection

Straightforward & Simple: Dependency Inversion VS Dependency Injection

I’ve been a software engineer for a few years now, yet every now and then I like to return to the fundamentals. We all “know” that we should follow the SOLID principles by good ol’ Uncle Bob, but common knowledge is not always common practice.?

Recently, it dawned on me that I don’t really know the difference between Dependency Inversion and Dependency Injection. I know what they mean, roughly, but I wasn’t sure what’s the difference between the two, and moreover, why is it called Dependency inversion, what exactly is inverted?

It turns out, like all good things, there’s a story behind it.

But first things first, let’s discuss the difference between the two concepts.

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 preparePizza 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 Inversion & 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.

What we’ve learned?today

We covered the difference between Dependency Injection and Dependency Inversion, and how they work together to produce great software. We also discussed why we use the term “Inversion”, and learned some software history along the way.

I hope you found this article useful, and until next time, keep it Straightforward & Simple!

Guy Erez

Software Engineering Team Lead at LayerX Security

3 年

Also published on Medium - follow me there for more articles on engineering, productivity, psychology, and (hopefully) interesting thoughts about life. https://guy-oxnb.medium.com/straightforward-simple-dependency-inversion-vs-dependency-injection-7d8c0d0ed28e

回复

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

Guy Erez的更多文章

  • Software Tidbit #6 - Tree Shaking

    Software Tidbit #6 - Tree Shaking

    In a nutshell - it means removing unused code from your bundle, AKA dead-code elimination. It’s a cool process that…

  • Software Tidbit #5 - A Cool Regex Feature

    Software Tidbit #5 - A Cool Regex Feature

    Like all good things, it starts with a story. I wanted to extract a query param out of a URL, so I did what every good…

    1 条评论
  • Random Software Tidbit #3 - Abstract Syntax Trees

    Random Software Tidbit #3 - Abstract Syntax Trees

    Abstract Syntax Trees are essentially a language-independent representation of a text. In the realm of software…

    2 条评论
  • Random Software Tidbit #2 - Monads

    Random Software Tidbit #2 - Monads

    Monads are an interesting functional programming concept. In a nutshell, they are value containers of a certain type T.

    2 条评论
  • Don’t Write Perfect Code

    Don’t Write Perfect Code

    Perfectionism. Nowadays it’s often displayed as a badge of honor.

    3 条评论
  • Are You a Cargo Cult Programmer?

    Are You a Cargo Cult Programmer?

    The Bad news is — Many programmers are Cargo Cult Programmers, and that’s not something to write home about. The Good…

    3 条评论
  • It Ain’t Voodoo — Debugging Code That Doesn’t Make Sense

    It Ain’t Voodoo — Debugging Code That Doesn’t Make Sense

    You’re staring intently at your code. You’ve just been informed of a bug someone found in production, and it’s your…

    3 条评论
  • Understanding the JVM Memory Model — Heap vs. Non-Heap

    Understanding the JVM Memory Model — Heap vs. Non-Heap

    The JVM is a beloved concept that every Computer Science undergrad knows by heart. It’s been with us since 1994, and…

    3 条评论
  • How SOLID Remains Solid — Software Principles vs. Patterns

    How SOLID Remains Solid — Software Principles vs. Patterns

    Last time, we covered the difference between Dependency Inversion & Dependency Injection. Both are important concepts…

    3 条评论
  • The Important Lesson I Learned Fixing My Fridge's?Door

    The Important Lesson I Learned Fixing My Fridge's?Door

    The Important Lesson I Learned Fixing My Fridge’s Door I find that one of the best ways to learn a concept or…

    3 条评论

社区洞察

其他会员也浏览了