31 Jul. 2011

Dynamics AX 2012 Table inheritance & polymorphism

In the version pre-AX 2012 all tables in the AOT extended from the Common table so that all tables had the methods from the xRecord class and you could use an object of type Common as a polymorphic reference to any table in the AOT. All this still applies to AX 2012 but the notion of table inheritance now different. Here is the list of the new features in AX 2012:
  1. Field inheritance
  2. Method inheritance
  3. Method polymorphism
  4. Casting
  5. CUD support for the entire graph of tables in the hierarchy
The first 3 points are pretty self-explanatory, but things start to get interesting with point 4. Casting. Consider the following code fragment:

    DirPartyTable   dirPartyTable;
    DirPerson       dirPerson;
    
    dirPartyTable = dirPartyTable::find('7');
    
    info(DirPartyTable.Name);
    
    if(dirPartyTable is DirPerson)
    {        
        DirPerson = dirPartyTable as DirPerson;
        info(enum2str(dirPerson.BirthMonth));        
    }        

Note how the record is casted from type dirPartyTable to type DirPerson. Dynamics AX 2012 introduces 2 new X++ operators is and as. Here the is operator checks if the dirPartyTable object can be downcasted to its subtype dirPerson. The as operator is used to actually downcast the dirPartyTable object to the dirPerson subtype. So far this is similar to what we could achieve (with slightly more X++ code) with the previous versions of AX, but this is not just syntax-sugar. In AX 2012 when you select a record with select * from this means select all fields form all parent and child tables and this is fundamentally different from the previous versions of AX.

Now, let's consider point 5. CUD support for the entire graph of tables in the hierarchy

Obviously CUD here stands for Create, Update, and Delete and "support for the entire graph of tables in the hierarchy" means that all CUD operations are cascaded to all parent and child tables in inheritance hierarchy. For example in AX 2012 when you create a record in a child table the same record will be created in the parent table as well. When you delete a record from table, belonging to a table inheritance tree, the same record will be deleted from all child and parent tables in inheritance hierarchy. It is important to note that if a the cascade operation on a child or parent table fails then the entire transaction will be aborted.




No comments:

Post a Comment