Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
AndreaUS
Product and Topic Expert
Product and Topic Expert
Since ABAP release 7.76, there's a new CDS entity available: the CDS projection view. This blog post explains what a CDS projection view is, what it does, and which components it consists of.

 








Update July 2022: Since ABAP release 7.83 | SAP BTP ABAP Environment 2102 | ABAP release 7.56, each CDS projection view must have a provider contract.

Syntax:
define view entity PVName
provider contract {transactional query | analytical_query | transactional_interface }
as projection on ProjectedEntity
{
...
}

Projection views without provider contract are implicitly set to the provider contract TRANSACTIONAL_QUERY. Therefore, the syntax described in this blog post refers to CDS transactional queries.

ABAP Keyword Documentation about provider contracts: PROVIDER CONTRACT

Blog post about types of CDS projection views: CDS Projection Views in ABAP CDS: What’s Your Flavor | SAP Blogs

 

Background on CDS views


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 similarly 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 post about CDS view entities.

What is a CDS projection view and what does it do?


CDS projection views serve a special purpose within the ABAP RESTful Application Programming Model (RAP). They 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. A CDS projection view is a CDS view defined using DEFINE VIEW ENTITY AS PROJECTION ON.


The following image shows the role that CDS projection views play in making a business service. Because CDS projection views are always based on existing CDS view models, they can't directly access a database table.


The image is an adaptation of the image published in the Developer Guide for the ABAP RESTful Application Programming Model.

Syntax of a CDS projection view


@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Exposure of SO Projection View'
define root view entity DEMO_SALES_PV_SO_EXP
provider contract transactional_query
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),
_Buyer : redirected to DEMO_SALES_PV_BUPA_EXP
}

 

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. You can also 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 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 (more details 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 (more details below).


Associations in CDS projection views


When CDS projection views were first published, it was not possible to define new associations. This feature was later introduced with ABAP release 7.80. The newly defined associations in CDS projection view have limitations though: they can 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 be used only to model new relationships that can be interpreted by consumer frameworks, such as SADL.

Virtual elements in CDS projection views


Virtual elements are not part of the projected entity - they are newly added elements that are calculated by frameworks, such as SADL, during runtime. Therefore, virtual elements cannot be read with ABAP SQL, ABAP SQL just returns initial values.

For example, in the ABAP RESTful Application Programming Model, 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 syntax 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


You can 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 association can be exposed directly. If the target entity is also projected, the association should be redirected to the target projection view, as shown for the association _Buyer 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 compositions in a CDS projection view


The image is an adaptation of the image 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 RAP 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.

More development on CDS projection views is being done. Soon there'll be different types of CDS projection views available: general, transactional, and analytical projection views.

To find out more, see our documentation:

 

 
26 Comments