Search code examples
domain-driven-designddd-repositories

DDD: Should repository reside in Domain Layer or Application Layer


In a Layered Architecture with Dependency Inversion Principle we have some Application Services (in Application Layer) and some Domain Services (in Domain Layer).

Now I want to do some validations on Object Compositions. In the red book, on chapter 5 under Validating Object Compositions somewhere it says:

But it may be best to manage that kind of validation using a Domain Service. The Domain Service can use Repositories to read the Aggregate instances it needs to validate.

  • Doesn't this means that repository interfaces live in domain layer?
  • If yes, are there any un-told truth about these repository interfaces? (maybe they can only fetch aggregates by id?)

I know that this question has been asked so many times. But I find most of the answers neglecting the blue and the red book this stackexchange thread for example


Solution

  • Doesn't this means that repository interfaces live in domain layer?

    Yes, repository interfaces live in the domain layer for sure and usually domain services and application services (a layer above) are their consumers.

    If yes, are there any un-told truth about these repository interfaces? (maybe they can only fetch aggregates by id?)

    Repositories are intended to fetch and modify aggregate roots, be it for validation or any other purpose. The fetch doesn't necessarily have to happen with id, it may happen with other parameters as situation demands.