跳到内容
技术文章
作者的资料照片Mykola Tokariev

动态OData实体声明

在本文中,我将显示一种动态声明ODATA实体的方法。

为什么?

如果我们应该更改业务逻辑类型并不想更改OData服务。

如何?

动态声明OData实体。

反?

如果OData服务有问题,查找错误会比较困难。

亲吗?

这是太快了。

在标准情况下,在我们开始使用OData服务之前,我们应该声明实体类型。基本上,这是业务逻辑类型和将显示给外部世界的实体之间的映射。如果业务逻辑发生了更改,我们还应该更改映射。

它需要很多时间,尤其是在开始时,任何实体都还没有声明。你可以看到一个映射示例如下:

方法define_dga_in。试一试。数据(entity_type)= model-> create_entity_type(iv_entity_type_name = zif_business_logic_types => entity_in inv_def_entity_set = abap_false)。属性= VALUE#((键= abap_true属性= zif_business_logic_types => prop_plant ABAP = zif_business_logic_types => field_plant)(abap_false键=属性= zif_business_logic_types => prop_tu_number ABAP = zif_business_logic_types => field_tu_number)(密钥= abap_true属性= zif_business_logic_types => prop_matnr ABAP= zif_business_logic_types => field_matnr)(密钥= abap_false属性= zif_business_logic_types => prop_batch_nr ABAP = zif_business_logic_types => field_batch_nr)(密钥= abap_false属性= zif_business_logic_types => prop_bbdate ABAP = zif_business_logic_types => field_bbdate)(密钥= abap_false属性= zif_business_logic_types => prop_bruttoABAP = zif_business_logic_types => field_brutto)(密钥= abap_false属性= zif_business_logic_types => prop_netto ABAP = zif_business_logic_types => field_netto)(密钥= abap_false属性= zif_business_logic_types => prop_tagew ABAP = zif_business_logic_types => field_tagew)(abap_false属性= zif_business_logic_t键=YPES => prop_gewei ABAP = zif_business_logic_types => field_gewei)(密钥= abap_false属性= zif_business_logic_types => prop_menge ABAP = zif_business_logic_types => field_menge)(abap_false属性= zif_business_logic_types => prop_meins ABAP = zif_business_logic_types => field_meins)(abap_false属性键=键== zif_business_logic_types => prop_dmeng ABAP = zif_business_logic_types => field_dmeng)(密钥= abap_false属性= zif_business_logic_types => prop_dmein ABAP = zif_business_logic_types => field_dmein)(密钥= abap_false属性= zif_business_logic_types => prop_aufnr ABAP = zif_business_logic_types => field_aufnr)(密钥= abap_false属性= zif_business_logic_types => prop_qplos ABAP = zif_business_logic_types => field_qplos)(密钥= abap_false属性= zif_business_logic_types => prop_lgort ABAP = zif_business_logic_types => field_lgort)(abap_false属性= zif_business_logic_types => prop_text ABAP = zif_business_logic_types => field_text)(密钥=键=abap_false属性y = zif_business_logic_types => prop_mess abap = zif_business_logic_types => field_mess)(key = abap_false属性= zif_business_logic_types => prop_extnr abap = zif_business_logic_types => field_extnr))。 create_properties( entity_type ). entity_type->bind_structure( iv_structure_name = 'zif_business_logic_types=>in' ). entity_type->get_property( zif_business_logic_types=>prop_bbdate )->set_nullable( abap_true ). entity_type->get_property( zif_business_logic_types=>prop_bbdate )->set_type_edm_string( ). entity_type->get_property( zif_business_logic_types=>prop_gewei )->disable_conversion( ). entity_type->get_property( zif_business_logic_types=>prop_meins )->disable_conversion( ). entity_type->get_property( zif_business_logic_types=>prop_dmein )->disable_conversion( ). DATA(entity_set) = entity_type->create_entity_set( zif_business_logic_types=>entityset_dga_in ). entity_set->set_addressable( abap_true ). entity_set->set_pageable( abap_true ). entity_set->set_filter_required( abap_false ). CATCH /iwbep/cx_mgw_med_exception INTO DATA(mgw_error). RAISE EXCEPTION mgw_error. CATCH /usi/cx_odata. ENDTRY. ENDMETHOD.

与实体的动态声明相同,我们可以实现相同的结果。

define_optional_material方法。DATA: DATA TYPE zif_business_logic_types=>optional_material。试一试。DATA(entity_type) = model->create_entity_type(iv_entity_type = zif_business_logic_type =>entity_optional_material iv_def_entity_set = abap_false)。结构?= cl_abap_typedescr=>描述_by_data(数据)。组件=结构- >组件。将AT组件循环为DATA(comp)。DATA(pop_entry) = _create_pop_entry(comp)。pop_entry-abap。当“EBORT”。 pop_entry-key = abap_true. ENDCASE. APPEND pop_entry TO properties. ENDLOOP. create_properties( entity_type ). entity_type->bind_structure( iv_structure_name = CONV #( structure->absolute_name ) ). DATA(entity_set) = entity_type->create_entity_set( zif_business_logic_types=>entityset_optional_material ). entity_set->set_filter_required( abap_false ). CATCH /iwbep/cx_mgw_med_exception INTO DATA(mgw_error). RAISE EXCEPTION mgw_error. CATCH /usi/cx_odata. ENDTRY. CLEAR properties. ENDMETHOD.

方法_create_prop_entry (comp)获取结构的组件,我们在业务逻辑中使用,并为实体创建属性。定义如下所示:

_create_pop_entry方法。DATA prop_name类型char40。pro_name = ' pro_ ' && i_comp-name。分配zif_business_logic_types=>(prop_name)到FIELD-SYMBOL()。IF  IS ASSIGNED。r_result-key = abap_false。r_result-property = < prop_value >。r_result-abap = i_comp-name。ENDIF。ENDMETHOD。

在这种情况下,我们所需要的只是一个属性名的全局声明。在这种情况下,我使用接口来声明类型。

常量:prop_ebort类型char40值'stappingpointforsubitem',prop_upmng型char40值'子istity',prop_uptwy型char40值'bomsubitemtext'。

就这样。我希望你有这一点。

简介:

这种方法的最大优势您可以看到是否需要更改类型中的某些内容。您需要的所有内容都是将一些新类型添加到您的结构和描述中,如上所示。您的ODATA服务将自动调整。不要忘记通知您的UI学院的变化

指定的标签

      第一个留下评论
      你一定是登录评论或回复一篇文章
      Baidu