表继承(Table inheritance)是AX 2012的新特性。
AX当中的表并不等价于数据库表对象,有一部分AX表在数据库当中并没有对应的物理表,而仅仅在应用层作为独立的对象存在。这些表的所有字段都是继承自母表。
从表结构出发理解它的原理:
母表: EcoResProduct (The EcoResProduct table stores products and is the base table in the products hierarchy)
子表:EcoResDistinctProductVariant/ EcoResProductMaster/ EcoResDistinctProduct 全部继承自EcoResProduct
母表的属性InstanceRelationType是区分子表的标识列
子表的属性Extends表示继承自哪张母表
属性LegacyId是表的标识
三张子表的数据都是保存在母表当中,仅以INSTANCERELATIONTYPE 列加以区分。INSTANCERELATIONTYPE 的值即为子表的LegacyId.
从字段方法理解它的运用:
在AX界面上通过 产品信息管理——常用——产品——已发布产品 可以找到每个产品有一个字段叫“产品子类型”
再通过右键“个性化”,可以看到该字段并不是一个具体字段,而是通过一个方法(EcoResProduct.productSubtype)取得的值。
继续跟踪到代码如下:
public display EcoResProductSubtype productSubtype()
{
switch (this.getInstanceRelationType())
{
case tableStr(EcoResDistinctProductVariant):
return EcoResProductSubtype::ProductVariant;
case tableStr(EcoResProductMaster):
return EcoResProductSubtype::ProductMaster;
case tableStr(EcoResDistinctProduct):
return EcoResProductSubtype::Product;
default:
return 0;
}
}
代码逻辑非常简单,根据getInstanceRelationType方法取得了InstanceRelationType,然后返回相应的枚举值EcoResProductSubtype.