Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTO conveter pattern in Spring Boot

The main question is how to convert DTOs to entities and entities to Dtos without breaking SOLID principles.
For example we have such json:

{ id: 1,
  name: "user", 
  role: "manager" 
} 

DTO is:

public class UserDto {
 private Long id;
 private String name;
 private String roleName;
}

And entities are:

public class UserEntity {
  private Long id;
  private String name;
  private Role role
} 
public class RoleEntity {
  private Long id;
  private String roleName;
}

And there is usefull Java 8 DTO conveter pattern.

But in their example there is no OneToMany relations. In order to create UserEntity I need get Role by roleName using dao layer (service layer). Can I inject UserRepository (or UserService) into conveter. Because it seems that converter component will break SRP, it must convert only, must not know about services or repositories.

Converter example:

@Component
public class UserConverter implements Converter<UserEntity, UserDto> {
   @Autowired
   private RoleRepository roleRepository;    

   @Override
   public UserEntity createFrom(final UserDto dto) {
       UserEntity userEntity = new UserEntity();
       Role role = roleRepository.findByRoleName(dto.getRoleName());
       userEntity.setName(dto.getName());
       userEntity.setRole(role);
       return userEntity;
   }

   ....

Is it good to use repository in the conveter class? Or should I create another service/component that will be responsible for creating entities from DTOs (like UserFactory)?

like image 976
Maksym Avatar asked Dec 19 '17 11:12

Maksym


People also ask

What are DTO in spring boot?

In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for the call.

What is DTO pattern?

2. The Pattern. DTOs or Data Transfer Objects are objects that carry data between processes in order to reduce the number of methods calls. The pattern was first introduced by Martin Fowler in his book EAA.

How do you convert entity to DTO using ModelMapper?

Thus, we can use model mapper to convert entity to dto or dto to entities. First, we need to add model mapper dependency. Next, we can create a @Bean factory method to create ModelMapper instance. This way, the model mapper instance will be available for injection on the application level.


1 Answers

I think the way to do it cleanly is to include a Role DTO that you convert to the RoleEntity. I might use a simplified User DTO in case that it is read only. For example, in case of unprivileged access.

To expand your example

public class UserDto {
 private Long id;
 private String name;
 private RoleDto role;
}

with the Role DTO as

public class RoleDto {
  private Long id;
  private String roleName;
}

And the JSON

{ 
  id: 1,
  name: "user", 
  role: {
   id: 123,
   roleName: "manager"
}

Then you can convert the RoleDto to RoleEntity while converting the User in your UserConverter and remove the repository access.

like image 174
Sebastian Gamaga Avatar answered Sep 28 '22 20:09

Sebastian Gamaga