DTO Convection Diagram in Spring Download

The main question is how to convert DTOs into entities and entities into Dtos without violating 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 the 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 a useful Java 8 DTO convector model .

But in their example, there is no OneToMany relationship. To create a UserEntity I need to get Role by roleName using the dao layer (service level). Can I add a UserRepository (or UserService) to the convector. Since it seems that the converter component will break SRP , it should only convert, should 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;
   }

   ....

? /, DTO (, UserFactory)?

+7
6

, .
: mapper, .
, , , , Converter Service.

, .
, : , .

:

controller -> service -> converter 
                      -> repository

, ( ):

controller -> service ->  repository            

, , DTO, .
, ( ).

+2

Mapstruct, , . dto , , (@Autowired).

, , .

+1

. , User dto /, dto /. , . , - SOLID , , , .

, , , , , , - , ( , ). , .

+1

:

public class UserConverter implements Converter<UserEntity, UserDto> {
   private final Function<String, RoleEntity> roleResolver;

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

@Configuration
class MyConverterConfiguration {
  @Bean
  public Converter<UserEntity, UserDto> userEntityConverter(
               @Autowired RoleRepository roleRepository
  ) {
    return new UserConverter(roleRepository::findByRoleName)
  }
}

Converter<RoleEntity, String> .

, , ( . Converter<List<UserEntity>, List<UserDto>> , , - ( , ).

MapStruct ModelMapper, . , : ( : ), - :

@Configuration
class MyConverterConfiguration {

  @Bean
  public Mapper<UserDto, UserEntity> userDtoCnoverter(@Autowired RoleRepository roleRepository) {
      Mapper<UserDto, UserEntity> mapper = Datus.forTypes(UserDto.class, UserEntity.class)
        .mutable(UserEntity::new)
        .from(UserDto::getName).into(UserEntity::setName)
        .from(UserDto::getRole).map(roleRepository::findByRoleName).into(UserEntity::setRole)
        .build();
      return mapper;
  }
}

( db Collection<UserDto>

, SOLID , / , , SOLID .

+1

, Entity.

public class UserEntity {
    // properties

    public static UserEntity valueOf(UserDTO userDTO) {
        UserEntity userEntity = new UserEntity();
        // set values;
        return userEntity;
    }

    public UserDTO toDto() {
        UserDTO userDTO = new UserDTO();
        // set values
        return userDTO;
    }
}

;

UserEntity userEntity = UserEntity.valueOf(userDTO);
UserDTO userDTO = userEntity.toDto();

, . Spring BeanUtils . RoleEntity , / UserEntity ORM.

0

, , DTO , .

controllers <-> converters <-> services ... 

JPA .

0

Source: https://habr.com/ru/post/1691035/


All Articles