Enabling Data Model Extensibility

Objectives

After completing this lesson, you will be able to:
  • Enable database tables for extension.
  • Enable CDS views for extension.

Extensibility Enablement for Database Tables

The Extension Include approach

Data model extension has two aspects: The extension of existing business object entities with more fields and the extension of an existing business object with extra BO entities.

Let's have a look at the extension of existing BO entities first.

This extension option begins with the extension of the database table in the ABAP dictionary. For this, RAP uses the well-established Append Structure technology.

However, instead of allowing the extension of the database table definition directly, it is common practice in RAP to add an include structure to the database table and then only release this structure for extension. This structure is referred to as the extension include structure.

By including the extension include structure in the active table and in the draft table, this approach ensures that the two tables are extended consistently, that is, with the same fields.

The Extension Include structure

In principle, the extension include structure should not contain any components. However, that is technically not possible. Therefore, the extension include structure is defined with a dummy field, often called dummy_field, of type abap.char(1).

The ABAPCatalog.enhancement.category annotations control the extensibility of the dictionary object. It is common practice to define a mandatory suffix for the enhancement fields.

Note

The annotations control the extensibility on a technical level. In addition, SAP uses the API release state of development objects to allow or disallow enhancements. See here for details on how to release a development object.

Extensibility Enablement for CDS View Entities

A visual representation of the extension include view approach

The CDS view extension technique is used to add fields to the entities of a RAP business object. The same technique is used to make the extension fields available in the BO projections and BO interfaces on top of the business object.

When enabling a CDS view for extension, it is possible to restrict the extensions to certain data sources from which they can read. It is the technical foundation for the following best practice when enabling the data model of a business object for extension:

The data model views that define the BO entities are defined with an association to a dedicated CDS view entity, called the CDS extension include view. This extension include view is extensible with fields from the underlying database table, for example the append fields.

The data model view, however, is not allowed to address fields from the database table. The only allowed data source is the association to the extension include view. Through this, the data model view extension is restricted to elements that have been added to the extension include view, first.

CDS extension include view

Technically, the extension include view is an ordinary CDS view entity. It only receives its special character from the way that it is integrated into the data model. To distinguish it from other CDS view entities in the data model, the naming convention request the letter E where the other CDS view entities have an R, I, and C.

The extension includes view reads from the same database table as the data model view, but only contains the primary key fields. The data model view then uses these key fields to define an exposed association. In the example, the key field is AgencyID and the association name is _extension.

The most important parts are the @ABAPCatalog.extensibility annotations. They ensure that the developer who extends the data model uses the extension include view.

The data model view uses the dataSources annotation to allow the extension with elements from this association and sets the allowNewDatasources annotation to false, to disallow any other data sources.

In our example, the extension include view allows the extension with fields from the database table and disallows other data sources.

Note

The dataSources annotation only accepts alias names. It is not possible to specify table names or CDS entity names directly. It's the reason why the extension include view introduces the alias name Agency for the database table in the FROM clause.

A visual representation of extensions for projections and interfaces

When developers enable the data model views of a business object for extension, they also allow the extension of the related projection views. On the projection level, the view extension has access to all elements of the underlying data source. This can be the data model view or, if the BO projection is build on top of the BO interface, the interface view.

The extension control for projection views and interface views

The extension control for projection views and interface views uses the same annotations as the extension control on the data model level. Extensions should always be restricted to the direct data sources, that is, the data model view for the interface view and the data model view or the interface view for the projection view.

Enable the Data Model Extensibility

In this exercise, you enable the extension of the data model, that is, the extension of the business object and its projection. First, you enable the extension of the database table for flight travel items and the related draft table. Then, you enable the extension of the data model view and the projection view for flight travel items.

Note

In this exercise, replace ## with your group number.

Solution

Repository Object TypeRepository Object ID
Database Table Definition (Active Data)/LRN/437H_TRITEM
Database Table Definition (Draft)/LRN/437H_TRIT_D
Structure Type (Extension Include Structure)/LRN/437H_S_EXT_TRITEM
CDS Data Definition (Model)/LRN/437H_R_TRAVELITEM
CDS Data Definition (Projection)/LRN/437H_C_TRAVELITEM
CDS Data Definition (Extension Include View)/LRN/437H_E_TRAVELITEM

