BT

ModelMapper: An Object To Object Mapping Library

by Kostis Kapelonis on Nov 19, 2012 |

ModelMapper, is an object-to-object framework that converts Java Beans (Pojos) from one representation to another. It automates different object mappings with a "convention follows configuration" approach allowing at the same time advanced functionality for cases with special needs. It is similar (but not a direct port) to the AutoMapper library for .NET.

ModelMapper can map a Java object on another with much more compact code and zero configuration in the simpler cases. It supports:

  • Name based mapping of object properties
  • Copying of public, protected and private fields
  • Skipping certain fields
  • Converters that affect mapping (e.g. converting strings to lowercase)
  • Mapping between fields of different types (e.g. from a string to a number)
  • Mapping using conditions
  • Loose mapping strategy when the default is not enough
  • Validation of the mapping process so that all fields are accounted for
  • Complete custom control of the mapping process for special cases
  • Integration with Guice or Spring

Transforming objects from one representation to another is a common pattern in Enterprise applications. An example would be domain objects that are loaded from the database and must be shown to the user in the GUI. An object in its original database format will contain a lot of properties for its lifecycle, while the user might only be interested in one or two fields at each screen. So several times the Pojo used in the database (the JPA entity) is a bit different from the Pojo used in the GUI. This is the problem that ModelMapper attempts to solve. In general object transformations are observed when information changes layers inside an Enterprise application.

Other scenarios that involve object transformations are:

  • Aggregating multiple objects into one
  • Computing some additional metadata on an existing object
  • Transforming objects for sending to external systems
  • Filling default values on undefined properties
  • Converting the existing properties in some way (mapping an object to itself)

ModelMapper is available in Maven Central. For more information see the user guide and Javadoc. The source code is on GitHub.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

How does this compare with other alternatives? by goo osi

I remember using some beanutils or dozer in the past for similar scenarios. How does model mapper compare with other similar frameworks?

Re: How does this compare with other alternatives? by Jonathan Halterman

ModelMapper will intelligently map many objects to each other, even if their property names don't match exactly. Ex:

modelmapper.org/getting-started/

Dozer requires XML configuration for this and BeanUtils only converts objects whose property names match exactly. For use cases that require specific mappings between properties, ModelMapper provides a mapping API that uses actual code rather than XML or String references, making the mappings refactoring safe and type-safe:

modelmapper.org/user-manual/property-mapping/

nice by Petar Shomov

This looks nice, we worked on something similar a few years back. Also code based and rather flexible - github.com/pshomov/charon.

Interesting and useful by Fred Amiter

For advanced purposes users need to understand PropertyMap and its proxy trickery. I'd probably write

D dest = map();
dest.setName(source.getFirstName());

dozer by ali akbar azizkhani

i think dozer is best framework for object mapping
github.com/DozerMapper/dozer

Re: nice by Kari Aba

Take a look at Orika Mapper, very flexible and fast mapper.

Re: nice by Raja Nagendra Kumar

Orika Mapper is too intuitive to code but not working with android also, the dependencies are too many and too much footprint size for andorid.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

7 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT