在域模型中传递 DTO
我看到 DTO 类型在域模型中的类型内创建并在类型之间传递。这是好的做法吗?
我一直认为 DTO 主要用在上下文边界(即对象图的边缘)来解耦上下文实现(例如在域/ui 边界)。
I see DTO types being created within and passed between types in the domain model. Is this good practise?
I always thought DTOs were to be used principally at context boundaries (i.e. at the edge of the object graph) to decouple context implementations (e.g. at the domain / ui boundary).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题有点主观,但没关系。与大多数“硬性规则”一样,实际上并不存在硬性规则。只有指导方针。总会有例外,或者在某些特殊情况下,最好的行动方案是违反最佳实践(例如使用 goto 语句立即打破多个嵌套循环)。
话虽这么说,不,在域模型中传递 DTO 类型并不是一个好的做法。 DTO 代表数据传输对象,传输通常意味着跨越某个边界的传输。如果您停留在域模型中,则不应转换为 DTO 类型,然后再转换回域类型。
Your question is sort of subjective, but that's ok. As with most "hard and fast rules", there really are no hard and fast rules. There are only guidelines. There is always an exception, or some special case where the best course of action is to do something against best practices (like using a
goto
statement to instantly break out of multiple nested loops).That being said, no, passing around DTO types withing your domain model is not a good practice. DTO stands for data transfer object, the transfer typically meaning transport across some boundary. If you're staying inside your domain model, you shouldn't be converting to DTO types and then back to domain types.
仅仅为了分层纯度而创建与域模型并行的 DTO 层次结构对我来说似乎是一种反模式。每次我都会反对。
EJB 1.0 鼓励以这种方式使用 DTO,因为传递冗长的实体 EJB 效率很低。人们会将数据加载到 DTO 中以避免网络流量。我觉得现在没必要了。
Creating a DTO hierarchy that parallels your domain model, just for the sake of layering purity, seems like an anti-pattern to me. I'd argue against it every time.
EJB 1.0 encouraged using DTOs this way, because passing entity EJBs that were chatty was inefficient. People would load the data into DTOs to avoid network traffic. I think it's unnecessary now.