InheritanceAttribute Class
Specifies where an entity descendant class stores its data.
Declaration
InheritanceAttribute = class(
TCustomAttribute
)
Remarks
Defining class inheritance with the InheritanceAttribute requires that you specify where each entity class descendant stores its data. You can accomplish this by passing a TdxMapInheritanceType option as the attribute’s parameter. The attribute uses the TdxMapInheritanceType.ParentTable option if nothing is passed. Storing data in the ancestor’s table (corresponds to the TdxMapInheritanceType.ParentTable option) expects that you:
Introduce a discriminator column in the ancestor using the DiscriminatorColumnAttribute;
Provide this class and each entity descendant class with a discriminator column value that uniquely identifies their rows in the table using the DiscriminatorAttribute.
Entity class descendants inherit their primary keys from their ancestors – so you need to identify it only once per inheritance tree (in the ancestor).
The following code examples show how to define class inheritance for a simple entity model using the attributes described above. Note how the TdxMapInheritanceType options affect the resulting data store schemas.
Consider an entity model including two entities:
A Person entity containing generic personal information;
An Employee entity (a Person descendant), extending its ancestor with employment information.
The following entity model uses the TdxMapInheritanceType.ParentTable option to store the Employee entity’s information in the Person entity’s table. A discriminator column called ‘Discriminator’ is added to this table, providing identity values for Person and Employee entity rows (0 and 1, respectively).
uses
..., dxEMF.Core, dxEMF.Attributes, dxEMF.Types;
type
[Entity]
[Automapping]
[DiscriminatorColumn('Discriminator')]
[Discriminator(0)]
TPerson = class
strict private
FId: Integer;
FAge: Integer;
FName: string;
public
[Generator(TdxGeneratorType.Identity)]
property Id: Integer read FId;
property Age: Integer read FAge write FAge;
property Name: string read FName write FName;
end;
[Entity]
[Automapping]
[Inheritance(TdxMapInheritanceType.ParentTable)]
[Discriminator(1)]
TEmployee = class(TPerson)
strict private
FHireDate: TDate;
[Size(20)]
FJob: string;
public
property HireDate: TDate read FHireDate write FHireDate;
property Job: string read FJob write FJob;
end;
Calling the CreateSchema procedure of a session component connected to an empty SQLite database and passing ‘TEmployee’ as a parameter creates the following schema:
The following entity model uses the TdxMapInheritanceType.OwnTable option to store the Employee entity’s information in a separate table. With this option, the entity classes no longer require the DiscriminatorColumnAttribute and DiscriminatorAttribute.
uses
..., dxEMF.Core, dxEMF.Attributes, dxEMF.Types;
type
[Entity]
[Automapping]
TPerson = class
strict private
FId: Integer;
FAge: Integer;
FName: string;
public
[Generator(TdxGeneratorType.Identity)]
property Id: Integer read FId;
property Age: Integer read FAge write FAge;
property Name: string read FName write FName;
end;
[Entity]
[Automapping]
[Inheritance(TdxMapInheritanceType.OwnTable)]
TEmployee = class(TPerson)
strict private
FHireDate: TDate;
[Size(20)]
FJob: string;
public
property HireDate: TDate read FHireDate write FHireDate;
property Job: string read FJob write FJob;
end;
Calling the CreateSchema procedure and passing ‘TEmployee’ as a parameter now produces the following schema in an empty SQLite database: