什么是 DTO 和 BO?有什么区别?
我知道DTO是数据传输对象,BO是业务对象。但是,它实际上意味着什么?我什么时候应该选择其中之一? 从我理解的DTO只是用来传输数据,没有业务逻辑。这是否意味着 DTO 没有任何仅方法属性(getter 和 setter)?但是,它仍然具有 BO 的属性。有人可以解释一下吗?谢谢。
I know DTO is a data transfer object and a BO is a business object. But, what does it actually mean? When should I choose one over the other?
From, what I understand DTO is just used to transfer data and doesn't have business logic. Does this mean that a DTO doesn't have any method only properties(getter and setter)? But, it still has properties of a BO. Can someone please explain? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DTO 用于在层/层之间传输数据。为此,它不需要任何方法,有时甚至不应该有任何方法 - 例如当 DTO 通过 Web 服务公开时。
业务对象是一个聪明的对象,它包含在此对象上执行操作(更改数据)的数据和方法。当你将BO暴露给上层时,它可以调用你对象的公共方法。有时您不希望这样,因此您创建的 DTO 只提供数据而不提供方法。
DTO 不必传输所有 BO 数据。当您遵循严格的 DTO 方法时,您可以为业务层上公开的每个操作创建特定的 DTO。例如,如果您的对象具有 CreatedBy、ModifiedBy、CreatedDate 等审核数据,并且您正在创建 Update 方法,则传入的 DTO(带有更新的对象)不需要具有这些属性,因为上层无法修改它们 - 只有业务逻辑可以。
DTO is used to transfer data between layers/tiers. For such purpose it doesn't need any methos and sometimes it even should not have any methods - for example when DTO is exposed over web service.
Business object is clever object which contains data and methods which performs operations (change data) on this object. When you expose BO to upper layer, it can call your object's public methods. Sometimes you don't want this and for that reason you create DTO which only offers data but not methods.
DTO doesn't have to transport all BO data. When you follow strict DTO approach you create specific DTOs for each operation exposed on your business layer. For example if your object has audit data like CreatedBy, ModifiedBy, CreatedDate, etc. and you are creating Update method your incomming DTO (with updated object) doesn't need to have these properties because upper layer cannot modify them - only business logic can.
一般来说,DTO在到达层之前有那一刻的相对静态数据,而BO可以动态地保存状态和流标志值; BO也可以是自包含的,可以对某些业务逻辑进行验证或逻辑重组或判断;但是 DTO 的变化取决于 tier 传递过来的数据的变化...但是,BO 的变化范围更广,例如,取决于业务流程状态、flag 的变化、甚至身份的更动态更新。实时变化,这些应该被捕获并采取行动以从BO反映,例如,余额从200美元变为零,或者余额从2000美元变为5000美元,那么交易/交易身份或状态将发生变化...这是DTO 和 BO 之间有很大区别。
Generally, DTO has relative static data for that moment before arrive tier, but BO can dynamically keep state and flow flag value; and BO also could be self contained to have validation or logically reorganization or judgement for some business logic; but DTO 's change depends on tier 's change of data that passed over... But, BO's changes has wider scope, for example , depends on more dynamically update with business flow state, flag 's change, even the identity could be changed in real time , these suppose to be captured and acted to reflect from BO, for example, such as balance from $200 become zero , or balance from $2000 to $5000, then the deal/trade identity or status will change ... this is big difference between DTO and BO .