You can declare custom types for elements in entity definitions. The custom types can be simple types, i.e. derived from one of the built-in types, structured types or associations. Here we will deal with simple types and structured types. Associations will be covered later in the learning journey.
Simple Types
In figure Example of a Simple Type, a simple type called NoOfBooks is defined. It is based on the built-in data type Integer. This means that wherever NoOfBooks is used, it essentially represents an Integer value.
The Books entity shown has a stock element that is typed with the NoOfBooks type. This means that the data type of this element is essentially Integer.
The definition
1
type NoOfBooks : Integer;
used in the figure is equivalent to the following:
1
define type NoOfBooks : Integer;
This means that the keyword
define is optional.
Note
Custom types can increase the semantic expressiveness of your models. However, avoid excessive use of such types. They are valuable if you have an appropriate level of reuse. Without reuse, your models will only become more difficult to read and understand, as you will always have to look up the relevant type definitions.
Structured Types
Structured types are custom data types that combine one or more related elements under a single type. They make it possible to create complex data structures and use them within entities.
The Books entity shown in figure Example of a Structured Type has a price element of type Price, which is a separately defined structured type.
The Price type has two elements: amount of built-in type Decimal and currency of built-in type String with a maximum length of 3 characters.
Hint
Naming conventions: It is recommended to capitalize type names in the same way as entity names. In contrast to entity names, however, use the singular form for type names (e.g. Price). Enumerations
Enumerations (or enums for short) can be used to make code more readable and self-explanatory, as they allow you to replace cryptic values with symbols in the application logic.
You can specify enumeration values for a type as a semicolon-separated list of symbols with assigned corresponding values. For string types, the assignment of actual values is optional; if omitted, the actual values are the string counterparts of the symbols.
The Books entity in figure Example of Enums has an element called genre of type Genre, which is a custom type with enumeration values.
The Genre type is based on the built-in Integer type, which means that each symbol in the enumeration corresponds to an Integer value.
The Genre enumeration contains two symbols: fiction, which corresponds to Integer value 1, and non_fiction, which corresponds to Integer value 2.