SAP Hybris的Convertor, Populator, Facade和DTO這幾個概念是如何協同工做的

Hybris裏極其重要的概念。
以前的issue談到了Hybris MVC裏的M指的是DTO,JSP做爲V顯示DTO的value,而DB層的Model和DTO的結構不一致。
那麼,數據從DB讀取以後,須要通過一個轉換,寫入到DTO。執行這個轉換的role,就是Convertor+Populator。java

注:DTO是軟件設計裏一個common的概念,不是Hybris特有。定義見wikispring

Hybris官方定義

(1) Facade: A facade is a software design pattern that abstracts from an underlying implementation and offers an alternate, often reduced and less complex interface.
(2) DTO:Data Transfer Objects (DTOs) are objects created to only contain values and have no business logic except for getter and setter methods. Using DTOs, you can "combine" Hybris items - for example, this document adds price- and media-related data to a product object.
先看CRM。less

line 17的方法從CDS view裏讀取persistent data:ui

這樣作類比。this

能夠看到CRM兩個layer之間的轉換很是light weight,僅僅是幾個賦值操做。
Hybris裏把這個賦值操做封裝到了兩個新的object裏:Converter和Populator。
爲何要搞這麼麻煩:spa

(1) Data objects are constructed from Models or other Service Layer objects using Converters and Populators. The Data object is always created from a prototype-scoped spring bean that is defined in the beans.xml file for the extension.
(2) Converters create new instances of Data objects and call Populators to populate these.
即Converter負責建立Data object的實例(就是Java class的實例), Populator負責call這個實例的setter方法把業務數據寫入data obeject,這樣JSP綁定到這些data object的某個屬性的field就能顯示出來值。prototype

既然Hybris基於Spring,那麼也要follow Spring的一些原則:ssr

(1) No concrete Converters should be written, all converters should be Spring configured only and should use the AbstractConverter base class.
(2) No Populator should be called directly in code, Converters should be Spring injected and used.
(3) All conversion logic should exist in Populators and these should be well-encapsulated and independent.設計

Populators

Populators break the conversion process of filling out a Data Object down into a pipeline of population tasks or steps. Each Populator carries out one or more related updates to the Data Object prototype. Each population step can invoke services or copy data from the source business object to the prototype Facade Data object. Facades always use a Converter to create a new instance of a Data Object prototype and then invoke Populators or other Converters to fulfill the task of building up the Data Object.code

看具體的例子:

這個文件位置:
"C:Hybris6.5.0.0.23546hybrisbinext-acceleratoracceleratorservicessrcdehybrisplatformacceleratorservicespaymentcybersourceconverterspopulatorsPaymentDataPopulator.java"

爲何要單獨抽象這兩個object出來?和CRM Genil layer的實現一對比就清楚了。

要獲取更多Jerry的原創文章,請關注公衆號"汪子熙":

相關文章
相關標籤/搜索