Wednesday, December 19, 2012

MS Dynamics AX : Extension Framework


Description

The basic idea of extension framework is that Frameworks, and their Foundation components  must adhere to the Open-Closed principle. This principle states that “software entities should be open to extension, but closed to modification”  this means that extending framework classes should not require changes to the any framework artifacts (classes, tables, Enums,).
There implementation of extension consist of two main steps.

• Designing for extension.
• Design/implement extension classes.




Designing for extension

Designing for extension consist of following steps, 
• Step 1 – Create extension base and extension list classes
• Step 2 – Define delegates
• Step3 – Define initialize method and call from static constructor
• Step 4 – Instantiate and populate the extension list
• Step 5 – Raise the event

Step 1 – Create extension base and extension list classes

Create an abstract base class for extensions. This class will need to provide methods for initialization, event subscription, and accessing the instance of the class being extended. The initialize method of this class will call the subscribe method which must be implemented by the derived extension classes. This provides the mechanism for runtime event subscription and avoids requiring extensions to modify framework artifacts in the AOT.




Create a strongly typed list class. The list is populated at runtime with the current extensions.

Step 2 – Define delegates

Define delegates for extension points. As a general rule pre and post event delegates should be created.
Right click on the class being extended in the AOT. Select New > Delegate. Follow naming guidelines when specifying the delegate name.
  
Below are examples from the SubledgerJournalizer class of delegate signatures for journalizing and journalized event handlers.

 

Step3 – Define initialize method and call from static constructor

An initialize method can be used to instantiate the extension list object and to initialize the state of the object instance. This method should be called from the static constructor of the class that will be extended. In this example the SubledgerJournalizer.

 


Step 4 – Instantiate and populate the extension list

In the static constructor for the extension list class you can use the extension factory to find all the classes that extend your base extension class, instantiate each extension, initialize each extension, and add to the extension list. The below example uses the SourceDocumentExtensionFactory. A more generic extension factory, SysExtensionFactory, is available and should be used instead.

 

Step 5 – Raise the event

Last but not least the events must be raised at runtime. In this case the SubledgerJournalizer.journalize() method does this be calling the journalizing (pre) and journalized (post) event handlers. This will result in each registered subscriber’s event handler method being called.

 


Design/Implement extension classes

• Step 1 –  Implement concrete extension
• Step 2 –Implement event handler
• Step 3 – Implement subscribe() method in derived extension class(es)

Step 1 – Implement concrete extension


Each extension needs to implement a concrete class that derived from the abstract extension base class.
 

Step 2 – Implement event handler

All classes that derive from the abstract extension class will need to implement event handlers. The signature of the event handler must match that of the delegate.


Step 3 – Implement subscribe() method in derived extension class(es)


All classes that derive from the abstract extension class will need to provide an implementation for the subscribe method. This will be called at runtime when the extension is initialized. The implementation of this method will subscribe to the event is being raised by the class being extended.
 


No comments:

Post a Comment