-1

I found some information of Design pattern DTO from the Wikipedia. I saw some discussions in other StackOverflow discussions. But couldn't get programmatic understanding of how to create and use DTO.

I wanted to know about:

  1. What makes this a “Design Pattern”?
  2. When to use this pattern?

Any source that could be helpful for a newbie to learn regarding DTO is highly appreciated.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 1
    `@Entity` classes are the DTOs in Java EE. – Matt Ball Mar 14 '13 at 05:21
  • 1
    You generally use a DTO when transferring data from some place to another, like entity classes in a Web Service or in a JMS queue message. – Luiggi Mendoza Mar 14 '13 at 05:23
  • 1
    Maybe related question: [Is DTO pattern deprecated or not?](http://stackoverflow.com/q/11237946/1065197) – Luiggi Mendoza Mar 14 '13 at 05:31
  • 1
    Well, if you want to understand DTO as a design pattern, just have a look at the average (or somewhat below average) script kiddie stuff produced by not-so-experienced programmers. There you'll find lots of anonymous hashes and (PHP) arrays all over the place and given from one class and method to the next like a sailor's harlot. At the end, noone really knows when and why this "object" ends up with specific content etc. So, please (!), always properly design and *define* your DTO. You could define that it is only produced by the corresponding DAO and beyond that read-only. – user1050755 Mar 14 '13 at 05:54

1 Answers1

3

1, DTO is not a design pattern. Accurately say, it just a technology.
DTO stand for Data Transfer Object.
2, you need to use transfer data from database to other places not use ResultSet, DTO may be a better choice.
3,DTO general application in the software development of multi-layer architecture,eg MVC.

Ex:

class User{
    private String id;
    private String age;
    private String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}    


class DateAccess{   
    public User getUserInfo(String id){   
        User user= new User();
        String sql ="select id,name ,age from user where id =?";
        ResultSet rs = query(sql,id);
        while(rs!=null&&rs.next()){
            user.setId(rs.getString("id"));
            user.setName(rs.getString("name"));
            user.setAge(rs.getString("age"));
        }
        return user;
  }
}
Mark Yao
  • 408
  • 2
  • 6