开发者

Difference between DTO(data transfer object ) and class object in java?

开发者 https://www.devze.com 2023-03-03 01:14 出处:网络
We can use a class object for transfering data to another class. what is the speciality of data transfer objects? How to create them ? Is it just like cla开发者_如何学JAVAss objects?The primary differ

We can use a class object for transfering data to another class. what is the speciality of data transfer objects? How to create them ? Is it just like cla开发者_如何学JAVAss objects?


The primary difference is that DTOs, by design, don't have any business logic in them. They're just data structures.

For instance: You might have a database in which you store "users", and if using DTOs, you might use a UserBean to store and retrieve user objects. But your business logic may have a User object (possibly derived from the bean, more likely using the bean via aggregation) that not only has the data, but additional methods for things that User can do.


I believe this should be true:

assertTrue(POJO == DTO)

The only special thing about DTO is that they should not contain any behavior.


1, The class object maybe contains too many references to other objects, thus too big to be serialized to transfer. DTO only selects the interesting parts, this can be a performance gain.

2, In Hibernate, the entity object may contain lazy-initialized references, these objects need session context to do initialization. These entity objects looks like "smart objects", DTO here convert these "smart objects" to "plain objects", because transfer "smart objects" is meaningless when the session context is no more existed.

Personally, I don't like DTO, it introduce another layer of redundant, but sometime (especially when working with Hibernate ORM) I can't live without it.


A DTO class is an ordinary Java class with a just special meaning - just like a Observer, a Factory or a Model. The name is coming from a core J2EE design pattern (the Transfer Object pattern) and the pattern proposes a common way to transfer information between a database and java-classes-based model.

In brief, a DTO is a java class where the class name maps to a database table name and each database column maps to a class attribute. Then it contains getter and setter methods.

Here is one explanation of the (Data) Transfer Object pattern.

0

精彩评论

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

关注公众号