Reputation: 119
I need a user management service for my spring-boot project. I generally understand the use of DTO (Data transfer object) in spring. But when I'm thinking to design the service, I use multiple DTOs for just one "User" model, such as UserDTO, RegisterUserDTO, UpdateUserDTO, ManagedUserDTO.. UserDTO is like a readonly data (output data with username, email, name) which is used to display user information. But RegisterUserDTO is like an input data (with password, confirmPassword to create a password for new user) which is used for user registration form. UpdateUserDTO is for Admin, since admin can be able to assign Authorities, enable or disable user.
Another reason might be because of validation, so that different hibernate annotation validator can be applied on different DTOs.
Am I doing right with using a lot of DTOs? Should it be like one DTO by using multiple spring validators for validation?
Is there any other options to simplify it?
Upvotes: 5
Views: 2581
Reputation: 1760
In my opinion having multiple DTOs is valid and leads to much cleaner logic.
User
object in different circumstancesUserDTO
contains general information pertaining to any user and RegisterUserDTO
can inherit UserDTO
without having to repeat the properties of UserDTO
and validation annotations.In addition to this you can also use Spring's conversion service to do the conversion for you between different DTOs and this conversion is totally decoupled from the core business logic. I see this not as an advantage but conversion service makes it less painful whenever there are many DTOs like this.
Upvotes: 6