SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 13 / 42

Middle

How does Dependency Inversion differ from Dependency Injection? Does every bean need an interface?

Short answer

DIP is a principle: both business code and infrastructure depend on an abstraction, and the abstraction belongs to the business side. DI is a technique: an object receives its dependencies from outside (constructor) instead of calling new itself.

depends onimplementsinjects via constructorPlaceOrderServiceOrderRepository interfaceowned by business sideJpaOrderRepositorySpring container

3 points to say:

  1. DIP = principle, DI = technique, IoC container = tool; DI helps implement DIP but is not the same thing.
  2. The "inversion" is that the interface belongs to the business side; injecting MySqlOrderDao directly is DI without DIP.
  3. Constructor injection, final fields; interfaces for ports and places with several implementations, not for every bean.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.