BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News ModelMapper: An Object To Object Mapping Library

ModelMapper: An Object To Object Mapping Library

This item in japanese

Bookmarks

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.

Rate this Article

Adoption
Style

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.

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

Community comments

  • How does this compare with other alternatives?

    by goo osi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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


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

  • Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: nice

    by Kari Aba,

    Your message is awaiting moderation. Thank you for participating in the discussion.

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

  • Re: nice

    by Raja Nagendra Kumar,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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

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

BT