Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create DTO class

Tags:

java

I want to create DTO class for User. my input to program is firstname, lastname,lastname.role,group1,group2,group3.

so for each user role consist of group_1,group_2,group_3.....

In database i want to store in following format demo,demo,demo,roleId, gorup_1_name group_1_Id demo,demo,demo,roleId, gorup_2 and group_2_Id demo,demo,demo,roleId, gorup_3 and group_3_Id

I was able separate all this things , but i want to assign this value to userDTO class and stored into database. basically im new to core java part. so how can create structure for this?

like image 495
Raje Avatar asked Dec 06 '22 20:12

Raje


2 Answers

A Data Transfer Object (DTO) class is a java-bean like artifact that holds the data that you want to share between layer in your SW architecture.

For your usecase, it should look more or less like this:

public class UserDTO {
    String firstName;
    String lastName;
    List<String> groups;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<String> getGroups() {
        return groups;
    }
    public void setGroups(List<String> groups) {
        this.groups = groups;
    }
    // Depending on your needs, you could opt for finer-grained access to the group list

}
like image 119
maasg Avatar answered Dec 09 '22 09:12

maasg


One thing to add:

The essence of a DTO is that it transfers data across the wire. So it will need to be Serializable.

http://martinfowler.com/eaaCatalog/dataTransferObject.html

like image 22
Jonathan Avatar answered Dec 09 '22 10:12

Jonathan