Should the repository layer return data-transfer-objects (DTO)?

半城伤御伤魂 提交于 2019-11-27 06:55:00

Short answer: No.

Long answer: repository is responsible for turning persisted data back to entities (models) and vice versa.

Model is a business Model representing a business entity. DTO on the other hand - while looks like Model - is concerned with transfer of the object between various environment and in essence is a transient object. Usually mappers are responsible for turning model into DTO.

So your repository needs to hydrate the entire entity even if it's not being used? This seems very inefficient. – ajbeaven Oct 29 '18 at 23:25

Couldn't you add methods to the repository interface for calls that don't need to hydrate the entire entity? I suppose that could lead to bloated interfaces, which is one of the main arguments against, I think.

To answer the question, I agree with the accepted answer of No. Repository implementations are in the persistence layer. The domain layer may need to retrieve deep or shallow objects from the persistence layer which knows nothing except the Interface it must implement. If the domain is constantly asking for a full refrigerator when it only needs butter, then maybe the Interface (or perhaps the data model) need some work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!