Converters and Populators:
If I want to convert my Model to DTO directly in facade layer then I can use converter. A converter is the one that constructs data objects from Models or service layers. It converters data from source to target using Populators. We can say converter contains one or more Populators. Converters create an object of DTO.
It converts data object to Model using populator and Model to data object using Reverse populator. The Data object is always created from a spring bean that is defined in the beans.xml file for the extension.All the converters are an instance of AbstractConverter or AbstractPopulatingConverter
Ex: Product Converter, Cart Converter, Customer Review Converter etc.
Note: No concrete Converters should be written, all converters should be spring configured only and should use the AbstractConverter base class.
Populators break the code for filling up data in DTO. This is required because, not each DTO needs all attributes of a model. Each Populator carries out one or more related updates to the Data Object. Each population step can invoke services or copy data from the source business object to Facade Data object. Facades always use a Converter to create a new instance of a Data Object and then invoke Populators or other Converters to fulfill the task of building up the Data Object.
- It contains conversion logic
- We cannot use this directly in code, before that Converters should be spring injected and used.
- Populators are used in facade layer only.
- Reverse Populators are used in Service layer.
Ex: Product data populator, cart populator, gender data populator etc.
We have two types of Populators:
- Configurable Populators
- Modifiable Configurable Populators
ConfigurablePopulators may implement different conversion rules based on a set of enums.
1. First the converter creates a data object using all the Populators which fill the mandatory fields.
2. Then a configurable populator fills the fields which are only required by some components/pages of the front-end. It has a Map<enums option, populator>.
Modifiable Configurable Populators enables you to modify ConfigurablePopulators that implement the ModifiableConfigurablePopulator interface, which includes adding Populators to, and removing Populators from already configured ConfigurablePopulator beans.
Modifications are configured as spring beans that extend the abstract parent configurablePopulatorModification bean. They register themselves with the target populator during context initialization.
The commercefacades-spring.xml file contains the definition of the abstract configurablePopulatorModification parent bean, which defines the init method that executes the registration with the target populator.