Exposing Denormalized Views

Objective

After completing this lesson, you will be able to define services tailored to specific use cases using denormalized views

Path Expressions

The AdminService implemented in our scenario so far exposes the underlying data model in a 1:1 fashion. Such services are rather rare. Instead, services often expose denormalized views that are tailored to specific use cases. Let's take a look at an example in the following.

We model a CatalogService that exposes books and authors from the domain model for use in a bookstore application. In other words, this service is intended to enable catalog users to access books and their authors and to place orders.

The new service that we are now creating is also defined in the srv folder of the CAP project. We use the cat-service.cds file for this (see following figure).

First, we proceed in exactly the same way as with the AdminService: We import all definitions with the namespace prefix com.sap.learning from the file db/schema.cds and set the local alias db for the imported definitions (line 1).

Lines 3 to 31 then contain a service block that defines a service interface called CatalogService as a collection of exposed entities. The @path annotation is used to specify the URL path /cat under which the service will be accessible.

The CatalogService exposes two entities that are defined as projections on entities from the underlying domain model. Lines 5ff declare an entity called Books, which is a view on the Books entity defined in the imported domain model. Accordingly, an Authors entity is declared in lines 18ff as a projection on the Authors entity from the domain model.

We will look at the Authors entity in the CatalogService in more detail later. First, let's take a closer look at the Books entity.

In contrast to the Books entity in the AdminService, which exposes the underlying domain entity in a 1:1 fashion, the Books entity in the CatalogService uses an explicit select clause. This means that only what is explicitly listed in the select clause is exposed.

So-called path expressions can be used in the select clause to navigate along associations and/or structured elements.

In the example shown, the author association of the Books model entity is used to access the author's name and this is included in the select clause as writer.

Also publCountry is an association defined on the Books model entity. It is used to relate the Books entity to the sap.common.Countries code list. In the example, the country name from this code list is included in the select clause as publCountry.

The price element of the Books domain entity has a structured type. The two components amount and currency of this type are also included in the select clause via corresponding path expressions.

Smart Selector and Excluding Clause

Now let's take a look at the second entity that is exposed via the CatalogService. The Authors entity in the CatalogService is a projection on the Authors entity from the imported domain model. 

Using the excluding clause in combination with the smart * selector in the select clause, all elements of the Authors domain entity except those listed in the exclude list are included in the projection.

The Authors model entity has an epoch association via which the Authors entity is related to the Epochs code list. The epoch name from this code list is included in the projection as an additional element with the name period using a path expression.

Demonstration & Exercise: Define a Service Based on Denormalized Views

Note

As exercise, carry out the step-by-step instructions in the following demonstration yourself in the SAP Business Application Studio.

As a starting point for the exercise, use the outcome of the previous exercise Add Optimistic Concurrency Control if you have successfully completed it. Alternatively, you can also use the branch 9_concurrency_control from the following GitHub repository as a starting point:

https://github.com/SAP-samples/cap-development-learning-journey

The complete implementation of the simulation can be found in the 10_denormalized_views branch of the GitHub repository.

Detailed information on the content of the repository and how to use it can be found here.

Watch the video to see how to define a service based on denormalized views.

Log in to track your progress & complete quizzes