3

Directly from this oracle article about the J2EE DAO Pattern:

Everything is very clear indeed but the Transfer Object "participant" (as they call it).

Here I quote the bit I would like more insights about (especially would be useful a real life example (an easy one)).

TransferObject

This represents a Transfer Object used as a data carrier. The DataAccessObject may use a Transfer Object to return data to the client. The DataAccessObject may also receive the data from the client in a Transfer Object to update the data in the data source.

I am trying to use this pattern as an exercise (as a student for the exam OCPJP it requires to understand the DAO Pattern). So far I have my DataSource (mysql database), my business object (JavaBean called Person) and my DAO object interfacing properly between the database and the JavaBean (Person).

So again What exactly a Transfer Object is?

EDIT: FROM THE FIRST REPLIES I UNDERSTOOD THAT ACTUALLY I KNOW WHAT A TRANFER OBJECT IS BUT I DO NOT KNOW WHAT A BUSINESS OBJECT IS... SO THE QUESTION REMAIN THE SAME BUT FOR THE BUSINESS OBJECT. NOT FOR THE TRANSFER OBJECT.

Thanks in advance and sorry about that.

Thanks in advance.

4

3 回答 3

5

转移对象是一个简单的类,有字段,没有逻辑。它们是可序列化的POJO(普通旧 Java 对象)并具有访问器(getter、setter)来访问字段。它们被称为传输,因为它们用于在层之间传递数据或粗略地说组参数传递给服务方法调用,它们不需要匹配业务对象

例子

UserLogin { // just fields that are needed to login, not a User business object
    String name;
    String password;
}

LoginService { // sample sarvice that check passwords
    boolean Login(UserLogin userLogin) {...}
}

传输对象与其他类似结构的类的区别在于它们的使用方式(传输数据)而不是它们的构建方式(字段和访问器)。

于 2013-06-09T09:56:33.713 回答
3

DTO

DTO 除了存储和检索自己的数据(访问器和修改器)之外没有任何行为。DTO 是简单的对象,不应包含任何需要测试的业务逻辑。

DTO 用法

案例 1:如果域对象很大,请使用 DTO,但您只需要它的一些属性!对于大型域对象,客户端仅在其中可视化、更改或显示其少数属性。在这种情况下,我们建议使用符号:prefixDTO。这是客户用来完成其工作的对象。服务获取域对象,创建 DTO 并将其返回。如果此 DTO 用于向导或公式类型中,用户可以在其中键入一些设置值,这意味着您必须将键入的信息存储到数据库中,那么您应该在 DTO 之外使用名为的新对象:prefixDTOData . 为什么?因为否则我们将强制将属性写入数据库的服务迭代整个域对象(请记住,域对象很大)在写入数据库之前查找和比较更改。

案例 2:如果客户端必须显示来自不同域对象的字段,则使用 DTO。在这种特殊情况下,您可以考虑将所有需要的属性放在数据库中,在服务器中创建 DTO,该 DTO 被传递给前往客户端的服务。客户端为案例 1 创建一个前缀 DTOData 的类比,并将其返回给服务。原因同案例一中的解释。

于 2013-06-09T09:57:21.173 回答
2

它将数据从一层传输到另一层。您可以将其称为DTO(DATA TRANSFER OBJECT) orVO(VALUE OBJECT)`。

例如,如果我们填写一个包含用户详细信息的表单以保存在数据库中。第一个值将转到操作类。然后我需要通过服务将它发送到 DAO。所以在操作类中,我会将所有详细信息设置为一个类然后在服务类中访问它

于 2013-06-09T09:55:30.240 回答