Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • Good answer, but note that in DDD, a Repository is part of the Domain Model, and as such it's implemented within the Domain Layer (Fowler's "Repository Pattern" is not quite the same as the DDD Repository). Commented Aug 6, 2019 at 18:58
  • sorry for being late, but @Rogério is true that Repositories are domain concepts, but is also true that repositories are ports, they don't implement access to database but are just an abstraction of a collection of aggregates, so it's still true that domain has no clue about data persistence (if any) Commented Aug 7, 2020 at 18:03
  • @CarmineIngaldi Actually, in DDD layered architecture (the one described in the original book about DDD) while the interface of a Repository is certainly "an abstraction of a collection of aggregates", the implementation of said interface knows about data persistence, and it belongs within the Domain Layer. Note this does not polute the business logic implemented elsewhere (in Domain Services, Factories, and Entities), which only know about the Repository interfaces. It seems to me that people fail to understand the interface/implementation divide. Commented Aug 10, 2020 at 1:59
  • yes @Rogério this is exactly what i would point out: Domain Layer is still unaware of persistence/durability details of aggregate state because the logic is implemented elsewhere. Repositories just declare APIs to access a collection of aggregates and are consumed from Domain Services. Spring Data JPA is a good example of this abstraction: we declare *Repository interfaces in our codebase, but the implementation is owned by the framework at infra level Commented Aug 10, 2020 at 11:12
  • @CarmineIngaldi No, the Domain Layer is not just a bunch of abstractions! As any layer within the application's codebase, it also contains the implementations for those interfaces/abstractions. A DDD Repository doesn't "just declare APIs". With Spring DATA JPA, we get runtime code generation from annotated Java interfaces, this is a peculiar technology, but we still have "implementation code" in such annotations, for example @Query("select c.firstname as firstname, c.lastname as lastname from Customer c") Collection<CustomerProjection> findsByProjectedColumns(); in CustomerRepository sample. Commented Aug 10, 2020 at 22:14