DDD: Should a Dto Assembler be a part of Domain Layer?

倖福魔咒の 提交于 2019-12-12 10:55:55

问题


Thanks in advance.

I have some Aggregates in the Domain Layer library. Also, some DTOs in a separate library, which is shared between Server and Client side.

An Aggregate of an entity is more informative than its DTO. So, in order to convert from DTO to Aggregate, a repository should be accessed by a Dto Assembler. Interfaces of repositories are in Domain Layer. That's why i come to conclusion that DtoAssembler should be a part of DomainLayer.

Is this right?


回答1:


No, this would be plain wrong in the context of DDD.

Try asking a (non-technical) domain expert what he thinks about the DTO assembler. He will look with large, questioning eyes at you.

DTOs (and consequently their assembler) are a technical concept - they define the data structure in the context of a specific interface to your system.

Repositories mostly return aggregates. If you query the database for statistical data that is not modeled in your domain, then a repository may also return an immutable data object. Just make sure that you're not accidentally missing a domain concept when doing that.

Once you have the data from a repository (no matter whether it is an aggregate or a data object) you can feed it into the DTO assembler.




回答2:


Not necessarily, in my opinion. Since you are using a layered architecture, I assume your DTOs reside in an upper layer -- commonly the service layer - that acts as the communication point between server and clients. Indeed, the main purpose of the DTOs as their name imply (Data Transfer Objects) is to enable for communication, transfering the information without any kind of logic attached - eg. operations.

Layers should only know those located beneath, not above; thus, the DTOs should be kept in the service layer and be consumed by whoever is responsible - for instance, a command processor - in order to retrieve the domain objects from a repository and perform whatever operations are involved.

Bottom line: DTOs don't apply to the domain, they belong to service / communications.



来源:https://stackoverflow.com/questions/33895762/ddd-should-a-dto-assembler-be-a-part-of-domain-layer

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