DTO 可以有返回派生值的实例方法吗?

发布于 2024-08-22 03:32:57 字数 218 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从﹋此江山别 2024-08-29 03:32:57

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文