0

Repository is like a collection of domain objects. So it should not return DTOs or anything that is not a domain object.

But, Suppose your domain model has 20 fields with large amount of data and you want to use only 2 fields here, you have to fetch the whole row first and then map it, which is very inefficient.

Elham Azadfar
  • 709
  • 2
  • 17
  • 34
  • 2
    Possible duplicate of [Using DTO to transfer data between service layer and UI layer](https://stackoverflow.com/questions/16866102/using-dto-to-transfer-data-between-service-layer-and-ui-layer) – VahidN Nov 29 '18 at 20:48
  • TAggregate GetById(object id) –  Aug 11 '21 at 16:54

1 Answers1

3

It depends. If you are modeling with DDD and CQRS then you should return Aggregates for commands and ViewModels for queries. You can split repos in reads and writes, where reads are used for serving views for example, or REST APIs, case in which you would have DTOs and not ViewModels, thus you only return the data (fields) that you need from the query.

In the write stack you should have a single method that returns, and the return type should be the Aggregate of that specific repository (use lazy loading if you don't want to load all related child collections)

TAggregate GetById(object id)