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:
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;classMap
can be declared. The mapping for the parent class will use it automatically when resolving the mapping strategy;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 e.g. 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);