Developing_with_Table_Inheritance
Developing_with_Table_Inheritance
Terminology
Microsoft Dynamics AX 2012 terms:
Term Definition
Root table A table at the head of the table inheritance hierarchy.
Leaf table A table at the bottom of the table inheritance hierarchy; no other tables
inherit from this table.
Base table A table with characteristics that can be inherited by other tables. A table
that functions as a base table can also be a derived table if it inherits
some of its characteristics from tables further up in the hierarchy.
Derived table A table that inherits some of its characteristics from one or more base
tables.
Type A unique data entity in a data model, represented by a table. The fields of
a table can be joined with the fields from the base tables (if any) in a
table buffer to represent all the fields for the type.
Note: Table inheritance properties such as Extends become enabled only if the
SupportInheritance property has been set to Yes.
Define an enumeration field of partyType on the Party base table. Name the field
DisplayRelationType. Override the insert table method on the Party table.
public void insert()
{
DictEnum enumType = new DictEnum(enumnum(partytype));
int enumValue;
if (this.DisplayRelationType != partytype::Unknown)
{
return;
}
enumValue = enumType..symbol2Value(this.getInstanceRelationType());
if (enumValue == 255) // symbol2Value returns 255 if cannot convert
{
SaveDataPerCompany
CacheLookup
ModifiedDateTime
ModifiedBy
ModifiedTransactionId
CreatedDateTime
CreatedBy
CreatedTransactionId
OccEnabled
EntityRelationshipType
InstanceRelationType
ValidTimeStateFieldType
10
Table properties not listed above are regular table properties that are not overloaded for defining
hierarchy behaviors. The properties TitleField1 and TitleField2, however, will inherit the values from
the base table if not defined on the current table. This inheritance principle also applies to the Field
Group definitions (such as AutoIdentification, AutoLookup, AutoReport and so on) under a table
node on table inheritance hierarchies.
Table methods
Microsoft Dynamics AX 2012 provides the same support for table method inheritance and
polymorphism on the table inheritance hierarchy that developers encounter with classes.
Developers can access the base table method on a derived table buffer. In addition, a derived table
method can override a base table method and call super() to invoke the same method on the base
table buffer. Upcasting and downcasting among base table and derived tables instances is also
supported. The actual methods invoked depend on the instance type at run time.
Certain limitations apply, as follows:
11
Figure 4: Automatically created relations between the base and derived tables
Developers can define extra relations on tables involved in the table inheritance hierarchy that are in
keeping with the data model. These relations must be named uniquely across the entire hierarchy
because they will be visible to the base and derived tables when the hierarchy is involved in designing
queries or forms.
12
DictTable.extends
DictTable.extendedBy
DictTable.fieldCnt
DictTable.fieldCnt2Id
DictTable.fieldNext
DictTable.fieldGroupCnt
DictTable.fieldGroup
DictTable.relation
DictTable.relationCnt
DictTable.objectMethodCnt
DictTable.objectMethod
DictTable.objectMethodObject
DictTable.titleField1
DictTable.titleField2
DictRelation.loadTableRelation
DictRelation.loadFieldRelation
Implicit enhancements to a range of reflection APIs were also made to supply these reflection APIs or
intrinsic functions with fields or relations defined uniquely on the inheritance hierarchy. In other
words, you do not need to specify a table scope for these reflection APIs. Examples are as follows:
DictTable.fieldName
DictTable.fieldObject
DictTable.fieldName2Id
DictTable.fieldNum
13
info(tstGov.getInstanceRelationType()); //GovermentOrganization
}
Microsoft Dynamics AX 2012 manages committing the data in a transaction across the relevant tables
in the inheritance hierarchy at run time when an insert operation is issued on a concrete type table
buffer. The inheritance relationship is reflected in the data by the propagating surrogate keys. The
data persisted in the database tables will look as follows.
14
The InstanceRelationType field defined on the Party base table is populated by Microsoft Dynamics AX
2012 and stores the TableIDs of the concrete types. However, when the type of a record instance is
needed, the developer should use the common.getInstanceRelationType API instead of directly
accessing the InstanceRelationType field.
ttsBegin;
while select forupdate * from tstOrg
{
tstOrg.State = "IL";
tstOrg.NumberOfEmployees = tstOrg.NumberOfEmployees+10;
tstOrg.update();
}
select forUpdate * from tstNpo;
tstNpo.AnnualContribution = 76543.21;
tstNpo.update();
ttsCommit;
}
Running the UpdateOrg code will produce the following data in the tables.
15
It is important to note that optimistic concurrency control (OCC) is applied to the concrete type buffer.
Therefore, the application runtime will detect the update conflict if multiple processes attempt to
update the same concrete type record, even if the updates occur on different back-end database
tables.
16
Running the TableMethodInheritance code will produce the results shown in Figure 5. Because we do
not override the JustSayHello method on the NonProfitOrganization table but only on the
Organization and Party, calling JustSayHello on the NonProfitOrganization table buffer invokes the
JustSayHello method defined on Organization and Party sequentially, as chained upwards by the
super() calls.
17
18
19
NonProfitOrganization npo;
Customer cust;
while select firstonly * from npo join cust
order by npo.Name
where
Cust.party == npo.recid
&& npo.NumberOfEmployees>100
&& npo.state == 'IL'
{
info(cust.AccountNumber);
info(npo.Name);
}
}
The Customer table schema has a foreign key relationship to the Party table, as shown in Figure 7.
20
21
22
//Only Party and Organization tables will be joined in the SQL query.
select name from Org join cust
where
Cust.party == Org.recid
&& Org.NumberOfEmployees>100;
info(Org.name);
23
24
The QueryPartyCustomer_adhoc query demonstrates an AOT query that queries the Person and
Organization customers in a particular state and returns the customer’s accountnumber, name, email,
and gender (if the customer is a person). Note that the developer is able to select the field gender
from a table Person derived from the data source Party.
25
26
System services
In Microsoft Dynamics AX 2012, the services programming model provides support for developing
service-based applications that integrate business logic and data with Microsoft Dynamics AX. The
table inheritance artifacts are fully exposed and ready to be consumed through the system services. In
particular, developers can create and execute queries against the table inheritance hierarchy through
the query metadata and query services, and can expect to see the same run-time behavior and
programming artifacts that are exposed to X++ developers.
Set-based operations will revert to record-by-record operations under certain conditions. These
conditions are listed in the following table of supported actions on table inheritance hierarchies.
27
28
29
Often, you do not want the system default type picker dialog to be displayed when creating new
records because of certain constraints imposed by the application requirements. If this is the case, you
need to override the FormRun.CreateRecord method to replace the default system type picker
dialog. In particular, the FormDatasource.CreateTypes method can be invoked in the overridden
FormRun.CreateRecord method to create a new record of the concrete type specified. The
developer can reference the SYS layer of the dirPartyTable form for the detailed design pattern.
Because the types of records displayed in a polymorphic form vary, Microsoft Dynamics AX 2012
displays visual cues on form controls bound to data fields that are not available. Figure 11 shows that
the NonProfitOrganization field-bound edit boxes are disabled when a GovernmentOrganization record
―Illinois State Tax Authority‖ is created.
31
Figure 11: Visual cues on form controls bound to data fields that are not available
32
Figure 12: Organization form with Organization Type column bound to DisplayRelationType field on
Party table
33
34
35
36
Figure 15: Automatic expansion of the base table data source and derived table data sources
37
Figure 16: Fields of the derived data sources listed in the designer
38
39
40
3. To add the report to the AOT, create an Autodesign by right clicking the Designs node,
clicking Add > AutoDesign, and specifying ―Organizations‖ as the name.
4. Add a Table node to the design.
5. Drag the NumberOfEmployees and Name fields from the Organizations node in DataSets
onto the Data node of the table.
41
42
43
44
Figure 20: OrganizationForm form with read-only records of all concrete type tables
Permission sets generated by the AutoInference feature can be modified, in place, or overridden. You
can override them in either the PartyTestPriv privilege or the PartytestRole role to change the
effective permissions on the concrete types.
45
46
Data upgrade
Microsoft Dynamics AX 2012 manages data access operations on a live system to ensure that the
inheritance relationship is correctly set on the data stored in the hierarchy. However, when performing
a data upgrade from a previous version of Microsoft Dynamics AX, the developer is responsible for
setting the correct inheritance relationships of the data in the target tables if the data schema of the
upgrading solution is being converted to a table inheritance model. These responsibilities include:
Linking the data stored in the base table and a derived table by setting the
baseTable.RecId=derivedTable.RecId relationship in the upgrade script.
Populating the InstanceRelationShip field on the base table with the correct value (the
corresponding TableID of the concrete type of the record instance).
Populating the RelationType fields on each table with the derived TableID that the record
spans across. The value is set to zero on the table corresponding to the concrete type.
Developers can refer to the upgrade scripts that migrate the global address book data from earlier
versions of Microsoft Dynamics AX to Microsoft Dynamics AX 2012 for detailed programming patterns.
47
This document supports a preliminary release of a software product that may be changed substantially prior to final
commercial release. This document is provided for informational purposes only and Microsoft makes no warranties,
either express or implied, in this document. Information in this document, including URL and other Internet Web
site references, is subject to change without notice. The entire risk of the use or the results from the use of this
document remains with the user. Unless otherwise noted, the companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted in examples herein are fictitious. No association with
any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended
or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without
limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a
retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or
otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights
covering subject matter in this document. Except as expressly provided in any written license agreement from
Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights,
or other intellectual property.
© 2011 Microsoft Corporation. All rights reserved.
Microsoft, the Microsoft Dynamics Logo, Microsoft Dynamics, MSDN, SQL Server, and Visual Studio are trademarks
of the Microsoft group of companies.
48