1

I am trying to find a proper explanation about the difference between Dependency Inversion, DI and IoC and read many articles to understand the difference like the following ones:

Inversion of Control vs Dependency Injection

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

However, as I read a new articles and the answers on SO regarding to the issue, I am getting much more confused. Because, on Spring Documentation, there is also the following clause for IoC:

IoC is also known as dependency injection (DI)

So, could you please simply explain the differences between Dependency Inversion, Dependency Injection and Inversion of Control?

Jack
  • 1
  • 21
  • 118
  • 236
  • in spring documentation that you mentioned, they said : dependency injection and not Dependency Inversion. So if you understand the difference between Dependency Inversion and IoC, you are good – Idriss Nov 19 '22 at 22:56
  • @Idriss Are you sure that you read the question? I am afraid you did not read even the title and made some useless comment. – Jack Nov 19 '22 at 23:04
  • lol according to spring : Dependency Injection = Inversion of Control. So we can discuss now this question : "Dependency Inversion vs Inversion of Control". The second link that you mentioned explain all. – Idriss Nov 19 '22 at 23:13
  • I can't promise that the Spring documentation strictly adheres to these definitions, but checkout these Wikipedia articles: https://en.wikipedia.org/wiki/Inversion_of_control, https://en.wikipedia.org/wiki/Dependency_injection, and https://en.wikipedia.org/wiki/Dependency_inversion_principle. – Slaw Nov 20 '22 at 01:47

1 Answers1

1

Dependency Injection (DI) is a design practice to avoid strong coupling between classes.

Example A

interface IB{}

Class B implements IB {}

Class A {
IB ib;

public A(IB ib){
this.ib = ib;
}
}

It is considered a dependency injection because ib can be anything that implements this interface. When you want to Test Class A you don't need to instantiate IB, or you can instantiate it with whatever you want. Bottom line, Class A doesn't depend strongly on ib (or class B)

Inversion of Control (IoC) is a concept where you, the developer, give the instantiation control to the framework (for example Spring). Hence, in a well-written code, you won't see the explicit invocation of constructors because the framework does it for you. IoC uses the DI design practice.

Let me know if you need a better clarification

Alexander Tilkin
  • 149
  • 1
  • 2
  • 13