Is this a proper use of DTO?

此生再无相见时 提交于 2019-11-30 20:59:52

I prefer to return actual entities from my repositories. This way when you need to orchestrate complex interactions between different entities in the service layer, there's no converting back and forth to DTO's. The service layer then projects entities into DTO's when returning data to the application layer.

I know there are purists who say that all interaction between layers should be done with DTO's, but I find that impractical. The service layer will end up coupled to the entities anyway, so it's not like you're adding coupling.

It also limits the projection/flattening of DTO's to entities to the service layer. Which to me is a plus since these activities add to complexity and decreases performance.

Your data context is your unit of work. The "one unit of work per repository" idea is an anti-pattern. Units of work should be scoped by the service layer, which may involve 1-many entities from 1-many repositories. If each repository has a different unit of work, you lose your ability to have service layer calls maintain consistent transactions easily.

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