Scala 有类似 AutoMapper 的东西吗?
我一直在寻找一些用于映射对象到对象的 scala 流畅 API,类似于 AutoMapper。 Scala中有这样的工具吗?
I have been looking for some scala fluent API for mapping object-object, similar to AutoMapper.
Are there such tools in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 Scala 中不太需要像 AutoMapper 这样的东西,因为如果您使用惯用的 Scala 模型,则更容易编写和操作,并且您可以使用隐式转换轻松定义自动展平/投影。
例如,这里是 AutoMapper 的 Scala 扁平化示例 中的等效项:
PS:我不应该使用金额翻倍...
I think there's less need of something like AutoMapper in Scala, because if you use idiomatic Scala models are easier to write and manipulate and because you can define easily automatic flattening/projection using implicit conversions.
For example here is the equivalent in Scala of AutoMapper flattening example:
PS: I should not use Double for money amounts...
我同意@paradigmatic,确实使用 Scala 代码会更干净,但有时您会发现自己在看起来非常相似的案例类之间进行映射,这只是浪费按键。
我已经开始开发一个项目来解决这些问题,您可以在这里找到它:https://github。 com/bfil/scala-automapper
它使用宏为您生成映射。
目前,它可以将案例类映射到原始案例类的子集,它处理可选值、可选字段以及其他次要事物。
我仍在尝试弄清楚如何设计 api 以支持使用自定义逻辑重命名或映射特定字段,任何想法或输入都会非常有帮助。
它现在可以用于一些简单的情况,当然,如果映射变得非常复杂,那么手动定义映射可能会更好。
该库还允许在任何情况下手动定义案例类之间的
Mapping
类型,这些类型可以作为隐式参数提供给AutoMapping.map(sourceClass)
或sourceClass .mapTo[TargetClass]
方法。更新
我刚刚发布了一个新版本,可以处理 Iterables、Maps 并允许传递动态映射(例如支持重命名和自定义逻辑)
I agree with @paradigmatic, it's true that the code will be much cleaner using Scala, but sometimes you can find yourself mapping between case classes that look very similar, and that's just a waste of keystrokes.
I've started working on a project to address the issues, you can find it here: https://github.com/bfil/scala-automapper
It uses macros to generate the mappings for you.
At the moment it can map a case class to a subset of the original case class, it handles optionals, and optional fields as well as other minor things.
I'm still trying to figure out how to design the api to support renaming or mapping specific fields with custom logic, any idea or input on that would be very helpful.
It can be used for some simple cases right now, and of course if the mapping gets very complex it might just be better defining the mapping manually.
The library also allows to manually define
Mapping
types between case classes in any case that can be provided as an implicit parameter to aAutoMapping.map(sourceClass)
orsourceClass.mapTo[TargetClass]
method.UPDATE
I've just released a new version that handles Iterables, Maps and allows to pass in dynamic mappings (to support renaming and custom logic for example)
对于复杂的映射,人们可能需要考虑基于 Java 的映射器,例如
Scala 对象可以从 Java 访问:
复杂对象的隐式转换的实现将更顺畅地声明性映射而不是手工映射。
在这里找到了更长的列表:
http://www.javacodegeeks.com/2013/10/java-object-to-object-mapper.html javacodegeeks.com/2013/10/java-object-to-object-mapper.html
For complex mappings one may want to consider Java based mappers like
Scala objects can be accessed from Java:
Implementations of implicit conversions for complex objects would be smoother with declarative mappings than handcrafted ones.
Found a longer list here:
http://www.javacodegeeks.com/2013/10/java-object-to-object-mapper.html