简体   繁体   中英

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

Use BoundMapperFacade to avoid repeated lookup of mapping strategy

So I am looking for something using the BoundedMapperFacade like below for better performance

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.

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. 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:

  1. If the MtuInterfaceMap and MtuInterfaceMapUI classes have the same field set, there is no need to declare the classMap for them. Orika will copy the list elements by default, mapping the fields by name;
  2. If the mapping is simple enough (eg copy values between fields with different names), classMap can be declared. 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 . In this case, the classMap definition for the parent class needs a hint to use this converter, with the fieldMap().converter() syntax. The custom converter can be written by extending eg 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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