advanced

Dependency Inversion Principle

Make policy depend on stable abstractions rather than concrete infrastructure so boundaries are replaceable and testable.

DIP: high-level policy should not depend on low-level details; both depend on abstractions. Domain defines `OrderRepository` port; infrastructure implements `PostgresOrderRepository`. Inversion moves dependency direction—core does not import SDK types.

Pairs naturally with DI: abstractions owned by the policy layer, implementations wired at the edge.

On interviews: draw dependency arrows before/after inverting a direct database import in domain code.

Common pitfalls: anemic ports mirroring ORM entities 1:1, abstraction owned by infrastructure package, and interfaces with no second implementation ever.

The trade-off is indirection and mapping layers versus replaceable infrastructure and fast unit tests.

Checklist:

  • Domain owns port interfaces.
  • Infrastructure implements ports.
  • Compose wiring at application root.