“对象不应该有public的field,应该用getter/setter将field封装起来。”我们对于这个规则已经太熟悉,熟悉得完全忘记了它的由来。Uncle Bob提出了一个问题:完全用于传输数据的DTO是否需要getter/setter?实际上,在很多时候,DTO并不是一个“对象”(我们用这个词表示“数据与行为的组合体”),仅仅是一个“数据结构”——把数据从一个地方带到另一个地方。给“数据结构”加上getter/setter,从OOD的角度来说着实没什么价值。
但是我至少有两个理由反驳Uncle Bob的话。第一,有时DTO要对其中携带的数据加以组合或变换,给出适合UI层的形式,这时需要getter(甚至setter)的封装;第二,用jakarta-commons-beanutils工具可以很方便地在提供了Java Bean语意(即getter/setter)的对象之间复制属性。
Uncle Bob's Software Craftsmanship Corner
Oh No! DTO!
by Robert C. Martin
February 26, 2004
Summary
Should DTOs have public variables? Or should they have private variables with getters and setters?
I'm teaching an XP Immersion(TM) class this week. We are writing FitNesse tests as the acceptance tests for the application we are writing. One of the programmers was using a RowFixture. RowFixture requires the use of a DTO (Data Transfer Object) and insists that the variables be public. The programmer was aghast. "DTOs are always made with private variables and getters and setters!" he said. "Why?" I asked.
Why indeed? Has the religion of OO carried us so far away from common sense that we can't recognize a data structure when we see one? Why do we need to bloat our code with a bunch of useless getters and setters just to fulfill some dogmatic rule that nobody can adequately explain?
In my view OO programs contain two kinds of entities: Objects and Data Structures. Objects have private data and public methods. Data Structures have public data and no methods (or sometimes trivial navigational methods). There are very good reasons for keeping the variables in an object private. We want to know which functions can manipulate them. We want to protect the invariants of the object. We don't want others to depend on our details (DIP). On the other hand there is no good reason to use getters and setters in a data structure. A data structure is simply a packet of data, nothing more.
分享到:
相关推荐
DTO数据传输对象简介PPT
如果想生成未预置的代码,如vo对象、前端vue页面以及其他自定义代码等,需要做一些定制和扩展来实现。 从使用角度而言,官方资料介绍得比较简要,要实际使用,仍然需要进行具体的尝试和摸索,包括必要时看下源码...
让繁琐的的数据集不需要开发者自己动手就可以封装的对应的bean中去
DTO(数据传输对象) 概述 创建该库是为了解决以下问题:从数据源获取数据模型,并且需要删除,添加,合并或转换数据模型为相似或完全不同的对象。 其他语言也有类似的库(DataMapper,DTO(C#)等)。 我永远找不...
Laravel多托 。 正在安装 $ composer require cblink/laravel-dto -vvv ... 您只需要确保遵循PSR-0,PSR-1和PSR-2编码准则即可。 任何新的代码贡献都必须在适用的情况下进行单元测试。 执照 麻省理工学院
该工具可以对接数据库,将数据库中的表转换为java的实体类和DTO类。
把分页按钮写在dto里,其他dto继承他
简单的EntityFramework4.3+三层+DTO,如果需要简化版的,我的资源里有一个不含DTO的版本。 这个Demo的主要功能是: 1、实体类的创建、复杂类型的嵌套 2、实体类的配置(主键、外键、一对一、1对多,多对多) 3、...
java DTO 详解 java DTO 详解 java DTO 详解 java DTO 详解
杰森2 DTO 库很棒,但是键入DTO很快就会变得很麻烦。 受Json2Typescript样式工具的启发,我们为PHP DTO构建了一个工具。网络版在尝试Web版本在易于使用的GUI中具有与cli版本相同的所有工具。 生成嵌套DTO时,该工具...
DTO的共享数据传输对象。在本文中,我们将介绍在微服务之间共享DTO的方法。 使用微服务管理表示应用程序域的模型。域模型的关注点与DTO不同,我们将它们与DAO层中的数据模型分开。 这样做的主要原因是我们不想通过...
登陆的dto.zip
根据数据库自动生成DAO和DTO,具体方法:解压到D盘根目录,修改property文件的数据库路径,驱动,用户名,密码等,进入CMD到D盘运行java -jar kingdao.jar即在D盘根目录test文件夹下生成DAO和DTO-DAO and the ...
个人对DTO这种设计模式的一些粗略见解。
NULL 博文链接:https://eddysoft.iteye.com/blog/1941484
EF+Mapper结合使用实现Dto到实体类再到数据的(框架模型基础实现)
项目中,自动生成javabean对象和Dto,所有的项目加注释
Java中 PO VO BO DTO DAO 和 POJO 关系图
PO可以严格对应数据库表,一张表对映一个PO。... VO:value object值对象、view object视图对象 PO:持久对象 QO:查询对象 DAO:数据访问对象——同时还有DAO模式 DTO:数据传输对象——同时还有DTO模式
前夕/ dto 用PHP编写的简单,灵活的数据传输对象库。为什么? 可以说,在层之间传递数据作为关联数组是一种常见的做法。 例如,用于创建新用户的服务方法可能如下所示: // UserService.phppublic function create...