'Repository implementation in Application, Domain and Infrastructure Layer (DDD)
I have a project structure like this :
Here, IApplicationDbContext.cs
interface defined in Application
layer and it's implementation is done in Infrastructure
layer. Similarly for Generic Repository called `IRepository'
I have a doubt about where to implement the Specific Repositories? in this case, I have IProductRepository
defined in the Application
Layer. So, where should I implement this? In the Application Layer
itself? or in the Infrastructure Layer
?
Please guide.
Solution 1:[1]
Repository implementation
The Infrastructural Layer
of an application represents the technical details that enable it to function. It is responsible for the technical implementation of storing information on the state of domain objects.
So the Repository implementation
comes under the Infrastructure Layer
because they deal with storage, which is not a responsibility
that the model should take on. It is usually backed by a persistence framework to do the heavy lifting. Specific Repositories must be implemented under the Infrastructure Layer
.
Repository contract
According to DDD Repository contract
should be defined in the Domain Layer
next to all of the domain objects. High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. Based on this rule we can't define repository interface in Infrastructure layer
. Repository also can be used by Domain Layer
in this case we can not define contract in Application Layer
.
The repository is the contract between the domain model and the persistence. It should be written only in terms of the Domain and without a thought to the underlying persistence. The contract of a repository is more than just a CRUD interface. It is an extension of the domain model and is written in terms that the domain expert understands. Your repository should be built from the needs of the application use cases rather than from a CRUD?like data access standpoint.
Where to define the interfaces for a repository in an layered architecture?
Domain-Driven-Design Implementation-Guide
Repository client
A typical client of a Repository
is the Application Service Layer
.
Domain Services
also can use repositories.
References:
Vaughn Vernon - Implementing Domain-Driven Design
Patterns, Principles, and Practices of Domain-Driven Design 1st Edition
by Scott Millett
Solution 2:[2]
IProductRepository - Domain Layer. "Product Repository" is a business concept that explicitly represents the storage of a domain entity. It translates well to the real world.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Yorro |