TypeMap.addMappings(PropertyMap) vs. TypeMap().addMappings(ExpressionMap)

312 views
Skip to first unread message

Malte Finsterwalder

unread,
Nov 18, 2019, 11:14:13 AM11/18/19
to modelmapper
Hi there,

when you look at the "Getting started"-guide or the UserManual, there are two ways to configure Mappings. One for Java 6/7 (using inline subclasses of PropertyMap) and one for Java 8 (using lambda expressions).
BTW: The lambda expression examples are unclear, since mm.addMapping(ExpressionMap) suggests ModelMapper.addMapping() is meant, which does not exist for ExpressionMap as a parameter.

So I thought those two approaches would produce the same result, but I found they don't.

Why are these two things not the same?

    ModelMapper mm = new ModelMapper();

    // New approach Java 8
    mm.typeMap(A.class, ADto.class).addMappings(mapper -> {
mapper.map(A::getSomeId, ADto::setSomeId);
mapper.map(src -> src.getSomething().getOtherId(), ADto::setOtherId);
});

    // Old approach Java 6/7
    mm.addMappings(new PropertyMap<A, ADto>() {
@Override
protected void configure() {
map().setSomeId(source.getSomeId());
map().setOtherId(source.getSomething().getOtherId());
}
});

The Java 8 version above gives an error stating: "ADto.setOtherId() matches multiple source propertiy hierarchies" ...
The Java 6/7 version does not produce this problem.

Maybe I completely misunderstood, but then it would be great to have an explanation, what the difference between the two approaches are and when to use which approach.

Greetings,
   Malte

Malte Finsterwalder

unread,
Nov 19, 2019, 5:23:27 AM11/19/19
to model...@googlegroups.com
Somewhat try answering my own question here...

After looking into this in more detail, I found that the new Java 8 approach creates a typeMap without a PropertyMap and then applies the given Mappings afterwards.
The TypeMap that is created without a PropertyMap now has the problem of ambiguous mappings. Those ambiguities will be resolved through mappings given later, but we never get there, of course.. :-/
The old Java 7 approach creates a typeMap with the given PropertyMap from the beginning and as such does resolve the ambiguity during creation and everything works fine...

At least that seams to be the problem.

I do like to Java 8 approach much better. Maybe there is a way to change ModelMapper in such a way, that the new approach also works in those situations? Maybe by moving the addMappins(ExpressionMap)-method to ModelMapper, removing the step, that the user of ModelMapper explicitly needs to request a typeMap first?

Greetings,
   Malte

--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modelmapper/563578ad-4543-454a-83b1-ff76737227fa%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages