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.

 

THIS POST APPLIES TO MICROSOFT DYNAMICS AX7 PREVIEW; IS PROVIDED AS-IS AND CONFERS NO RIGHTS.

Comments (6)

  1. Roland Steber says:

    Hi msmfp,

    Do I need to recompile the assembly (package) providing the event publisher in order to get the event subscription work in my own assembly?

    Thanks, Roland

    1. Hi Roland,
      No need to compile the code you are subscribing to - that would defeat the purpose there.
      Thanks,
      Michael

  2. Hans says:

    Hi Msmfp,

    Is there also a simple way to check which handlers subscribed to a an event using this attribute?

    Thanks, Hans

    1. Hi Hans,
      You can use "Find references" on the delegate to find all subscribers.
      Thanks,
      Michael

  3. Hi Vasily,

    The += construct is also supported - even in AX2012.

    The attribute based subscriptions solves a hen-and-egg problem.  If you want to make a truly unintrusive customization you cannot change any existing logic. That means you have no place to do the first dynamic event subscribtion using += notion.

    The subscribesTo attribute offers an elegant way of solving that problem.

    I hope this explains the value - it is one of my favorite new features.

    Michael

  4. Vasily says:

    But why, WHY??? not to use standard += construction from C#? For what sake is there attribute used for?

Skip to main content