remap icon indicating copy to clipboard operation
remap copied to clipboard

Extend mapper for parent class to map child class

Open michalre opened this issue 7 years ago • 7 comments

Thanks for the great declarative mapper. When there is Child class that extends Parent class, how can I define mappers for them so that mapping for parent is not duplicated in child mapper?

michalre avatar Apr 28 '18 22:04 michalre

Currently there is no support but maybe you can use the example in the test sources of the mapper: https://github.com/remondis-it/remap/blob/develop/src/test/java/com/remondis/remap/inheritance/MapperTest.java

Have a look at the test case shouldReuseParentMapperConfig() which tries to reuse a mapper configuration for the inherited class. This way you can externalize the mapping configuration and apply it to the child mapper like it was done with parentMappingConfig().

Maybe this works for you, but I also see that this may result in a lot of code to manage when the object hierarchies grow.

Let me know if you have ideas how this could be supported.

schuettec avatar Apr 29 '18 20:04 schuettec

Thanks, this will help at least partially in my case. It would be nice if after calling .useMapper(parentMapper) the child mapper would automatically use the parentMapper for fields from the parent, but I am not sure if it does not go against some principles of remap.

michalre avatar Apr 29 '18 22:04 michalre

Definitely a nice idea. But feels non-trivial.

schuettec avatar Apr 30 '18 08:04 schuettec

Hm basically the mapper would need the feature provided in the example above. But the next feature request would be "How to inherit just a few field mappings from the parent mapping and exclude others?" and I don't know how to accomplish that.

My idea is that the mapper could provide the following method, but this would inherit all mappings and there is no way to exclude some fields.

/**
   * Inherits the mapping configuration from another mapper, that maps the super types of the source and destination
   * types of this mapper.
   * 
   * @param parentMapper The mapper providing the mapping configuration for super types.
   */
  public void inheritMapper(Mapper<? super S, ? super D> parentMapper) {...}

schuettec avatar Apr 30 '18 20:04 schuettec

Yeah, I think the usual convention is that if child mapper defines mapping for some field from the inherited parent mapper, then this child mapping should take precedence for that field, similarly to how method overriding works.

michalre avatar May 02 '18 10:05 michalre

FYI A year later :D I tried to implement the first step to support inheritance #114

The next step is to give the user a chance to override existing field mappings.

schuettec avatar Aug 14 '19 08:08 schuettec

Great, looks good to me.

michalre avatar Aug 20 '19 21:08 michalre