Udostępnij za pośrednictwem


The Microsoft Dynamics AX 2012 extension framework – Part 2

Arthur Greef and Michael Gall.

In this blog entry we examine the new extension framework that was shipped with Microsoft Dynamics AX 2012. In part 1 of this series, we explained how the extension framework can be used to instantiate objects from derived classes that are decorated with static class attributes. This design pattern should be used when derived class customizations are located in only one application object layer and if the customization does not change the class attribute specification that is used to annotate a derived class.

If a derived class is customized in more than one application object layer or in more than one application model, or if the class-attribute class specification is customized, then dynamic model element attributes must be used in place of static class attributes. This is because derived class customizations and class attribute customizations are merged at runtime. 

This blog entry describes the model attribute design and shows how these attributes can be dynamically associated with application model elements at runtime. In the next blog entry, we’ll show how these dynamic attributes are used to dynamically extend the Microsoft Dynamics AX application.

There are two parts to this design. The first part describes how application model elements are identified by the extension framework, and the second part of the design describes how to dynamically associate dynamic attributes to these application model elements.

Model elements

The extension framework maintains a cache of model element objects that identify application object tree (AOT) model elements by name. Model elements are stored and retrieved by using a cache key that is composed of the application model element name and the classId of the SysExtModelElement base class or the classId of a class that is derived from the SysExtModelElement base class. Model elements identify run-time class specifications that can be decorated with model attributes. Model elements can either be -defined by a developer or they must reference any type of AOT object that can be identified with an AOT path. Note that all AOT model elements, not only classes, can be decorated with dynamic model elements.

Model attributes

A model element can be assigned one or more model attributes. Model elements themselves can serve as attributes as well. Model attributes have a name, and they have a property map that stores the name and the type of attribute properties. A model attribute instance is an instance of a model attribute with one or more property-value assignments. The extension framework provides a SysExtModelAttributeFactory for creating model attribute instances as shown in the following code example.

static void createAttributeInstance_Factory(Args _args)

{

    SysExtModelAttributeInstance attributeInstance;

   

    // Create an instance of a model attribute named‘String’ that

    // comprises one property named ‘String’that is assigned the value ‘A’.

 

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

}

 

The following code example shows how custom attribute instances can be created. This example is for illustration only. The extension framework provides model attribute, property map, property-value map, and model attribute instance factory classes that should be used instead.

 

static void createAttributeInstance_Custom(Args _args)

{

    SysExtModelAttribute attribute;

    SysExtModelAttributeName attributeName;

    SysExtModelPropertyName propertyName1, propertyName2;

    SysExtModelAttributeInstance attributeInstance;

    Map propertyMap;

    Map propertyValueMap;

   

    // Set the name of the attribute

    attributeName = 'MyAttribute';

    propertyName1 = 'MyProperty1';

    propertyName2 = 'MyProperty2';

     

    // Create property map

    propertyMap = new Map(Types::String, Types::Enum);   

    propertyMap.insert(propertyName1, Types::String);   

    propertyMap.insert(propertyName2, Types::Integer);

   

    attribute = SysExtModelAttribute::newFromParameters(attributeName, propertyMap);

   

    // Create property map

    propertyValueMap = new Map(Types::String, Types::Container);

    propertyValueMap.insert(propertyName1, 'PropertyValue1');

    propertyValueMap.insert(propertyName2, 2);

     

    attributeInstance = SysExtModelAttributeInstance::newFromParameters(attribute, propertyValueMap);

}

Model element extensions

A dynamic and run-time model element extension is specified by binding a model element to one or more model attribute instances. Model element extensions are stored in the SysGlobalObjectCache.

The next blog entry (part 3) will demonstrate how the extension framework uses these model element extension objects for various dynamic and run-time extension scenarios.