AX 2012 supports events/delegates which wasn’t supported in previous versions of AX. Events have provided in AX a way, to customize the existing functionality of lower layer code without changing the existing code. A developer, when developing a functionality which is likely to be customized by other developers in implementations, can make use of delegates and fire a delegate instead of method directly(We will discuss about delegates in details later in this article.) The customization developer can hook up his events to that delegate. This will help saving customization cost of existing functionality and code remains clean.
Delegates behave like methods but they are different in a way that they don’t have definition. Delegates only have ‘void’ return type. They just have name and parameters. Depending upon the scope of that delegate, it can either be static or non-static.
delegate void testDelegate()
Event handlers are the methods which are called when a call is given to delegate. Methods must be hooked to a delegate first in order to use it as an event handler. Event handler method must also be public.
public void testEventHandler() //This is an ordinary method which will serve as event handler
info(“Event handler called”);
Event hook up
Event handler is hooked up with delegate using ‘+=’ operator and ‘eventhandler’ keyword.
public void test()
this.testDelegate += eventhandler(this.testEventHandler);// Hooking up event
this.testDelegate();// Firing event
Static event handlers
Event handlers can be static. They can either be hooked up same as defined above or directly from AOT. In the class->delegate node in AOT, right click->New event handler subscription. A new eventhandler node will be created. In the properties window, class and method will need to be specified.
Pre- and Post-event handler for methods
There are automatic events for methods. One is called before executing that designated method and one is called after finishing the designated method. The method and the pre- or post-event handlers must be static. On a method where pre- or post-event handler is to be added, right-click and new event handler subscription(this is same as we did for delegate). This will add a node in AOT under the method. In the properties of that newly added node, set the class and the method and the ‘called when’ property to specify whether this is a ‘pre-event handler’ or a ‘post-event handler’.
In a pre-event handler, parameters of that method can be changed and in a post-event handler, return value can be changed.