Why do we need Converters and Populators?

In most applications we need to create a subset of data from a larger data structure. For e.g:- we might have a customer table in the database that has 100 fields but we might only need only 10 of them (with some logic applied to them) to display on the UI. Your model object (source) is that larger data structure (that has all the data from the database) and let’s call the subset of data that your screen needs the target data structure  the DTO (Data Transfer Object). We call it the target because we are trying to convert from the source (Model) to the target(DTO).

So using converters and populations we are trying to create a target DTO by converting a source Model object using Populators to populate the DTO.

Advantages

Here rises a question, why can’t we just select the data that we need from the model object and apply the business logic we want to those fields.

A few reasons SAP Commerce defined this as a reusable pattern are:-

1. Extensibility:- Since SAP Commerce is built on an extendable platform, its easier to add additional Popluators if we need to add data to an existing DTO from an extension.

2. Performance:- We typically do not want to send all the data in the model object to a consuming storefront or web service. This pattern allows us to convert and populate only the required fields on the storefront.

3. Testability:- Its easy to develop test cases for code that does the populations by creating mock interfaces for the dependencies of those Populators.

4. Customization:- By delegating the responsibility of populating a target data object to a Populator we can have other extensions add/delete/override data within a target object using custom business logic that they implement.

Now that we have defined what we are trying to do and why we need Converters and Populators and the advantages of using the pattern. Lets see how SAP Commerce implements Converters and Populators.

Implementation

The typical flow of program execution within SAP Commerce is this.

Controller —> Facades —> Services —> DAO

Converters and Populators exist in the Facade layer.

From the diagram above you might be thinking where do converters fit in?

Converters provide a uniform interface for the facade to invoke all the Populators configured for that Converter.

The below diagram captures the high level flow of how Converters trigger Populators and how the DTO is sent to the Facades.

Leave a comment