温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SAP Hybris的Convertor, Populator, Facade和DTO这几个概念是怎么协同工作的

发布时间:2021-12-30 17:25:57 来源:亿速云 阅读:91 作者:柒染 栏目:互联网科技

SAP Hybris的Convertor, Populator, Facade和DTO这几个概念是怎么协同工作的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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](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:

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI