on ‎2018 Aug 08 3:05 PM
Hi Team,
I am new to Hybris. I need to know how populators are implemented in hybris with full explanation.
Any help would be highly appreciated.
Thanks
Help others by sharing your knowledge.
AnswerRequest clarification before answering.
Hi Anuj,
Firstly, here is an article introducing Populators: https://help.hybris.com/6.6.0/hcd/8b937ff886691014815fcd31ff1de47a.html And one page from trail with simple example: https://help.hybris.com/6.6.0/hcd/56749e96d95a41ca8a1298bd8c59edda.html As the subject is tightly connected with DTOs and Converters here’s another article with Pest Practices: https://help.hybris.com/6.6.0/hcd/8c7acd1986691014a5f4b5880d032474.html
In general - Populators & Converters:
They both offer some interaction from Models to Data Transfer Objects (DTO).
Populators are responsible to fill attributes in an existing DTO from the ones from the Model.
Converters are responsible to completely transform Models to a new DTO.
To avoid code duplication:
Converters only do the DTO creation
Then call an ordered list of Populators to handle the real attribute filling.
There are 2 different types of Populators:
Direct Populator - Each type has its own Populator that is responsible to populate only the direct & "native" attributes.
Relation Populator - Each type has a Populator for each relation that takes the base type and only populates that relation. This is only a "glue" Populator as it only calls the relevant Populators, in a typical "strategy" pattern.
Guidelines when writing populators:
You should not use instanceof in a populator.
If you need to populate a different set of attributes based on the type of the Model, you should use 2 different spring populators beans.
The choice is then done with 2 different spring converter beans, and either :
In the Facade by injecting 2
different converters, and doing the
instanceof there
In the Controller by injecting 2 instances of the same facade implementation, but with injecting different converters.
You can use a "proxy Converter".
If using multiple converter beans is cumbersome, one can inject a Proxy converter that will choose the correct bean.
This will inject easily the strategy without needed to refactor many layers.
You should not put anything else than IDs in the Session
Only ID should be in the Session. You'll retrieve then the Model from the Entity Cache anyway. If there is no Model that exists, since the data is directly coming from an external WebService for example, you can create a Populator that call that depends on the root object, and calls that WebService to inject the distant data inside the DTO.
A good example for that would be to retrieve the LoyaltyAmount from a CustomerModel to be able to show it on the storefront. The populator would be called LoyaltyAmountCustomerPopulator<CustomerModel, CustomerData>. It could be then easily cached with the standard DTO cache package. It can even be disabled upon high load with some AOP.
Hope this helps.
Best Regards,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.