orika-mapper/orika — pull request #31 by matt-deboer (2014-01-23 07:35:36+00:00)
Title: Abstract mappers ('favorExtension')

This adds the capability to define mappings and/or exclusions at the level of some shared parent class, and have that mapping inherited by any auto-generated (dynamic) mappers.

Previously, when a mapping was to be reused, it had to be explicitly called out by the **use(Class, Class)** method on ClassMapBuilder, which implies that any mapping wanting to inherit it had to be explicitly declared. This is fine if you only have a few inheriting classes; but for some, the mapping they wanted to reuse was inherited by most of their object model, meaning they had to declare many mappings explicitly, only for the sake of the **use** declaration.

If you did not declare these mappings explicitly, Orika *would* use the declared mapping--but it would not dynamically map the child types; all you get is the mapping for the parent types (usually not what you want in this type of scenario).

Now, by declaring **favorExtension(true)** on DefaultMapperFactory.Builder, you can tell Orika to 'favor extension', meaning that a declared class-map should be dynamically extended where possible; for example, if you've explicitly declared a mapping for A <-> B, and you then call a mapping function to map A1 <-> B1 (where A1 extends A, and B1 extends B), Orika should dynamically generate the mapping for A1 <-> B1, and the new mapping should include the mapping from A <-> B in the 'used mappers' (i.e., inherit it).

Of course, you can also **favorExtension(true|false)** explicitly on an individual ClassMapBuilder definition.
The default value is *false*, which preserves the current behavior for backward compatibility.