Skip to content
rfreier edited this page Apr 13, 2018 · 1 revision

Bean-Mapping

For decoupling you sometimes need to create separate objects (beans) for a different view. E.g. for an external service you will use a transfer-object instead of the persistence entity so internal changes to the entity do not implicitly change or break the service.

Therefore you have the need to map similar objects what creates a copy. This also has the benefit that modifications to the copy have no side-effect on the original source object. However, to implement such mapping code by hand is very tedious and error-prone (if new properties are added to beans but not to mapping code):

public PersonTo mapPerson(PersonEntity source) {
  PersonTo target = new PersonTo();
  target.setFirstName(source.getFirstName());
  target.setLastName(source.getLastName());
  ...
  return target;
}

Therefore we are using a BeanMapper for this purpose that makes our lives a lot easier.

Bean-Mapper Dependency

To get access to the BeanMapper we use this dependency in our POM:

    <dependency>
      <groupId>io.oasp.java</groupId>
      <artifactId>oasp4j-beanmapping</artifactId>
    </dependency>

Bean-Mapper Usage

Then we can get the BeanMapper via dependency-injection what we typically already provide by an abstract base class (e.g. AbstractUc). Now we can solve our problem very easy:

PersonEntity person = ...;
...
return getBeanMapper().map(person, PersonTo.class);

There is also additional support for mapping entire collections.

Dozer has been configured as Spring bean in the file src/main/resources/config/app/common/beans-dozer.xml.

Clone this wiki locally