简体   繁体   English

使用BoundedMapperFacade进行包含其他对象的类的Orika映射

[英]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种解决方案:

  1. If the MtuInterfaceMap and MtuInterfaceMapUI classes have the same field set, there is no need to declare the classMap for them. 如果MtuInterfaceMapMtuInterfaceMapUI类具有相同的字段集,则无需为其声明classMap Orika will copy the list elements by default, mapping the fields by name; Orika默认会复制列表元素,并按名称映射字段。
  2. If the mapping is simple enough (eg copy values between fields with different names), classMap can be declared. 如果映射足够简单(例如,在具有不同名称的字段之间复制值), classMap可以声明classMap The mapping for the parent class will use it automatically when resolving the mapping strategy; 解决映射策略时,父类的映射将自动使用它。
  3. If custom mapping is required, one can write a custom converter and register it with the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM