Difference between revisions of "DTO DSL"
(→id) |
(→version) |
||
Line 158: | Line 158: | ||
[[File:dto-10.png|center|frame|''Figure 10 - A version property can be mapped into a DTO by the <code>version</code> keyword.'']] | [[File:dto-10.png|center|frame|''Figure 10 - A version property can be mapped into a DTO by the <code>version</code> keyword.'']] | ||
+ | </blockquote> | ||
+ | |||
+ | ===transient=== | ||
+ | <blockquote> | ||
+ | marks the property to be transient. Transient properties are not mapped from the DTO to the entity and thus not persisted. Getter and setter methods are created. | ||
+ | |||
+ | [[File:dto-11.png|center|frame|''Figure 11 - The <code>transient</code> keyword allows properties to be excluded from the mapping.'']] | ||
</blockquote> | </blockquote> |
Revision as of 12:54, 4 August 2016
Contents
Copyright Notice
All rights are reserved by Compex Systemhaus GmbH. In particular, duplications, translations, microfilming, saving and processing in electronic systems are protected by copyright. Use of this manual is only authorized with the permission of Compex Systemhaus GmbH. Infringements of the law shall be punished in accordance with civil and penal laws. We have taken utmost care in putting together texts and images. Nevertheless, the possibility of errors cannot be completely ruled out. The Figures and information in this manual are only given as approximations unless expressly indicated as binding. Amendments to the manual due to amendments to the standard software remain reserved. Please note that the latest amendments to the manual can be accessed through our helpdesk at any time. The contractually agreed regulations of the licensing and maintenance of the standard software shall apply with regard to liability for any errors in the documentation. Guarantees, particularly guarantees of quality or durability can only be assumed for the manual insofar as its quality or durability are expressly stipulated as guaranteed. If you would like to make a suggestion, the Compex Team would be very pleased to hear from you.
(c) 2016-2024 Compex Systemhaus GmbH
Purpose
The DTO DSL facilitates the creation and handling of data transfer objects (DTOs) in order to carry data between processes. Communication between processes is usually done by calls to remote interfaces and services where each call is a computationally expensive and time-consuming operation. The use of DTOs that aggregate the data that would have been transferred separately reduces the number of calls that are necessary, thus speeding up the operation. Furthermore, DTOs are decoupled from the JPA, thus eliminating burdensome dependencies.
Using the DTO DSL, it is easy to create DTOs and the mapper classes that link them to the respective persistence entities. Property change support is automatically included in order to have current data without direct dependencies on the persistence layer.
Overview
The main semantic elements of the DTO DSL are the following:
- Package - the root element that contains all the other elements. A model can contain multiple packages.
- Import declarations - used to import other DTO models or the entity model files that are covered by the DTOs.
- Datatype declarations - a way to define datatypes that can be used (only within the package - private scope).
- DTO - the model of a DTO that wraps an entity. It contains further elements such as properties and references. Appropriate mapper methods can be specified.
- Property - a reference to an enum, a Java class or a “simple datatype" (as defined in the datatype declaration). Can be inherited from the wrapped entity and offers multiplicity.
- Reference - a reference to another DTO. Can be inherited from the wrapped entity and offers multiplicity.
- Operations - similar to Java methods. The Xbase expression language can be used to write high-level code.
- Comments can be added to all elements.
► Caveat: In order to make use of the DTO DSL, one has to make sure that the OSBP builder is executed after the Xtext builder.
DTO model files
DTO models are described in .dtos
files. These files describe the DTO model and are the basis for the code generation. DTO models may be split over several .dtos
files containing packages in the same namespace.
A .dtos
file may contain several packages with DTOs.
package
Packages are the root element of the DTO DSL grammar. Everything is contained in a package: Imports, datatypes, DTOs and enums have to be defined inside the package definition. One document can contain multiple packages with unique names.
The elements a package can contain are DTOs and enums. Additionally, a package allows import statements and the declaration of datatypes.
► Syntax:
package name { import importStatement; datatype datatypeDefinition; DTOs }
import
In order to wrap entities into DTOs, the DTO DSL has to reference entities defined in entity model files. Furthermore, it is possible to reference DTOs in other packages. The
import
statement is a way to address these elements by their fully qualified name.Import statements allow the use of the
*
-wildcard.► Syntax:
package name { import importStatement; datatype datatypeDefinition; DTOs }
datatype
The DTO DSL allows the definition of datatypes. These are translated by the inferrer into their standard Java presentation. The behaviour of the generator can be controlled by the datatype definitions. There are three types of datatype definitions:
- jvmTypes
- Datatpye definitons that map types to jvmTypes take the basic syntax of
datatype <name> jvmType <type> as primitive;
Specifying datatypes in this manner uses an appropriate wrapper class in the generated Java code; adding the keywordas primitive
enforces the use of primitive datatypes where applicable:datatype foo jvmType Integer
compiles to anInteger
whereasdatatype foo jvmType Integer as primitive
results inint
.
- dateTypes
- The datatypes for handling temporal information can be defined by the following statement:
datatype <name> datetype date|time|timestamp;
Datatypes that have been defined in this manner can be used as property variables in DTOs.
- blobs
- Binary blobs can be handled by defining a datatype with the
as blob
keyword. The Java implementation of such a blob is a byte array.datatype <name> as blob;
After import statements and datatype definitions, the content of the .dtos file is made up of DTO definitions.
DTO
Data Type Objects (DTO) are the most complex elements in the DTO DSL. A DTO wraps an entity defined in the Entity DSL and its properties and references into a single object used for communication purposes.
DTO are defined by name, properties, references and wrapper methods. A DTO normally does not implement business logic.
For each DTO that is defined in a package, the DTO DSL automatically generates a Java DTO class and a corresponding mapper class that handles data transfer and conversion between DTO and entity.
► Syntax:
abstract dto <name> extends <parentDto> wraps <entityName> {
dtoFeatures
}
extends
extends marks a DTO that is derived from another DTO. That means that the properties and references of the parent DTO are inherited.
wraps
wraps indicates the entity that is handled by the DTO. The entity has to be imported from the entity model file into the DTO package (for importing several entities, the use of the _*_-wildcard may be advisable).
Enums
Enums are an abstraction above the Java enum. They compile to enum classes and can be used as properties in DTOs.
Properties
Properties of DTOs are references to datatypes or enums. They can be regarded as variables and are defined by a keyword followed by a datatype (Java type or datatype defined in the datatype section) and a name.
Properties can be determined with the following keywords:
var
The basic property. Defines a variable that can be used within the DTO. Appropriate getters and setters are created.
id
► -- deprecated -- deprecated -- deprecated -- deprecated -- deprecated -- deprecated -- deprecated -- deprecated --
defines a field in the DTO for mapping an
id
property (used as primary key in the persistence layer). This keyword is deprecated. If no id is given, a warning is shown. Caution: ID autogeneration causes problems since the value is not set before the entity is persisted in the database, which can cause problems.It is advised to use the
uuid
keyword instead.
uuid
creates a field for the mapping of Universally Unique IDs as primary keys. UUIDs have to be strings.
version
defines a field for mapping a version property used by the JPA-Compiler.
transient
marks the property to be transient. Transient properties are not mapped from the DTO to the entity and thus not persisted. Getter and setter methods are created.