Task 1: Enable Extension of DB Tables

Enable the extension of your database table for flight travel items Z##_TRITEM and of the corresponding draft table Z##_TRITEM_D. Follow the best practice to ensure that future extensions are consistent: Define an extension include structure (suggested name: Z##_S_EXT_TRITEM) with a dummy component and include it in both database table definitions. Then define the extension include structure and both database tables as extensible with char-like and numeric fields and define a mandatory field suffix (suggested suffix: ZIT).

Steps

  1. Create a structure type as extension include (suggested name: Z##_S_EXT_TRITEM).

    1. In the Project Explorer, locate your package ZS4D437_## under the Favorite Packages node.

    2. Expand your package and right-click the Dictionary sub node. Choose NewStructure.

    3. Enter Z##_S_EXT_TRITEM as Name and Extension Include Structure for Travel Items as Description. Then choose Next.

    4. Confirm the transport request and choose Finish.

  2. In the structure type, define a single component (suggested name: dummy_field). As the component type, use the built-in dictionary type CHAR with a length of 1.

    1. In the structure type definition, replace the component_to_be_changed placeholder with dummy_field.

    2. Replace abap.string(0) with abap.char(1).

  3. Enable extensibility for the structure type. Allow extension with char-like and numeric fields but not with deep component types like, for example, table types.

    1. In the AbapCatalog.enhancement.category annotation, replace the default value #NOT_EXTENSIBLE with #EXTENSIBLE_CHARACTER_NUMERIC.

      Hint

      Use code completion to choose from the available enhancement categories.
  4. Define a mandatory suffix Z## for extension fields.

    1. Adjust the code as follows:

      Code Snippet
      12345678
      @EndUserText.label : 'Extension Include Structure for Travel Items' @AbapCatalog.enhancement.category : #EXTENSIBLE_CHARACTER_NUMERIC @AbapCatalog.enhancement.fieldSuffix : 'Z##' define structure z##_s_ext_tritem { dummy_field : abap.char(1); }
  5. Activate the structure type.

    1. Press Ctrl + F3 to activate the development object.

  6. Edit the definition of your database table for flight travel items Z##_TRITEM and include the structure type into the field list.

    Note

    As you C1 released the database table in a previous exercise, you will receive a popup with the following message when you try to edit the table definition: Object is a stable API. Do not change incompatibly. Confirm this popup with OK.
    1. At the end of the field list, add the following code:

      Code Snippet
      1
      include z##_s_ext_tritem;
  7. Enable extensibility for the database table definition with the same enhancement category as in the extension include structure and activate the database table definition.

    1. In the AbapCatalog.enhancement.category annotation, replace the default value #NOT_EXTENSIBLE with #EXTENSIBLE_CHARACTER_NUMERIC.

    2. Press Ctrl + F3 to activate the development object.

  8. Edit the definition of your draft table for flight travel items Z##_TRITEM_D. Include the structure as before and activate the database table definition.

    1. Add the following code at the end of the field list:

      Code Snippet
      1
      include z##_s_ext_tritem;
    2. Press Ctrl + F3 to activate the development object.

Task 2: Enable Extension of CDS Views

Enable the extension of the CDS view entities for flight travel items. Follow the best practice to ensure consistent extensions: Define an extension include view (suggested name: Z##_E_TravelItem) with only the key field of flight travel items. Then add an association to the view on data model level Z##_R_TravelItem with the extension include view as target (suggested association name: _Extension).

Allow the extension of the view on data model level Z##_R_TravelItem and the projection view Z##_C_TravelItem. In both cases, restrict the extensibility to elements from the target of the association _Extension.

Steps

  1. Create a CDS view entity (suggested name: Z##_E_TravelItem) with your database table for active flight travel items Z##_TRITEM as data source.

    Note

    Specify the database table as a Referenced Object to generate the element list.
    1. In the Project Explorer, locate the database table for active travel items Z##_TRITEM.

    2. Right-click the database table and choose New Data Definition.

    3. Enter Z##_E_TravelItem as Name and Extension Include for Travel Items as Description. Make sure that the Referenced Object field contains the name of the database table and choose Next.

    4. Confirm the transport request and choose Next.

    5. From the list of templates, select the defineViewEntity template and choose Finish.

  2. In the definition of the view entity, remove all elements except for the key element ItemUuid.

    1. In the element list, select all elements without the keyword KEY and press the Delete key.

    2. Remove the comma at the end of the remaining element.

  3. Enable extensibility for the extension view entity.

    1. In the AbapCatalog.viewEnhancementCategory annotation, replace the default value #NONE with #PROJECTION_LIST.

    2. Before the DEFINE VIEW ENTITY statement, insert the following code:

      Code Snippet
      123
      @AbapCatalog.extensibility: { extensible: true }
  4. Restrict the extension to the existing data source.

    Note

    To achieve this, you have to define an alias name for the data source (suggested name: Item). You define this alias in the FROM clause of the SELECT statement.
    1. Adjust the DEFINE VIEW ENTITY statement as follows:

      Code Snippet
      12345
      define view entity Z##_E_TravelItem as select from z##_tritem as Item { key item_uuid as ItemUuid }
    2. Adjust the AbapCatalog.extensibility annotation as follows:

      Code Snippet
      12345
      @AbapCatalog.extensibility: { extensible: true, allowNewDatasources: false, dataSources: ['Item'] }
  5. Define a mandatory suffix Z## for extension elements and activate the data definition.

    1. Adjust the AbapCatalog.extensibility annotation as follows:

      Code Snippet
      123456
      @AbapCatalog.extensibility: { extensible: true, allowNewDatasources: false, dataSources: ['Item'], elementSuffix: 'Z##' }
    2. Press Ctrl + F3 to activate the development object.

  6. Edit the definition of your CDS view entity for travel items on data model level Z##_R_TravelItem. Define and expose an association with the extension include view as target (suggested association name: _Extension).

    1. Adjust the code as follows:

      Code Snippet
      123456789
      define view entity Z##_R_TravelItem as select from z##_tritem association to parent Z##_R_Travel as _Travel on $projection.AgencyId = _Travel.AgencyId and $projection.TravelId = _Travel.TravelId association to Z##_E_TravelItem as _Extension on $projection.ItemUuid = _Extension.ItemUuid {
    2. At the end of the element list, adjust the code as follows:

      Code Snippet
      123
      _Travel, _Extension }
  7. Enable extensibility for the CDS view entity and activate the data definition. Specify the same mandatory element suffix and restrict the extensibility with the _Extension association as the only allowed data source.

    Note

    Adding annotation AbapCatalog.viewEnhancementCategory is optional. If it's missing, the default value [#PROJECTION_LIST] is used.
    1. At the beginning of the data definition, add the following code:

      Code Snippet
      1234567
      @AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST] @AbapCatalog.extensibility: { extensible: true, allowNewDatasources: false, dataSources: ['_Extension'], elementSuffix: 'Z##' }
    2. Press Ctrl + F3 to activate the development object.

  8. Edit the definition of your CDS view entity for travel items on projection level Z##_C_TravelItem. Enable extensibility for the CDS view entity and activate the data definition. Specify the same mandatory element suffix as before and restrict the extensibility with the primary data source Z##_R_TravelItem as the only allowed data source.

    Note

    To achieve this, you must define an alias name for the data source (suggested name: Item) in the AS PROJECTION ON addition.
    1. Adjust the DEFINE VIEW ENTITY statement as follows:

      Code Snippet
      123
      define view entity Z##_C_TravelItem as projection on Z##_R_TravelItem as Item {
    2. At the beginning of the data definition, add the following code:

      Code Snippet
      1234567
      @AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST] @AbapCatalog.extensibility: { extensible: true, allowNewDatasources: false, dataSources: ['Item'], elementSuffix: 'Z##' }
    3. Press Ctrl + F3 to activate the development object.