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

i042416發表於2020-01-06

Hybris裡極其重要的概念。

之前的issue談到了Hybris MVC裡的M指的是DTO,JSP作為V顯示DTO的value,而DB層的Model和DTO的結構不一致。

那麼,資料從DB讀取之後,需要經過一個轉換,寫入到DTO。執行這個轉換的role,就是Convertor+Populator。

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

注:DTO是軟體設計裡一個common的概念,不是Hybris特有。定義見[wiki]( en.wikipedia.org/wiki/D )

# 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:

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

這樣做類比。

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

可以看到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.

看具體的例子:

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

這個檔案位置:

"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的實現一對比就清楚了。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2672028/,如需轉載,請註明出處,否則將追究法律責任。

相關文章