gpt4 book ai didi

java - Spring Boot 中的 DTO 转换器模式

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:24:36 25 4
gpt4 key购买 nike

主要问题是如何在不破坏的情况下将DTO 转换为实体 并将实体转换为Dto SOLID原则。
例如我们有这样的 json:

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

DTO 是:

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

实体是:

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

还有有用的Java 8 DTO conveter pattern .

但是在他们的示例中没有 OneToMany 关系。为了创建 UserEntity,我需要使用 dao 层(服务层)通过 roleName 获取 Role。我可以将 UserRepository(或 UserService)注入(inject)到转换器中吗?因为转换器组件似乎会损坏 SRP ,它只能转换,不能知道服务或存储库。

转换器示例:

@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)?

最佳答案

尽量将转换与其他层解耦:

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>>,这看起来可能是一个转换单个对象时有点麻烦,但您现在可以批量处理您的数据库请求,而不是一个一个地查询 - 用户不能错误地使用所述转换器(假设没有恶意)。

看看MapStructModelMapper如果您想在定义转换器时更加舒适。最后但并非最不重要的一点是给datus一个镜头(免责声明:我是作者),它可以让您以流畅的方式定义您的映射,而无需任何隐式功能:

@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;
}
}

(此示例在转换 Collection<UserDto> 时仍会遇到数据库瓶颈

我认为这将是最 SOLID 的方法,但是给定的上下文/场景正在遭受无法提取的依赖性以及对性能的影响,这让我认为在这里强制使用 SOLID 可能不是一个好主意。这是一个权衡

关于java - Spring Boot 中的 DTO 转换器模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47886322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com