Create Facades
Define, create, and register facade beans to aggregate service calls and provide data transfer objects (DTOs) that simplify the development of controller classes and Java Server Pages (JSP).
Requirements
-
Interface shall be documented on why its working this way and intentions of the facade and the facade method.
-
Internal inline comments shall be written for any complex or ambiguous logic. No private methods, all protected methods intended for replacement shall be documented. Inline comments shall describe why the functionality is working a certain way, explaining the thought process.
-
Facades shall be very extensible and easy to extend. For example, replacing facade converters or replacing methods or the entire facade should be possible.
Best Practice Guidelines
SAP recommends these guidelines for software developers extending SAP Commerce Cloud.
- Create facade objects to convert SAP Commerce Cloud Model objects into Data Transfer Objects (DTOs)
- SAP Commerce Cloud model objects may not provide the most suitable form of data for use in Java Server Pages or other presentation technologies. Provide a facade class to translate data from one or mores services into a more suitable form for presentation layers. This typically involves converting one or more objects of hybris model classes into plain old Java beans, often referred to as Data Transfer Objects (DTO).
- Name interfaces of facades after the related domain concept with “Facade” on the end
-
The following displays some example:
ProductFacade CatalogVersionFacade ConsentFacade
- Comply with the packaging conventions for facade interfaces
-
Where an extension has been created specifically for facades, e.g. assistedservicepromotionfacades, place the Java interfaces of facades in the root package of the extension. The following shows an example:
de.hybris.platform.assistedservicepromotionfacades
- Name implementations of facades the same as the service interface but with a standard prefix.
-
For product development, use a prefix of Default. The following shows some examples:
-
CartFacade <--- DefaultCartFacade
-
OrderFacade <-- DefaultOrderFacade
-
ReturnsFacade <-- DefaultReturnsFacade
-
AtsFacade <-- DefaultAtsFacade
-
UserFacade -> DefaultUserFacade
-
- Put the implementation classes of facades in an impl package under the facade package
-
Implementations go in an impl package and begin with the prefix Default. Let's take a look at this example: de.hybris.platform.ordermanagement.returns.impl.DefaultReturnsService.
-
Spring bean ID should be defaultXXX with additional alias XXX with first letter lower case:
<bean id= "defaultProductService" class="... DefaultProductService"/>
-
Define alias for default beans
<alias alias= "productService" name= "defaultProductService"/>
-
- Define DTO beans in beans.xml files
-
Use the built-in bean definition features to crate data transfer objects (DTO) classes by defining them in xml in the <extension-name>-beans.xml file located in the resource folder of each extension. This enables bean definitions to be added to by other extensions in a similar way to item type definitions. The following shows a compliant example:
<?xml version="1.0" encoding="ISO-8859-1"?> ... <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="beans.xsd"> ... <bean class="de.hybris.platform.commercefacades.product.data.ReviewData"> <property name="id" type="String"/> <property name="headline" type="String"/> <property name="comment" type="String"/> <property name="rating" type="Double"/> <property name="date" type="java.util.Date"/> <property name="alias" type="String"/> <property name="principal" type="de.hybris.platform.commercefacades.user.data.PrincipalData"/> </bean> ... </beans> - Use Convertors and Populators with DTO beans
-
Instead of hardcoding the logic for converting hybris model objects into Data Transfer Objects in facade classes, put that logic into convertor and populator Spring beans. This is more flexible and extensible especially where the definition of a DTO is spread across multiple extensions. The following shows a compliant example:
Create populator classes that directly or indirectly implement the Populator interface.
package de.hybris.platform.commercefacades.product.converters.populator; import de.hybris.platform.commercefacades.product.data.ReviewData; import de.hybris.platform.commercefacades.user.data.PrincipalData; import de.hybris.platform.converters.Populator; import de.hybris.platform.core.model.security.PrincipalModel; import de.hybris.platform.customerreview.model.CustomerReviewModel; import de.hybris.platform.servicelayer.dto.converter.Converter; import org.springframework.beans.factory.annotation.Required; import org.springframework.util.Assert; /** * Converter implementation for {@link de.hybris.platform.customerreview.model.CustomerReviewModel} as source and * {@link de.hybris.platform.commercefacades.product.data.ReviewData} as target type. */ public class CustomerReviewPopulator implements Populator<CustomerReviewModel, ReviewData> { private Converter<PrincipalModel, PrincipalData> principalConverter; protected Converter<PrincipalModel, PrincipalData> getPrincipalConverter() { return principalConverter; } @Required public void setPrincipalConverter(final Converter<PrincipalModel, PrincipalData> principalConverter) { this.principalConverter = principalConverter; } @Override public void populate(final CustomerReviewModel source, final ReviewData target) { Assert.notNull(source, "Parameter source cannot be null."); Assert.notNull(target, "Parameter target cannot be null."); target.setId(source.getPk().getLongValueAsString()); target.setComment(source.getComment()); target.setDate(source.getCreationtime()); target.setHeadline(source.getHeadline()); target.setRating(source.getRating()); target.setAlias(source.getAlias()); target.setPrincipal(getPrincipalConverter().convert(source.getUser())); } }Configure converter beans with lists of populator beans in the <extension-name>-Spring.xml file in the resources directory of the extension:... <alias name="defaultCustomerReviewConverter" alias="customerReviewConverter"/> <bean id="defaultCustomerReviewConverter" parent="abstractPopulatingConverter"> <property name="targetClass" value="de.hybris.platform.commercefacades.product.data.ReviewData"/> <property name="populators"> <list> <ref bean="customerReviewPopulator"/> </list> </property> </bean> <alias name="defaultCustomerReviewPopulator" alias="customerReviewPopulator"/> <bean id="defaultCustomerReviewPopulator" class="de.hybris.platform.commercefacades.product.converters.populator.CustomerReviewPopulator" > <property name="principalConverter" ref="principalConverter"/> </bean> ... - Name Populator Classes after the item type they populate followed by Populator
-
The names of population classes should start with the type of item being converted and finished with the string Populator. The following lists some examples:
-
CustomerReviewModel => CustomerReviewPopulator
-
AbstractCouponModel =>CSACouponDataPopulator
-
- Comply with the packaging conventions for population classes
-
Place populator classes in a package called populator or convertors.populators either directly under the root package of the extension or under the package for the relevant domain concept if the extension is organised in that way. The following lists examples:
-
CustomerReviewPopulator => de.hybris.platform.commercefacades.product.converters.populator
-
CSACouponDataPopulator => de.hybris.platform.assistedservicepromotionfacades.populator
-