您好,登錄后才能下訂單哦!
SAP Hybris的Convertor, Populator, Facade和DTO這幾個概念是怎么協同工作的,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Hybris里極其重要的概念。
之前的issue談到了Hybris MVC里的M指的是DTO,JSP作為V顯示DTO的value,而DB層的Model和DTO的結構不一致。
那么,數據從DB讀取之后,需要經過一個轉換,寫入到DTO。執行這個轉換的role,就是Convertor+Populator。
注:DTO是軟件設計里一個common的概念,不是Hybris特有。定義見[wiki](https://en.wikipedia.org/wiki/Data_transfer_object)
# 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。
line 17的方法從CDS view里讀取persistent data:
這樣做類比。
可以看到CRM兩個layer之間的轉換非常light weight,僅僅是幾個賦值操作。
Hybris里把這個賦值操作封裝到了兩個新的object里:Converter和Populator。
為什么要搞這么麻煩:
(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就能顯示出來值。
既然Hybris基于Spring,那么也要follow Spring的一些原則:
(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.
看具體的例子:
這個文件位置:
"C:\Hybris\6.5.0.0.23546\hybris\bin\ext-accelerator\acceleratorservices\src\de\hybris\platform\acceleratorservices\payment\cybersource\converters\populators\ PaymentDataPopulator.java"
為什么要單獨抽象這兩個object出來?和CRM Genil layer的實現一對比就清楚了。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。