Butjadinger Str. 34a 28197 Bremen
++49 173 2494181
wolff@manfred-wolff.de

Converter Populator

Converter Populator

Converter and Populator

Introduction

For a better separation of concerns it is good practice to provide view objects which are independent from the business logic; the solution ist the Converter and Populator pattern. The question is: How to fill. Inspired from SAP Commerce Cloud and a video from Michael Albrecht – only in German (but there is also a link to an English version) I implemented a converter/populater pattern with some lines of code in java. From my point of view not the implementation is important but the pattern itself. In the above Video Michael explains how it can help in real software projects.

For each Bean (Entity, Business Object, what else) you provide a Populator object, which populates the properties, which should be shown in the view. A converter collects all the populators which are necessary to fulfill the work.

Converter an Populator concrete

You want to provide contact details as shown in Michaels video the Converter and Populator pattern helps you a lot. The Name is taken from a User-Object, the Address of an Address-Object. All data are collected in a ContactView-Object. So you provide a populator, which populates the prename, lastname from Customer-Object. You provide a second polulator, which provides the address details from the Address-Object. You can stick everything together to a converter e.g. with Spring.

<bean name="userPopulator" class="org.mwolff.populator.UserDataPopulator"/>
<bean name="addressPopulator" class="org.mwolff.populator.AddressPopulator"/>

<bean name="contactconverter" class="org.mwolff.converter.GenericConverter">
     <property name="populatorlist">
        <list>
            <ref bean="userPopulator"/>
            <ref bean="addressPopulator"/>
        </list>
    </property>
</bean>

Putting Converters and Populators together ist quite easy:

@Autowired
GenericConverter contactconverter;

    public void foo() {

        Address address = magicGetAddress(); // wherever it is located
        Customer customer = magicGetCustomer(); // wherever it is located
        ContactView target = new ContactView();

        contactconverter.addSource(address)
        .addSource(customer)
        .convert(target);
    }

If you want to add some more properties from other sources, just create a new populator in the list of populators and put another source into the converter. Thats its. If the property names are equal, you can just take a GenericPopulator, otherwise you’ve provide a litte bit code, how to transform.

public class UserDataPopulator implements Populator<Customer, ContactView> {

    @Override
    public void convert(Customer source, ContactView target) {
            target.setName(source.getLastname());
            target.setPrename(source.getFirstname());
    }
}

Resources

Check out: https://gitlab.com/mannewolff/simple-converter.git
Resources for Converters and Populators can be downloaded here: https://repo1.maven.org/maven2/org/mwolff/simple-converter/1.0.2/
Documentation: Comes soon, but you can browse the tests.
Include it in your project:

<dependency>
  <groupId>org.mwolff</groupId>
  <artifactId>simple-converter</artifactId>
  <version>1.0.2</version>
</dependency>

For other frameworks as gradle etc. browse here:
https://search.maven.org/artifact/org.mwolff/simple-converter/1.0.2/jar

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.