Arthur Greef and Michael Gall.

In part 1 of this blog entry series, we explained how classes can be extended by using the Microsoft Dynamics AX 2012 extension framework and statically declared class attributes. In part 2, we described the application model elements and application model attributes that are used to extend any application object tree (AOT) object dynamically. In this blog entry, we explain to how to use model attributes and model elements to program dynamic object extensions.

Using model elements as model attributes to program dynamic object extensions

The following example shows how to create a model element object from the DocumentType base enumeration and to then use the model element to create an instance of a model attribute. The resulting model attribute is then used to cache an extension point for the InventoryPolicy1 class.

public void build()

{

    SysExtModelElement      element;

 

    element = SysExtModelElementAppFactory::newFromEnum(enumNum(DocumentType), DocumentType::Doc1);

 

    SysExtension::addExtension(
        SysExtModelElementAppFactory::newFromClassName(classStr(InventoryPolicy1)).parmCacheKey(),
        SysExtModelAttributeInstanceFactory::newFromElement(element).parmCacheKey());

}

 

This extension point could be used to extend the FormLetterJournalCreate class instead of using the FormLetterJournalCreate.newSalesJournalCreate factory method currently implemented in Microsoft Dynamics AX 2012. Both methods use the DocumentType base enumerator to identify class extension points. The extension framework, however, will decouple the base FormLetterJournalCreate declaration from its extensions.

Checking extension design constraints

The SysExtensionValidatorStrategy class can be extended to check that extensions meet design constraints. For example, an extension validator can be used to check if more than one extension point maps the same model element to more than one class extension. An exception can be thrown if a design constraint requires a model element to uniquely identify a class extension.

 

The extension framework provides the SysExtAppClassUniqueValidator class that enforces a unique model attribute-class extension constraint for all extension classes in a class hierarchy. You can implement your own SysExtensionValidatorStrategy, and in the validate method you can implement additional validation logic (such as, allowing only extensions with particular model attributes, and so on). The validators will be executed after the dynamic builders have been executed.

Runtime extension manipulation

Model attribute extensions can be manipulated at runtime. For example, a model attribute instance that is assigned to a particular model element can be replaced with a different model attribute instance as the following example shows.

static void RuntimeExtension(Args _args)

{

    SysExtModelElementApp           element;

    SysExtModelAttributeInstance    attributeInstance;

   

    // Get the element where you want to replace something

    element = SysExtModelElementAppFactory::newFromClassName(classStr(PCAdaptor));

   

    // Get the new attribute instance that you would like to set

    attributeInstance = SysExtModelAttributeInstanceFactory::newFromValue('NewValue');

   

    // Set for the element the new attribute instance

    SysExtension::replace(element, attributeInstance);

}