Member-only story

The Difference Between Dependency Inversion and Dependency Injection

Phillip Johnson
CodeX
Published in
10 min readApr 9, 2022
Photo by Raghavendra V. Konkathi on Unsplash

Introduction

Understanding Dependency Inversion has been a long journey for me. I quickly understood Dependency Injection as it gained popularity and, as somebody who likes automated unit tests, could immediately see benefits of using it for testing. What is Dependency Inversion though and how does it differ from Dependency Injection? In this article we will look at a very simple example of Dependency Injection and develop it further to consider Dependency Inversion.

Different Types of Dependency

To understand Dependency Inversion you need to understand two different types of dependency:

  1. Flow of Control Dependency — when you are debugging code and the line of execution goes from one project into another project or dll.
  2. Source Code Dependency — where one project references another project or dll.

What do we mean by Dependency Inversion

Dependency Inversion is where the Flow of Control Dependency goes in one direction between two projects or dlls, but the Source Code Dependency goes in the other direction.

Why Invert a Dependency?

--

--

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Phillip Johnson
Phillip Johnson

Written by Phillip Johnson

Over 20 years experience in software engineering, mostly in leadership positions. Passionate about software craftsmanship.

Responses (2)