Thursday 11 February 2016

X++ in AX7: Static event subscription

In AX2012 we introduced events. Unfortunately, we got the subscription part of static events upside down. In the meta model you were forced to change the publisher when adding a subscriber.


This resulted in an intrusive customization. And thus defeats most of the purpose.




In AX7 this has been corrected. You can now subscribe to events using the SubscribesTo attribute.
Extending the singleton example from my previous post, we can now implement the flush method.




class MyClass
{  
    static MyClass singleton;

    public MyClass getInstance()
    {
        ...
    }

    [subscribesTo(classStr(SysTest), delegateStr(SysTest, postInvokeTearDown)]
    public static void flush()
    {
       singleton = null;
    }
}     




Note: Subscribing to the SysTest.postInvokeTearDown event to flush a singleton is overkill. This event will fire for all tests, and thus slow down every test execution (a little bit). An alternative implementation could be to use dynamic event subscription to only hook up the event as needed. Dynamic event subscriptions work in exactly the same way as in AX2012.

No comments:

Post a Comment