在大型开发项目中使用组

ABAP RESTful 应用程序编程模型 › 学习 › 业务对象 › RAP 业务对象最佳实践 › 业务对象建模 › 在大型开发项目中使用组

本部分介绍组的概念,可用于将业务逻辑的操作、操作和其他实施相关部分分为多个行为实施组。

用例

通常,业务对象实体的操作和操作的实施在 Local Types 中包含与该实体相关的行为池(实施业务对象行为的 ABAP 类)中完成 - 除非使用 IMPLEMENTATION IN CLASS 语法(在实体级别)对实施类的控制已完全分配。

由于局部类型包含文件一次只能由一个开发人员更改,因此在较大实施的情况下,开发效率将显著降低。让我们考虑具有广泛业务逻辑实施的大型业务对象,其中许多实体,每个实体可能包含各种元素。

作为解决方案,实体的操作和操作可以分为多个组,因此每个组都可以分配不同的行为池作为实施类。

特别是在大型开发项目中,应用程序逻辑的实施职责将分配给开发团队的不同成员。为了在技术上支持此方法,我们引入了组的概念。通过此方法,开发人员团队可以相互独立地实施部分业务逻辑。此外,组概念允许根据语义注意事项定制业务对象的功能。

实体、组和实施类之间的关系

有关为非托管业务对象定义组的语法的详细信息,请参阅 CDS BDL - 实施分组(ABAP 关键字文档)

语法:定义被管理业务对象的组

可以使用以下语法在 管理类型的业务对象的行为定义中定义组:

[implementation] managed [implementation in class ABAP_ClASS [unique]];
define behavior for Entity [alias AliasedName]
  persistent table DB_TABLE
  ...
{
  [create;]
  [update;]
  [delete;]
  [read;]
  [association AssociationName [abbreviation AbbreviationName] {[create;] } ]

  group groupName_1 implementation in class ABAP_CLASS_1 unique
  {
    // Implementation-relevant content of the entity
  }

  group groupName_2 implementation in class ABAP_CLASS_2 unique
  {
    // Implementation-relevant content of the entity
  }

  // It is possible to assign the same behavior pool as the implementation class in different groups
  group groupName_3 implementation in class ABAP_CLASS_1 unique
  {
    // Implementation-relevant content of the entity
  }

  group ...

}

解释性说明

(1) 组名称在实体行为定义中本地定义,不得与同名操作、确定或验证冲突。

(2) implementation in class 语法只能用于组,而不再用于单个实体的行为定义本身。组需要行为池 ABAP_CLASS_* 和添加唯一 。

备注

通过行为定义标头中的附加项 implementation in class ABAP_ClASS,您可以选择为通用行为池中的所有实体实施剩余功能。例如,可以在单个行为池中实施业务对象所有实体的保存序列 ABAP_CLASS。

可以在不同的组中指定与实施类相同的行为池。在上述语法中,groupName_1 和 groupName_3 的实施相关内容必须在 ABAP_CLASS_1 中实施。

(3) 实体的实施相关内容可以是:

  • 操作

  • 基于实例的功能控制

  • 确定 - 仅针对托管实施类型

  • 验证 - 仅针对托管实施类型。

备注

对于非托管实施类型,必须将标准操作(CREATE, UPDATE, DELETE) 以及按关联的 READ 和 CREATE 分配到组。但是,在托管情况下,可以在组内部或外部指定标准操作(由框架实施)以及关联的 READ 和 CREATE。

(4) 映射信息(与实施无关)必须始终在组外指定。

(5) 隐式标准操作(自动定义且不必显式指定)必须在非托管情况下的同一组中显式指定(与实施相关):

  • read; - 用于实体的 READ 操作

  • lock; - 针对定义为锁主数据的实体的 LOCK 操作。

示例

清单:非托管业务对象的行为定义中的组

implementation unmanaged implementation in class /DMO/BP_TRAVEL_U;

// behavior definition for the TRAVEL root entity
define behavior for /DMO/I_Travel_U alias travel
  etag LastChangedAt
  lock master

{
  group travel_cud implementation in class /dmo/bp_travel_cud unique
  {
    field ( read only ) TravelID;
    field ( mandatory ) AgencyID, CustomerID, BeginDate, EndDate;
    field(features:instance)   overall_status;

    create;
    update(features:instance);
    delete;

    read; // read and lock must be assigned explcitly to a group
    lock;

  }

  group travel_cba implementation in class /dmo/bp_travel_cba unique
  {
    association _Booking { create; }
  }

  group travel_main_actions implementation in class /dmo/bp_travel_main_a unique
  {
    action(features : instance) set_status_booked result [1] $self;
    action                      getTravel result [1] $self;
    action                      copyTravel result [1] $self;
  }

  group travel_aux_actions implementation in class /dmo/bp_travel_aux_a unique
  {
    action(features:instance) getMaxDate  result [1] $self;
    action(features:instance) getminDate  result [1] $self;
  }

  mapping for /dmo/travel
  {
    ...

  }
}

