Wednesday, April 18, 2012

2012 Table Inheritance

In AX 2012, a table can derive from another table. Two properties SupportInheritance and Extends together control table inheritance

Few important points to remember:
1. Table inheritance is described in terms of base table and derived table. Terms Parent/Child table are used to describe foreign key relationships.
2. When a row is inserted into a derived table, AX kernel inserts the fields from the derived table into the derived table and the fields from the base table into the base table.
3. The RecId field in both the base and derived tables is the same.
4. The InstanceRelationType field on the base table stores the Id of the derived table that the record originated in.
5. In case there are more than 2 tables in the hierarchy, a field relationType is also filled by the AX kernel. The relationType field stores the Id for the table directly above the current table in the hierarchy. The field in invisible in Table browser but can be seen in SQL.
6. Deleting a record in a base table or a derived table will automatically cascade delete the record in other table(s).

Downcasting
Downcasting is casting a base table to a derived table. If a base table has more than one table derived from it and you cast to a derived table, you will see the fields in the derived and base tables that exist for that record.

Imagine a case of three tables.
TableDerived2  extends TableDerived1 extends TableBase
Syntax for downcasting from TableDerived1 to TableDerived2 :
TableDerived1 tableDerived1;
TableDerived2 tableDerived2;
select * from tableDerived1 where tableDerived1.Field1 == 'Demo';
if(tableDerived1 is TableDerived2)  //is keyword
{
            tableDerived2 = tableDerived1 as TableDerived2;  //as keyword
}
is and as - the new yin and yang?
The "is" operator returns true if the table is a derived of the base table, or if the object is the same type as the class.
The "as" keyword is used to downcast a base table variable to a derived table variable. The "as" keyword tells the compiler that you believe the downcast will be valid during run time. The "as" keyword applies to the downcasting of both tables and classes. If the cast is not valid at runtime, null is assigned to the variable.
Like downcasting, we have upcasting as well. More on that later.
Also, once a table has been downcasted to a type using the "as" keyword, the methods on that table are available for use as are the methods on its base table.

Abstract err... table?
The Abstract property on a table indicates that a table buffer cannot be instantiated directly. It can only be instantiated through its descendent/derived tables. This is very similar in concept to an abstract class.

Derived Data Sources
If you drop a base table as a datasource in a form, a new type of node called Derived Datasources appears. The Derived Datasources node on a form contains all of the tables that inherit from the base table added as a data source to that form.
If a form is using a data source that has multiple derived data sources, the query the form uses to retrieve data can be very expensive. To reduce the size of the query use the OnlyFetchActive property located on a form data source. When the OnlyFetchActive property is set to Yes the query generated by the form only joins to the tables that have bound controls on the form's design.

Very interesting concept indeed. But table inheritance should be used sparingly and only where it truly makes sense. Because it has its own performance degradations.

No comments:

Post a Comment