2

I'm using ModelMapper to do straight-forward maps between objects.

ModelMapper mapper = new ModelMapper();
// TypeMap created
mapper.map(sourceObj, destObj);

mapper = new ModelMapper();
// TypeMap is recreated
mapper.map(sourceObj, destObj);

I noticed in the source code that if a TypeMap doesn't exist between sourceObj and destObj that it will cache the TypeMap in getOrCreate method.

As far as I can tell this cache is tied to the mapper instance, meaning that if I have to map the sourceObj to destObj for second time using a new ModelMapper instance, that it will have to re-create the TypeMap.

I'm assuming this is a fairly expensive operation, say if I'm mapping a collection of sourceObj to destObj. This leads me to the conclusion that I should re-use the ModelMapper instance.

Is this conclusion correct?

thanks

1 Answer 1

9

Yes. There is even a section in the FAQ about this:

Should I reuse my ModelMapper instance?

Unless you need different mappings between the same types, then it’s best to re-use the same ModelMapper instance. If you use a dependency injection container, you can accomplish this by configuring ModelMapper as a singleton.

1
  • Thanks! Somehow missed that section.
    – Avner
    Commented Sep 20, 2016 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.