Using ModelMapper in Grails

using modelmapper in grailsUsing ModelMapper in Grails for mapping domain object to DTO/POGO/POJO object.  ModelMapper does good job mapping faster and effectively. However I come to know via author of library, ModelMapper faces issue with Groovy/Android byte code handling while PropertyMap creation.  Default configuration works very well, however real-world usage never satisfied with default configuration in software industry, it’s a well known factor.

PropertyMap is used to configure ModelMapper library for your application object mapping such as deep mapping, handling mismatch types, skipping properties, conditional mapping, string mapping, etc.

Article will guide you to configure and create handy Groovy/Grails Meta methods for application – Using ModelMapper in Grails.


Steps to Achieve it

Version Info

Typically it should work in upcoming versions too, if you face any issues and share its resolution you have taken via comments.

Configuring ModelMapper

BuildConfig.groovy or if your Gradle build tool then build.gradle

Define bean in resources.groovy

Autowired/Inject a ModelMapper bean wherever need

At this point your application is ready to use ModelMapper in your Grails application like below, also you can leverage other methods appropriately for your need.

How to achieve property Skip while object mapping, i.e. this article goal

We will be achieving properties skip using PropertyCondition and TypeMap.

Defining ModelMapper PropertyCondition

Better place to put this Util class or Helper Class in your application

Defining helper method of createTypeMap, place method where you’re globally configuring ModelMapper. Typical spots are Bootstrap.groovy or dedicated  ApplicationInitializeService.groovy

Define Object Mapping

Utilizing above definition for ModelMapper configuration like Domain to DTO/POGO/POJO. Let’s say have method called initializeModelMapper to abstract this configuration into a method.

Calling Map method

That’s it, we have achieved Using ModelMapper in Grails with Skipping properties!! Wow 🙂


How about Groovyness with Collection and Solo Mapping

Groovy provides meta programming to extends Class capabilities.  Let’s utilize and do some Groovyness.

  • Add support of as keyword for Grails Domain to DTO/POGO/POJO
  • Add following methods to Grails Domain class, Collection, ArrayList, Hashset and TreeSet

Note: Like I said in the article before. All these methods should goto Bootstrap.groovy or your custom initialize service class.

Defining Collection to DTO/POGO/POJO method

Defining Closures

Initializing Meta methods

That’s it we did it. We can comfortably invoke methods as described below-

Grails Domain Class has –
  • Native as keyword support. For e.g.: student domain object then use ‘student as StudentDto’ will return StudentDto object
  • toDto(<DTO class>)
  • toDto(<DTO class>, typeMapName)
Collection, ArrayList, HashSet, TreeSet has –
  • toDto(<DTO class>)
  • toDto(<DTO class>, typeMapName)

Also I have created Gist of this article code here or Clone URI

Have fun and share your thoughts via comments – Using ModelMapper in Grails!!