DDD, modularizing the application and domain layers without breaking the DIP

architectural-patternsdomain-driven-designlayerspatterns-and-practicessolid

Quoting DDD theory:

The application layer is thin in terms of domain logic – it merely
coordinates the domain layer objects to perform the actual work.

When it comes to modularization, and assuming that the module containing the domain entities and the interfaces of the domain services belongs to the domain layer, the application layer depends on the domain layer, which breaks the dependency inversion principle.

The implementation of the high-level component's interface by the low
level component requires that the low-level component package depend
upon the high-level component for compilation, thus inverting the
conventional dependency relationship.

What am I missing?

I add an UML diagram to clarify the problem I see.
enter image description here

  • The Persistence Layer depends upon an abstration in the Domain Layer
    -> DIP ok!
  • The Application Layer depends upon an abstraction in the Domain Layer -> Does this break the DIP?

Best Answer

It doesn't break DIP because Domain is the highest level, higher than Application, and doesn't have a dependency on it. The reverse (Domain depending on Application) would break DIP.

Also, the "should depend on abstraction" part of DIP doesn't necessarily apply here, because domain entities can't be abstracted -- they are already a pure conceptual model of your domain. The only exception may be Domain Services, because you could place an interface in front of those.

As a side note, you generally don't need to leverage dependency inversion inside the Domain model itself, because

  • Domain entities have few to no collaborators
  • The ones they have are well identified (ubiquitous language) and often not polymorphic in nature
  • Domain resides entirely in memory with no external communication (3d party framework, network, disk, etc.) involved, so it can be tested with fast integration tests without needing mocks.
Related Topic