Java 传输对象,它是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17008416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:41:43  来源:igfitidea点击:

Java Transfer Object, what is it?

javajakarta-eedesign-patternsdao

提问by Rollerball

Directly from this oracle articleabout the J2EE DAO Pattern:

直接来自这篇关于 J2EE DAO 模式的oracle 文章

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.

传输对象

这表示用作数据载体的传输对象。DataAccessObject 可以使用传输对象将数据返回给客户端。DataAccessObject 也可以在 Transfer Object 中接收来自客户端的数据以更新数据源中的数据。

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).

我正在尝试将此模式用作练习(作为 OCPJP 考试的学生,它需要了解 DAO 模式)。到目前为止,我的数据源(mysql 数据库)、我的业务对象(称为 Person 的 JavaBean)和我的 DAO 对象在数据库和 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.

提前致谢。

回答by Grzegorz ?ur

Transfer object is a simple class with fields and NO logic.They are serializablePOJOs (Plain Old Java Objects) and have accessors (getters, setters) to access fields. They are called transfer because they are used to pass data between layersor roughly speaking group arguments to service method calls, they need not to match business objects.

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

Example

例子

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) {...}
}

Transfer Objects are distinguished from other structure-like classes by the way they are used (transferring data) not by the way they are build (fields and accessors).

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

回答by PSR

It will transfer the data from one layer to another layer.You can call it as DTO(DATA TRANSFER OBJECT) orVO(VALUE OBJECT)`.

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

For example if we fill a form contains user details to save in the databse.First values will go to the action class.Then i need to send it to the DAO through service.So in action class i will set all the details to a class and then access it in service class

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

回答by Juned Ahsan

DTO

DTO

DTO does not have any behavior except for storage and retrieval of its own data (accessors and mutators). DTOs are simple objects that should not contain any business logic that would require testing.

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

DTO Usage

DTO 使用

Case 1: Use DTO's if the domain object is huge, but you just need a few attributes of it! For large domain objects, in which the client visualize, change or display only a few of its attributes. In this case we recommend to use the notation: prefixDTO. This is the object which the client uses to do its works. The service gets the Domain Object, creates the DTO and gives it back. If this DTO is used in a wizard or kind of formular in which the user may type something in setting values, which means that you have to store the typed information into database, then you should use in addtion to the DTOs new objects called: prefixDTOData. why that? Because otherwise we would force the service which writes the attributes into database to iterate over the whole domain object (remember that the domain object is huge) looking and comparing for changes before writing down in the database.

Case 2: Use DTOs if the client has to show fields from different domain objects. In this special case you may consider putting all the needed attributes together already in the database creating the DTO in the server which is passed to the service going to the client. The client creates analoque to Case 1 a prefixDTOData an gives it back to the service. the reasons are the same as in explained in Case 1.

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

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