The 4D language supports the concept of classes and thus, the concept of constructor.
On the another hand, the ORDA abstraction layer, through Data Model Classes, provides a great business benefit. It allows you to write business-oriented code and “publish” it just like an API. Datastore, dataclasses, entity selections, and entities are all available as class objects that can contain functions as well as computed attributes, and aliases.
This leads your apps to follow easily the MVC principles with powerful and optimized code.
To work with a complete object-oriented approach, starting from 4D 20 R10, entity classes can now have a constructor(). Need to set up initial values when a new entity is instantiated? It’s now possible! Keep reading to learn more …
With ORDA data model classes, data is handled through entities which are instances of the Entity classes you have in your Structure.
The 4D 20 R10 brings the possibility for you to implement a constructor() for entities objects.
The entity constructor
To work with a complete object-oriented approach, starting from 4D 20 R10, entity classes can now have a constructor() which is run when they are instantiated
This is the perfect way to initialize some default values such as a stamp or a createdBy attribute for example.
Before, to do this, you probably implemented a createNew() function in the appropriate dataclass. Given a Products dataclass, your developers must be aware there is a createNew() function in this class and that they must use it instead of naturally instantiate a Products entity using ds.Products.new().
To create a new product, they do:
Form.product:=ds.Products.createNew()
how to implement the constructor
Just implement the constructor() in the entity class.
Here is the ProductsEntity class:
Class extends Entity
Class constructor()
This.stamp:=Timestamp()
This.createdBy:=Current user()
And the calling code is so logic. For example, when clicking on the Create product button in a form, just do:
Form.product:=ds.Products.new()
and the Form.product entity comes with the stamp and createdBy attributes properly filled!

The constructor() is not only triggered by calling the new() function. Whatever the means the entity is instantiated with, the constructor is triggered.
It can also be:
– the Data explorer (when creating a new entity)
– the fromCollection() function
– a REST API call creating a new entity on the server (e.g. $method=update)
– code running on the REST server and processing an entity instanciated on a client such as a Qodly app front end or a 4D client using a remote datastore.
other examples
Given this REST request:
127.0.0.1/rest/Products?method=update
run using a POST with the body:
[
{"name": "Red pack", "price": 50}
]
Here is the result in the Data explorer. Note the values for the stamp and createdBy attributes.

When importing producs with the fromCollection() function:
var $products : cs.ProductsSelection
var $data:=New collection({name: "Red pack"; price: 50}; {name: "Green pack"; price: 10})
$products:=ds.Products.fromCollection($data)
Here is the result. Note the values for the stamp and createdBy attributes.

This blogpost explains how to implement the constructor().
After the reading, if you need to learn more about how the constructor() behaves in C/S or using 4D Qodly pro or when working with a remote datastore, read this blogpost (coming soon).
Check the documentation to learn more and play with the HDI!
