DTO 可以有返回派生值的实例方法吗?
DTO 具有返回基于 DTO 数据的派生值的实例方法是否可以接受?或者 DTO 应该是没有其他方法(除了 getter/setter 之外)的纯数据容器?
我内心的纯粹主义者说,业务逻辑要渗透到这些方法中是非常容易的。但是,如果(例如)DTO 跨应用程序层共享,则可能存在在 DTO 上使用此类方法的争论。
您对此有何看法?是否存在可以接受的情况,或者应该避免这种情况?为什么/为什么不呢?
Is it ever acceptable for a DTO to have instance methods which return derived values based on the DTO's data? Or should DTOs be pure data containers with no additional methods (other than getters/setters)?
The purist in me says that it is far to easy for business logic to creep into such methods. However, if (for example) a DTO is shared across application layers, then maybe there is an argument for having such methods on the DTO.
What are your views on this? Are there ever situations where it is acceptable, or should this sort of thing be avoided? And why/why not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DTO 不应该有行为,它们只是跨进程边界传输数据的容器,并且应该仅包含 setter/getter。
应该不惜一切代价避免这种情况,否则会被视为对 DTO 模式的误用。
DTOs should not have behaviour, they are mere containers for transporting data across process boundries and should consist of setters/getters only.
It should be avoided at all costs otherwise it would be construed as misapplication of the DTO pattern.