// behavior defintion for the BOOKING child entity
define behavior for /DMO/I_Booking_U alias booking
  lock dependent ( travel_id = travel_id )
{

  group booking_rud implementation in class /dmo/bp_booking_rud unique
  {
    read;
    update;
    delete;
  }

  group booking_fc implementation in class /dmo/bp_booking_fc unique
  {
    field ( read only ) TravelID, BookingID;
    field ( mandatory ) CustomerID, AirlineID, ConnectionID, FlightDate;
    action(features:instance) confirmBooking  result [1] $self;
  }

  mapping for /dmo/booking
  {
    ...
  }

}

清单:被管理业务对象的行为定义中的组

implementation unmanaged implementation in class /DMO/BP_TRAVEL_M;

// behavior definition for the TRAVEL root entity
define behavior for /DMO/I_Travel_M alias travel
  persistent table /dmo/travel_m
  with additional save
  lock master
  authorization master ( instance )
  etag LastChangedAt
{
  create;
  delete;
  association _Booking { create; }

  group travel_fc implementation in class /dmo/bp_travel_fc unique
  {
    field ( read only )      TravelID;
    field ( mandatory )      AgencyID, CustomerID, BeginDate, EndDate;
    field(features:instance) overall_status;
  }

  group travel_cba implementation in class /dmo/bp_travel_cba unique
  {
    association _Booking { create; }
  }

  group travel_actions implementation in class /dmo/bp_travel_a unique
  {
    action(features : instance)     set_status_booked result [1] $self;
    action                          getTravel result [1] $self;
    action ( authorization : none ) copyTravel result [1] $self;
    action(features:instance)       getMaxDate  result [1] $self;
    action(features:instance)       getminDate  result [1] $self;
  }

  group booking_det_val implementation in class /dmo/bp_booking_det_val unique
  {
    determination determineDiscount on modify { create; }
    validation validateAgency       on save   { field Agency_ID; }
    validation validateCustomer     on save   { field Customer_ID; }
    validation validateDates        on save   { field Begin_Date, End_Date; }
    validation validateStatus       on save   { field overall_Status; }
  }

  mapping for /dmo/travel
  {
    ...

  }
}

// behavior definition for the BOOKING child entity
define behavior for /DMO/I_Booking_M alias booking
{
  read;
  update;
  delete;

  mapping for /dmo/booking
  {
    ...
  }

  group booking_fc implementation in class /dmo/bp_booking_fc unique
  {
    field ( read only ) TravelID, BookingID;
    field ( mandatory ) CustomerID, AirlineID, ConnectionID, FlightDate;
    action(features:instance) confirmBooking  result [1] $self;
  }

  group booking_det_val implementation in class /dmo/bp_booking_det_val unique
  {
    determination totalBookingPrice on modify { field Flight_Price; }
    determination determineCustomerStatus on modify { create; }
    // internal action: triggered by determination
    internal action SetCustomerStatus;
  }

}

实施相关方面

(1) 组的名称以及与此组关联的操作与外部用户无关(因此开发人员可以回溯更改)。这意味着外部操作和操作仍通过常规语法访问,在该语法中,仅实体名称、操作以及操作/确定/验证或关联(如果适用)扮演角色,而不是组的名称。

但是,也有例外情况:组的名称与基于实例的功能控制的实施相关 - 然后,相应的实施仅控制与其相应组相关联的功能。(框架合并外部使用者的信息。)相应的语法 entity-group 只能在与该组关联的实施类中使用。具体而言,涉及以下声明:

methods ... for features 的语法

methods method_name for features key_param
       request request_param for entity~group_name
       result result_parameter.

types/data ... for features 的语法

type|data ... type table|structure for features key|request|result entity~group_name.

备注

此声明也可以在实施类的 public section 中完成,以使组相关类型在 外公开。由于对组分配的更改与外部用户不兼容,因此不建议进行此类发布。

示例:为功能控制实施声明本地处理器

在实施类中,只能通过指定组名称来定义基于实例的功能控制的语法 methods ... for features:

class lhc_travel_fc definition inheriting from cl_abap_behavior_handler.
  private section.
    methods get_features for features
      importing keys request requested_features for travel~group_name result result.

endclass.

(2) 由于与常规关联语法的关联只能作为整体分配给组,因此无法在 CREATE 操作以外的实施类中实施关联的 READ 操作。

示例:按关联创建的本地行为实施

class lhc_travel_cba definition inheriting from cl_abap_behavior_handler.
  private section.
    methods create_bookings for modify
      importing entities for create travel\_Booking.

    methods read_bookings for read
      importing keys for read travel\_Booking full result_requested
      result result link association_links.
endclass.

class lhc_travel_cba implementation.

  method create_bookings.
   ...
  endmethod.
  method read_bookings.
   ...
  endmethod.
endclass.

来源:SAP Help Portal · 2025 FPS02 (Oct 2026) · 中文为 SAP 官方机器翻译

← RAP 重用数据元素 · 使用表实体作为活动持久性 →