java 从域对象生成 DTO 和映射器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2828403/
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
DTO and mapper generation from Domain Objects
提问by Nicolas C
I have plenty of java domain objects that I need to transform to DTOs.
我有很多需要转换为 DTO 的 Java 域对象。
Please, don't start with the anti-pattern thing, the Domain Objects are what they are because of a long history, and I can't modify them (or not too much, see below).
请不要从反模式的事情开始,领域对象是因为历史悠久,我不能修改它们(或不能太多,见下文)。
So, of course, we've passed the age of doing all that manually. I've looked around, and dozer seems the framework of choice for DTO mapping.
所以,当然,我们已经过了手动完成所有这些工作的时代。我环顾四周,推土机似乎是 DTO 映射的首选框架。
But... what I'd really like is this: annotate classes and fields that I want in DTO, and run a tool that would generate the DTOs and the mappers.
但是……我真正喜欢的是:在 DTO 中注释我想要的类和字段,并运行一个工具来生成 DTO 和映射器。
Does that sound too unreasonable?
这听起来是不是太不合理了?
Does such a tool already exist?
这样的工具是否已经存在?
采纳答案by Jonathan
Consider checking out ModelMapper.
考虑检查ModelMapper。
It differs from Dozer and others in that it minimizes the amount of configuration needed by intelligently mapping object models. Where configuration is needed, ModelMapper offers a refactoring safe API that uses actual code to map properties and values rather than using string references or XML.
它与 Dozer 和其他产品的不同之处在于,它最大限度地减少了智能映射对象模型所需的配置量。在需要配置的地方,ModelMapper 提供了一个重构安全 API,它使用实际代码来映射属性和值,而不是使用字符串引用或 XML。
Check out the ModelMapper site for more info:
查看 ModelMapper 站点以获取更多信息:
回答by Gunnar
You might be interested in MapStruct, a code generator for JavaBeans mappers. You'd have to implement source model (e.g. your domain objects) and target model (e.g. DTOs), and MapStruct generates type-safe and fast code for mapping between these models (disclaimer: I'm the author of this project).
您可能对MapStruct感兴趣,它是 JavaBeans 映射器的代码生成器。您必须实现源模型(例如您的域对象)和目标模型(例如 DTO),并且 MapStruct 生成类型安全且快速的代码以在这些模型之间进行映射(免责声明:我是该项目的作者)。
回答by pbuchheit
This thread is a bit old, but if anyone is is still trying to do this, http://www.dtonator.org/is the best option I have found so far. It is the only tool I have been able to find that actually CREATES the DTO files and isn't tied to a specific framework (Spring, Seam, etc.) or IDE.
这个线程有点旧,但如果有人仍在尝试这样做,http://www.dtonator.org/是我迄今为止找到的最佳选择。它是我能找到的唯一一个实际创建 DTO 文件并且不依赖于特定框架(Spring、Seam 等)或 IDE 的工具。
回答by Denis
Use GeDA - much faster and more flexible than all of the mentioned mappers. http://inspire-software.com/confluence/display/GeDA/GeDA+-+Generic+DTO+Assembler
使用 GeDA - 比所有提到的映射器更快、更灵活。 http://inspire-software.com/confluence/display/GeDA/GeDA+-+Generic+DTO+Assembler
There aren't many examples on the wiki but the junits (in source) are full of them
wiki 上的例子并不多,但 junit(在源代码中)充满了它们

