Guice Integration

ModelMapper’s Guice integration allows for the provisioning of destination objects to be delegated to a Guice Injector during the mapping process.

Setup

To get started, add the modelmapper-guice Maven dependency to your project:

<dependency>
  <groupId>org.modelmapper.extensions</groupId>
  <artifactId>modelmapper-guice</artifactId>
  <version>3.0.0</version>
</dependency>

Usage

Let’s obtain a Guice integrated Provider, which will delegate to an Injector whenever called:

Provider<?> guiceProvider = GuiceIntegration.fromGuice(injector);

Then we can configure the Provider for to be used globally for a ModelMapper:

modelMapper.getConfiguration().setProvider(guiceProvider);

Or set the Provider to be used for a specific TypeMap:

typeMap.setProvider(guiceProvider);

The provider can also be used for individual mappings:

with(guiceProvider).map().someSetter(source.someGetter());