Tuesday, 15 September 2020 08:04

New type of CDS entity available: CDS projection views

Written by Andrea Schlotthauer
Rate this item
(0 votes)
“© 2020. SAP SE or an SAP affiliate company. All rights reserved.” “Used with permission of SAP SE”

With ABAP release 7.76, there’s a new CDS entity available: the CDS projection view. Read this blog post to learn what a CDS projection view is, what purpose it serves, and which components it consists of.

Background: What is a CDS projection view and what is its function

A CDS projection view is a CDS view defined using DEFINE VIEW ENTITY AS PROJECTION ON. There are currently three types of CDS views available: CDS view entities (DEFINE VIEW ENTITY), CDS DDIC-based views (DEFINE VIEW), and CDS projection views. CDS projection views have been designed similar to CDS view entities, for example, they don’t have an SQL view attached. For further details on the different types of CDS views, see this blog post about CDS view entities.

CDS projection views serve a special purpose within the ABAP RESTful Application Programming Model. They are mainly used to define interfaces on the basis of existing CDS view models. A projection view is always based on exactly one existing CDS view entity or CDS DDIC-based view and exposes a subset of its elements, which are required for a specific business service.

The following image shows the role of CDS projection views in the creation of a business service. CDS projection views are always based on existing CDS view models and they address the needs of a specific business service. They cannot directly access a database table.

CDS%20data%20model%2C%20created%20by%20myself

CDS data model, image created by myself

Syntax of a CDS projection view

@AccessControl.authorizationCheck: #CHECK @EndUserText.label: 'Exposure of SO Projection View' define root view entity DEMO_SALES_PV_SO_EXP as projection on DEMO_SALES_CDS_SO { key so_key, id, lifecycle_status as LifecycleStatus, buyer_id as BuyerId, created_by, @ObjectModel.virtualElementCalculatedBy: 'ABAP:CL_DEMO_SALES_VIRT_ELEM_EXIT' virtual expiry_date : abap.dats(8), _Items : redirected to composition child DEMO_SALES_PV_SO_I _Buyer }

 

The example shows the syntax of a CDS projection view:

  • Annotations (entity annotations, view annotations, and element annotations) are inherited from the projected entity by default, but they can be overwritten. It is also possible to add new annotations.
  • A projection view is defined using DEFINE [ROOT] VIEW ENTITY AS PROJECTION ON.
  • The data source of a CDS projection view can be either a CDS view entity without parameters, or a CDS DDIC-based view without parameters (this is still supported for reasons of downward compatibility).
  • In the element list, fields and associations from the underlying projected entity are exposed.
  • New calculated fields can be added with the keyword VIRTUAL (please find further details on virtual elements below).
  • All elements can be given a new alias name.
  • Associations from the projected entity can either be exposed directly, or redirected to a new target (please find further details on redirection below).

Associations in CDS projection views

Since ABAP release 7.80, it is possible to define new associations in CDS projection views. These associations have limitations though: they can only be defined and exposed in the element list, but they cannot be used to include fields from the association target in the current projection view. Associations in CDS projection views can only be used to model new relationships that can be interpreted by consumer frameworks, such as SADL.

Virtual elements in CDS projection views

Virtual elements are read-only elements of a CDS projection view which are not part of the projected entity. Reading the virtual elements of a projection view with ABAP SQL delivers initial values. In order to fill and analyze the virtual elements of projection view, special frameworks like SADL have to be involved.

In RAP, for example, a virtual element can be connected to an ABAP class that implements a special virtual element interface by using the framework-specific annotation ObjectModel.virtualElement.CalculatedBy. In that case the value of the virtual element is calculated during runtime by the SADL framework.

In the example above, the class CL_DEMO_SALES_VIRT_ELEM_EXIT is used to calculate the expiry date of a sales order item.

For more information, see Using Virtual Elements in CDS Projection Views.

Redirecting exposed associations in CDS projection views

It is possible to expose a CDS association of the projected entity in the element list of a CDS projection view. If the target of the association does not change in the projection layer, the associations can be exposed directly – this is the case for the association _Buyer in the example above. If the target entity is also projected, the association should be redirected to the target projection view – this is done for the association _Items in the example above. There are three different keywords available for redirection, depending on the type of association:

  • REDIRECTED TO: redirects a simple association
  • REDIRECTED TO COMPOSITION CHILD: redirects a composition. The redirected association must be a CDS composition
  • REDIRECTED TO PARENT: redirects a to-parent association. The redirected association must be a to-parent association.

The image below shows how several views belonging to a business object are projected and redirected.

Redirected%20compositions%20in%20a%20CDS%20projection%20view

Redirected compositions in a CDS projection view. Image was first published in the Developer Guide for the ABAP RESTful Application Programming Model.

Outlook

Do CDS projection views remind you of consumption views? That’s right: CDS projection views are the ABAP RESTful Application Programming Model’s pendant to consumption views and new scenarios within this framework will be designed using CDS projection views. The existing consumption views will still be available, but fewer new ones will be created.

Development of CDS projection views is still ongoing, there will soon be a distinction between different types of CDS projection views, namely general, transactional, and analytical projection views.

To find out more, see our documentation:

  • The ABAP Keyword documentation explains all possible additions and their functionality. It is also available in SAP GUI and in ADT via the F1 key.
  • The developer guide for the ABAP RESTful Application Programming model explains how to create CDS projection views.

 

Read 62 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.