Building Views on Views

Objectives

After completing this lesson, you will be able to:

  • Define a CDS view that reads from another view.
  • Control the propagation of element annotations.

View on View - Motivation

When you define a CDS view entity, you can not only read from database tables, you can also use other CDS views as data source. Why would you build views on top of views?

  • The first reason is similar to the reasoning when structuring source code in classes and methods:

    • Reuse existing functionality
    • Avoid redundancies
    • Support error analysis and maintenance
  • The second argument is more technical. There are certain limitations when it comes to the nesting of SQL expressions and SQL functions within the same CDS view. Nested views are a common technique to overcome such restrictions.
  • Probably the most important reason for nested views is the layering of your data model. In the ABAP RESTful Programming Model, for example, it is recommended that you define consumption views on top of data model views. While the data model views contain the general data model logic, the consumption views add consumption specific logic and metadata.

Propagated Annotations

If a CDS view entity reads from another view entity it can inherit metadata from the source view. This is called annotation propagation.

In this example, CDS view entity C_Employee uses CDS view entity R_Employee as data source. By default, the element annotations of R_Employee get propagated into C_Employee. As a consequence, annotation @Semantics.amount.currencyCode is not required in C_Employee, again.

Propagation is restricted to element annotations. Entity and view annotations never get propagated.

Entity annotation @Metadata.ignorePropagatedAnnotations allows you to control the propagation of element annotations. If it is missing from the reading CDS view entity, or is set to value false, element annotations are inherited. With @Metadata.ignorePropagatedAnnotations: true the propagation of element annotations is disabled.

If in our example, CDS view entity C_Employee is annotated with @Metadata.ignorePropagatedAnnotations: true, the @Semantics.amount.currencyCode annotation for element AnnualSalary is not propagated. The syntax check displays an error because the mandatory annotation is missing for the amount field.

How to Define a View on Top of a View

Play the video to see how to define a view on top of a view.

Define a View on a View

You want to separate the basic definition of your data model from its specific usage (consumption). To do so, you build a new CDS view entity on top of your existing data model entity.

Template:

  • n.a.

Solution:

  • /LRN/C_EMPLOYEE_ANN (Data Definition)

Prerequisites

For this exercise, you need the view entity for employee data that you created and annotated in previous exercises (suggested name was: Z##_R_Employee, where ## is your group number). If you have not finished the previous exercise, create a copy of data definition /LRN/R_EMPLOYEE_ANN.

Task 1: Define a CDS View Entity

Define a new CDS view entity that reads from your CDS view entity for employee data (suggested name: Z##_C_Employee, where ## is your group number). Use a template that adds all view elements of entity Z##_R_Employee to the element list of the new view and takes over the key definition from the base view.

Steps

  1. In your own package, create a new data definition (suggested name: Z##_C_EMPLOYEE, where ## is your group number). Specify your existing view entity as Referenced Object and choose the Define View Entity template to generate the definition statement, some standard annotations and the element list.

    1. In the Project Explorer view, right-click your existing data definition Z##_R_EMPLOYEE to open the context menu.

    2. From the context menu, choose New Data Definition.

    3. Confirm that the Package field contains the name of your package and that the Referenced Object field contains the name of your data definition.

    4. In the Name field, enter the name for the CDS view entity (Z##_C_Employee, where ## is your group number).

    5. Enter Employee (Consumption) in the Description field and choose Next.

    6. Confirm the transport request and choose Next.

      Caution

      Make sure you don't choose Finish yet. If you do, you are not able to choose the template that you want to use.
    7. From the list of Templates, choose Define View Entity, then choose Finish.

  2. Adjust the @ObjectModel.usageType annotations. Use the same values as in the base view.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567
      @ObjectModel.usageType:{ serviceQuality: #D, sizeCategory: #M, dataClass: #MASTER }
  3. Apply source code formatting.

    1. From the eclipse menu, choose Source CodeFormat. Alternatively, choose Shift + F1.

Task 2: Propagate Annotations

Enable the propagation of element annotations in your new view and analyze the result in the Active Annotations view. Then disable the propagation again and specify the mandatory element annotation directly.

Steps

  1. In your new data definition, enable the propagation of element annotations.

    1. Change the value of annotation @Metadata.ignorePropagatedAnnotations from true to false.

  2. Activate the data definition.

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

  3. Open the Active Annotations View and analyze the element annotation values for view elements AnnualSalary and CurrencyCode .

    1. Right-click anywhere in the source code of the data definition and choose Open WithActive Annotations.

    2. The Active Annotations view appears as a new tab below the editor view.

    3. From the toolbar on the Active Annotations view, choose Collapse All.

    4. Expand Z##_C_EMPLOYEEAnnualSalary@SemanticsAmount.

  4. In your new data definition, disable the propagation of element annotations again.

    1. Change the value of annotation @Metadata.ignorePropagatedAnnotations back to true.

  5. Solve the syntax error by adding the mandatory element annotation directly to the AnnualSalary view element.

    1. Adjust the code as follows:

      Code Snippet
      Copy code
      Switch to dark mode
      12345
      DepartmentId, AnnualSalary, CurrencyCode,
  6. Activate the data definition.

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

Log in to track your progress & complete quizzes