Basic concepts of Entities

Entity collections are representations of a database table in memory. An entity is one record within this collection. In the AI-Framework, data manipulation is done in memory and then it automatically updates the actual database. Entities are more than just a copy of data on disk in memory. They contain additional properties and methods. This adds a functional approach to data manipulation.

More technical details about entities and their properties and methods are found in Modelling > Programming > Entities > Entities 

In this article, we will see how to set up an entity and use it in the model. When talking about 'setting up an entity', the model entity is in mind. There is also a generated entity, which is automatically generated.

 

Two levels of entities

The AI-Framework has two levels of entities.

  1. The generated entity
    When the model of a database table has been defined, and the code for that database table is generated, the AI-Framework will automatically build the generated entity. In it are all the fields of the table available.

  2. The model entity
    This model entity inherits from the generated entity. Apart from that this model entity is empty. It is in this entity that the model programmer in the AI-Framework adds extra properties and methods.

 

Why use an entity

An entity in the AI-Framework is based upon a record from a database table. To be more precise: An entity in the model of the AI-Framework inherits from an automatically generated entity (see above), which is based on a database table. Every single table that is used in the (model) software can have one or more records and any record can have one or more entities. When the data of this table needs to be accessed, it is done by loading the entity collection. The AI-Framework then automatically connects to the database and finds what is needed from the data table.

The strength of working with entities in the AI-Framework lies in the properties and methods that can be associated with the data.

Simple example with a property in the entity

  • We have a table, named Stock. In it we store article numbers, how many are on stock, the required minimum, where the articles are on stock and more.
  • When generating the source, we automatically create a generated entity which make these fields available in the model. Also an empty model entity is created, which inherits from the generated entity.
  • In this model entity, we will add an extra property, a CalculatedBooleanProperty named IsLowOnStock. It will be true when the number of articles on stock is less than the minimum required.

    Note: The properties and methods in the model entity are defined in one file. These properties and methods will then be available for each single entity (record) that is based on the loaded entity collection. So the CalculatedBooleanProperty named IsLowOnStock will be calculated afresh (and may be different) for each different entity (record).

  • With this Boolean available on entity level, we can quickly see if new articles need to be ordered, without having to do the calculation each time. The content of the properties is updated the moment they are needed.

Simplified code example:

new If(Stock.IsLowOnStock)
{
    ...
},

The Boolean IsLowOnStock has been defined in the model entity and is available and updated live each time the Boolean is needed.

Expanding the example by adding a method to the entity

  • We create a method, that calculates how many of these articles have been sold over the last week.
  • Using this information, we can then calculate when new stock needs to be ordered, so we are in time and not running out of stock.
  • All this calculated information is then available, right from the entity, without having to do the calculations each time.

Summarised, properties and methods make accurate information available straight from the entity. This means the logic has been lifted up to the level of the data, which makes it easier for the programmer to write the operations of the model.

 

Setting up an entity

In the following part, we will set up a database table, look at the generated entity and then set up an entity that inherits from this generated entity.

...

Using an entity

Once the entity has been properly set up, it can be used in many different ways. ...

The volatile entity

The volatile ...

See also ...

Summaries

A special type of entities are the summaries ...

See also ...