java 通用 DTO 转换器模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17413788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic DTO converter Pattern
提问by janatar
I have many DTO objects. Every dto class have the methods
我有很多 DTO 对象。每个 dto 类都有方法
convertDTO(Entity entity)
convertDTOList(List<Entity> entity)
convertDTO(Entity entity)
convertDTOList(List<Entity> entity)
I want to use a desing pattern for my dto object converter. Which desing pattern I can use and how?
我想为我的dto 对象转换器使用设计模式。我可以使用哪种设计模式以及如何使用?
Dozer framework is good. But I want to write a generic pattern.
推土机框架很好。但我想写一个通用模式。
回答by Fabrizio Stellato
If you use Java8, I'd suggest to use DTO to domain converter pattern as suggested here
如果您使用 Java8,我建议按照此处的建议使用 DTO 到域转换器模式
Below an implementation example:
下面是一个实现示例:
GenericConverter
通用转换器
public interface GenericConverter<I, O> extends Function<I, O> {
default O convert(final I input) {
O output = null;
if (input != null) {
output = this.apply(input);
}
return output;
}
default List<O> convert(final List<I> input) {
List<O> output = new ArrayList<O>();
if (input != null) {
output = input.stream().map(this::apply).collect(toList());
}
return output;
}
}
ConverterDTO
转换器DTO
public class AccountCreateRequestConverter implements GenericConverter<AccountCreateRequest, AccountOutput> {
@Override
public AccountOutput apply(AccountCreateRequest input) {
AccountOutput output = new AccountOutput();
output.setEmail(input.getEmail());
output.setName(input.getName());
output.setLastName(input.getLastName());
output.setPassword(input.getPassword());
return output;
}
}
Consumer
消费者
The consumer class:
消费类:
class Consumer {
@Inject
AccountCreateRequestConverter accountCreateInputConverter;
void doSomething() {
service.registerAccount(accountCreateInputConverter.apply(input));
}
}
The strength of this pattern come from the easiness to use, because you could pass either a single or a list of entities, in which there can be other nested DTO to convert using their dedicated converters inside the converter parent class. Something like this:
这种模式的优势在于易于使用,因为您可以传递单个实体或实体列表,其中可以有其他嵌套的 DTO 使用转换器父类中的专用转换器进行转换。像这样的东西:
nested collection DTO converter example
嵌套集合 DTO 转换器示例
class ParentDTOConverter {
@Inject
ChildDTOConverter childDTOConverter;
void doSomething() {
@Override
public ParentDTOOutput apply(ParentDTOInput input) {
ParentDTOOutput output = new ParentDTOOutput();
output.setChildList(childDTOConverter.convert(input.getChildList()));
}
}
}
回答by Luis Rivera
There are many different solutions. You can find a discussion about it here Object Conversion Pattern
有许多不同的解决方案。你可以在这里找到关于它的讨论 对象转换模式
回答by Marconius
I'm not quite sure what you're trying to do here, but based purely on what you wrote, I'd say make an interface that is implemented by all your DTOs, possibly with generic parameters.
我不太确定你在这里尝试做什么,但纯粹基于你写的内容,我会说创建一个由你所有的 DTO 实现的接口,可能带有通用参数。