开发者

Can a DTO have instance methods returning derived values?

开发者 https://www.devze.com 2022-12-20 05:06 出处:网络
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/sett

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消