简体   繁体   中英

Should I create multiple DTOs for the same entity?

(Sorry my English is so bad).

The user entity in my spring boot project has the following basic fields: id, name, email, role, password. Admin can create new employee (including password field), update employee (except for password field), view employee information (except for password field). Employees can edit their information(except for role field). If I create only one DTO, will it be used for all of the above actions? Should I create a DTO for each of those actions?

If I create only one DTO, will it be used for all of the above actions?

Not unless you write code to make that happen. You could have a single DTO for any number of actions if you wanted, but Spring does not impose that. Creating a DTO will not make it be used for all of the actions.

Should I create a DTO for each of those actions?

Generally, no.

(I am french so please excuse my english if it is incorrect, I do my best).

I would create one DTO per "display" case. But it is not an obligation.

You can use Single DTO for all those operations and exclude that particular field which performing the operation. Like when Employee is editing their information then exclude the input of role property while saving/updating the record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM