Is this a proper use of DTO?

前端 未结 1 954
天涯浪人
天涯浪人 2021-01-06 01:15

I\'m writing a console application that does a good amount of data retrieval from stored procedure recordsets. For each recordset type I\'m working with, I have a Repository

相关标签:
1条回答
  • 2021-01-06 01:42

    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.

    0 讨论(0)
提交回复
热议问题