Clean architecture and Repository pattern

clean-architecturerepository-pattern

I'm working in a new project and I'm trying to use the Clean Architecture approach with Repository pattern. I'm using .net core 3, C#, Automapper, MediatR and Dapper.

I have these layers:

Domain (in the center, the core of all): here I have the business Entities, Aggregations and Value Objects with their validations rules and exceptions;

Application (it's around the Domain): here I'm using CQRS patterns and I have my Commands, Queries and Interfaces;

Persistence: here I have the implementations of the repositories interfaces.

I red that a repository should be responsible of the all CRUD operations relative to one table in the database. For this I want to know how I should implement the repositories for an ENTITY that is an AGGREGATION ENTITY. Should I create an AGGREGATION REPOSITORY that extract data from different tables? Or should I have a repository for each table and a SERVICE that creates AGGREGATION using more than one repository?

Thanks

Best Answer

A good repository should abstract away the underlying database structure. An entity may have its data stored in several different tables, but your domain logic shouldn't care about that. At most, you'd have one repository for each entity in your domain, but you could also just have one repository for each aggregate root. It's up to you to decide what makes sense for your case.

For example, let's say your domain contains Order entities and OrderLineItem entities. Does it make sense in your domain to retrieve an OrderLineItem directly without the corresponding Order? If so, then you may need a repository for both entities. However, if you only access OrderLineItem entities from an Order entity, then you only need a repository for Orders. This repository would potentially query both the orders table and the order line items table to construct a complete Order with all of its OrderLineItems.

Related Topic