Sobes.tech
Junior — Middle

How does the principle of dependency inversion differ from dependency injection methods in applications?

sobes.tech AI

Answer from AI

The Dependency Inversion Principle (DIP) is one of the five SOLID principles, which states that:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.

In other words, DIP aims to reduce tight coupling between components through the use of abstractions (e.g., interfaces).

Dependency Injection (DI) methods are specific techniques that enable the practical implementation of DIP. DI is a way of passing dependencies (objects, services) into a class from outside, rather than creating them inside the class.

Examples of DI methods:

  • Constructor injection — dependencies are passed through the constructor.
  • Setter injection — dependencies are passed through setters.
  • Injection via interface or fields (e.g., using frameworks).

Thus, DIP is a design principle, and DI is a way to implement it in code.