Incorporating Common Reuse Aspects

Objective

After completing this lesson, you will be able to extend entity definitions by new elements with the help of CDS's aspects

Aspects

Let us now explore how the definition of entities can be enhanced with new elements using so-called aspects.

Watch the video to learn about CDS aspects.

In this lesson, we will learn about the extend directive. We will discuss the annotate directive later in another lesson.

The extend Directive

Using the extend directive you can add extension fields to existing definitions as shown in the following figure:

In the example, element someAdditionalField is added to the Authors domain entity. This also adds this field to the generated database table.

Note

Consumers always see the merged effective models, with the separation into aspects fully transparent to them.

With the extend directive, metadata such as annotations can also be added to existing definitions or existing metadata can be overwritten.

Type properties can also be set via extend. For example, the length of an existing element of type String can be increased.

Named Aspects

You can use extend with predefined aspects to apply the same extensions to multiple targets, as shown in the following figure:

The example shows a named aspect ManagedObject that defines the two elements createdAt and createdBy. Both the Books and the Authors entity are extended by these fields via this named aspect.

Note

The syntax aspect ManagedObject has the same effect as the syntax define aspect ManagedObject, i.e. the define keyword is optional.

A named extension can contain anything, such as fields, types or entities.

Includes as Shortcut Syntax

You can also use named aspects as includes in an inheritance-like syntax. This syntax allows you to extend a definition with several named aspects at the same time. In the following figure, the Authors entity is extended by the two named aspects ManagedObject and AnotherAspect.

Includes are syntactical sugar. The example shown is equivalent to using a sequence of extends as follows:

Code Snippet
1234567
entity Authors {} extend Authors with ManagedObject; extend Authors with AnotherAspect; extend Authors with { key ID : UUID; ... }

Common Reuse Aspects

CDS ships with a pre-built model @sap/cds/common that provides common types and aspects for reuse.

It is recommended that all applications use the common types and aspects provided through @sap/cds/common. This allows you to benefit from best practices learned from real business applications.

In the following section, we will take a closer look at the two aspects cuid and managed from @sap/cds/common. Later we will also discuss the CodeList aspect and the Country and Currency types from this model.

Aspect cuid

The cuid aspect is a convenient shortcut that allows you to add canonical, universally unique primary keys to your entity definitions.

Note

As a reminder, service provider runtimes automatically fill in UUID-typed key fields with auto-generated UUIDs.

Aspect managed

The managed aspect can be used to add the four fields createdAt, createdBy, modifiedAt and modifiedBy to an entity. These fields are used to track who created and updated data records and when.

By using this aspect, you can store the relevant record-related management information and still keep your core domain model clean and comprehensible.

The fields createdBy and modifiedBy have the data type User, which is a common reuse type from @sap/cds/common.

The annotations @cds.on.insert and @cds.on.update used in the aspect denote elements that are automatically filled in by the generic handlers when inserting and updating.

The pseudo variables used in these annotations are resolved as follows:

  • $now is replaced by the current server time (in UTC)
  • $user is the ID of the current user as received from the authentication middleware

Note

The fields modifiedAt and modifiedBy are set whenever the respective row has been modified, i.e. also during CREATE operations.

Using Aspects from @sap/cds/common

To be able to use aspects from the @sap/cds/common model, you must import them via the using directive. In the following figure, for example, this is done for the common reuse aspects cuid and managed via the ES6-like deconstructor variant of using.

The two imported aspects are used as includes in the example to extend the Authors entity with the corresponding fields. Without the two predefined aspects, the following definition of the Authors entity would be required to achieve the same outcome as in the example shown using native means:

Code Snippet
123456789101112
entity Authors { key ID: UUID; createdAt : Timestamp @cds.on.insert: $now; createdBy : User @cds.on.insert: $user; modifiedAt : Timestamp @cds.on.insert: $now @cds.on.update: $now; modifiedBy : User @cds.on.insert: $user @cds.on.update: $user; name : String(100); dateOfBirth : Date; dateOfDeath : Date; books : Association to many Books on books.author = $self; }

Demonstration & Exercise: Use Pre-Defined Aspects

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 Associations to the Domain Model if you have successfully completed it. Alternatively, you can also use the branch 5_associations 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 6_common_reuse_aspects 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 use pre-defined aspects.

Log in to track your progress & complete quizzes