[英]Orika Mapping using BoundedMapperFacade for classes containing other objects
I have classes of the form - 我有以下形式的课程-
class EdgeMtuMismatchEvent {
private final List<MtuInterfaceMap> list = new ArrayList<>();
private int id;
// public getters and setters
}
I have to map the above class to something like below 我必须将上面的类映射到下面的内容
class EdgeMtuMismatchEventUI {
private final List<MtuInterfaceMapUI> list = new ArrayList<>();
private int id;
// public getters and setters
}
I know I can have the mapper like below 我知道我可以像下面这样映射
final DefaultMapperFactory factory = new DefaultMapperFactory.Builder().build();
factory.classMap(MtuInterfaceMap.class, MtuInterfaceMapUI.class).byDefault().register();
factory.classMap(EdgeMtuMismatchEvent.class, EdgeMtuMismatchEventUI.class).byDefault().register();
//factory.getMapperFacade().map()
As Orika performance tuning guide says 正如Orika性能调整指南所说
Use BoundMapperFacade to avoid repeated lookup of mapping strategy
使用BoundMapperFacade避免重复查找映射策略
So I am looking for something using the BoundedMapperFacade
like below for better performance 所以我正在寻找使用
BoundedMapperFacade
东西,如下所示,以获得更好的性能
BoundMapperFacade<EdgeMtuMismatchEvent, EdgeMtuMismatchEventUI> facade = factory.getMapperFacade(EdgeMtuMismatchEvent.class, EdgeMtuMismatchEventUI.class, false)
I am not able to figure out how can I add the mapper for MtuInterfaceMap
in the above code snippet. 我无法弄清楚如何在上面的代码片段中为
MtuInterfaceMap
添加映射器。
Can anyone suggest anything? 有人可以建议什么吗?
BoundMapperFacade
will lazily resolve the mapping strategy from the mapper factory and cache it on first invocation of the map()
method. BoundMapperFacade
将懒惰地解决映射器工厂中的映射策略,并在第一次调用map()
方法时对其进行缓存。 So all the required mapping definitions should be registered with the mapper factory at that time. 因此,所有必需的映射定义都应在那时向映射器工厂注册。
Depending on what is required, 3 solutions are available: 根据需要,可以提供3种解决方案:
MtuInterfaceMap
and MtuInterfaceMapUI
classes have the same field set, there is no need to declare the classMap
for them. MtuInterfaceMap
和MtuInterfaceMapUI
类具有相同的字段集,则无需为其声明classMap
。 Orika will copy the list elements by default, mapping the fields by name; classMap
can be declared. classMap
可以声明classMap
。 The mapping for the parent class will use it automatically when resolving the mapping strategy; MapperFactory
. MapperFactory
。 In this case, the classMap
definition for the parent class needs a hint to use this converter, with the fieldMap().converter()
syntax. classMap
定义需要提示以使用此转换器,并且使用fieldMap().converter()
语法。 The custom converter can be written by extending eg BidirectionalConverter<List<MtuInterfaceMap>, List<MtuInterfaceMapUI>>
. BidirectionalConverter<List<MtuInterfaceMap>, List<MtuInterfaceMapUI>>
来编写自定义转换器。 The example code can be written as follows: 示例代码可以编写如下:
final DefaultMapperFactory factory = new DefaultMapperFactory.Builder().build();
// (1) auto-mapping
// nothing here
// (2) if the scenario is simple enough
factory.classMap(MtuInterfaceMap.class, MtuInterfaceMapUI.class)
.field("comment", "details")
.byDefault()
.register();
// (3) if converter is required
final String listConverterId = "listConverter";
factory.getConverterMap()
.registerConverter(listConverterId , new MtuInterfaceMapListConverter());
//
factory.classMap(EdgeMtuMismatchEvent.class, EdgeMtuMismatchEventUI.class)
.fieldMap("list", "list").converter(listConverterId).add() // for case (3) only - declare converter
.byDefault()
.register();
BoundMapperFacade<EdgeMtuMismatchEvent, EdgeMtuMismatchEventUI> facade =
factory.getMapperFacade(EdgeMtuMismatchEvent.class,
EdgeMtuMismatchEventUI.class,
false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.