Hybris的 Convertor, Populator, Facade 和 DTO 这几个概念是如何协同工作的

举报
Jerry Wang 发表于 2022/02/17 21:20:16 2022/02/17
【摘要】 新建一个产品,identifier设置为i042416-1,创建之后立即能够在Backoffice里搜索出来:等到Storefront的indexing做完之后,前台通过关键字i042416也能将这个新创建的product搜索出来了:如何使用Restful API根据这个关键字进行搜索呢?https://<host>:9002/rest/v2/electronics/products/sea...

新建一个产品,identifier设置为i042416-1,创建之后立即能够在Backoffice里搜索出来:

clipboard1,1

等到Storefront的indexing做完之后,前台通过关键字i042416也能将这个新创建的product搜索出来了:

clipboard2,2

如何使用Restful API根据这个关键字进行搜索呢?

https://<host>:9002/rest/v2/electronics/products/search?query=i042416

返回的响应结果里,包含命中的product个数:

clipboard3,3

以及命中的product的code, description, name和stock(库存)字段。由此可见,当前模糊搜索匹配的字段为name.

clipboard4,4

如果想一个roundtrip里还能取回其他额外字段,比如product的图片url,在API后面加上fields=FULL这个参数:

clipboard5,5

这个返回的响应里,就包含了product image url数据:

clipboard6,6

在使用Hybris Commerce User API读取一个user信息时,我遇到一个问题,在API返回的结构里没有包含期望看到的Phone字段。


仔细观察Swagger里对response结构的说明,发现phone字段是隶属于Address这个DTO(Data Transformation Object)的:

因此,我们进入Backoffice,找到user的address维护部分,这个default shipment address的postal code是617777,双击进入地址编辑页面:

在弹出窗口的Additional Address information字段里,就可以维护电话号码了:

最后API返回的phone number是Phone1字段的值:

Hybris里极其重要的概念。
之前的issue谈到了Hybris MVC里的M指的是DTO,JSP作为V显示DTO的value,而DB层的Model和DTO的结构不一致。
那么,数据从DB读取之后,需要经过一个转换,写入到DTO。执行这个转换的role,就是Convertor+Populator。

注:DTO是软件设计里一个common的概念,不是Hybris特有。定义见wiki

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的实现一对比就清楚了。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。