Converters and Populators

Before start to converters and populators , We need to understand DTO (Data transfer object). In the field of programming , we can describe DTO like "It is a object that used to encapsulate data, and transfer the data between classes and modules of your application". A DTO is helpful whenever you need to group values in ad hoc structures for passing data around. DTOs help to further decouple presentation from the service layer and the domain model. Data Transfer Objects (DTOs) are objects created to only contain values and have no business logic except for getter and setter methods

Using Converters, we will trying to create a target DTO by converting a source Model object and using Populators to populate the DTO. It means that Converters create new instances of Data objects and call Populators to populate these data objects.

In applications , we need to create a subset of data from a larger data structure. For example , we have customer table(CustomerModel) in the database with 30 fields but we required only 10 field of them to return to Front end(UI) or other API. We will fetch our customer model from the database as source and we will convert source customer model to target Customer DTO data structure which is subset of the customer model and will be passed customer DTO to UI. We will use converters and populators to create a target DTO by converting a source Model object using and Populators will populate data in DTO.

Converter

It creates new instances of Data objects and call Populators to populate data. It contains list of populators and uses populators to convert data from source to target. The Data object is always created from a prototype-scoped spring bean that is defined in the beans.xml file for the extension.

Populator

It fills the data object by getting the data from the model. It’s not recommended to call populator directly in code. We can , but it is not recommended by hybris. Because, we need to convert our model( ProductModel ) from multiple places. like PDP page, PLP page and order summary page etc. It would be batter to separate class to do this conversion.