Search code examples
springrestdto

User entity with two DTO's vs two entities - UserLogin, User


I am currently writing a project in Spring Boot. I'm wondering about login method. On front-end I would like to display all users with some detailed data like projects they participate etc. As I don't want to send password and some private data to front I found two solutions:

  1. Create two DTO's - one used for login, signup; second - for displaying infromation about user.
  2. Create two entities - UserLogin with data needed to signup and login, and User with rest of data. Then make a relation one-to-one with them.

Which approach is more proper?


Solution

  • In my opinion the first approach with DTOs is better and I use it also myself, because with DTOs you can efficiently transport only the data you need.

    You can easily create more DTOs that cover other combinations of User properties - such as a DTO for showing user information in a list of users for example (there you will maybe only need name and e-mail).

    In Spring docs they use a PersonForm DTO (they call it a form-backing object) to validate user registration.

    Thus, DTOs can cover various business-cases for the same Entity (registration, showing personal profile, showing list of Entities, etc.).

    On the other hand, if you use two separate database tables for your Users, you are adding unnecessary complexity to your program (e.g. you need to save/update/delete information on two places when making changes).

    Also, if you want to start adding new ways of displaying your User data, you will eventually start using DTOs either way (or making a separate database table for each case, which would result in lots of duplicate data).