AX 7. Breaking dependencies. Part 2.

In the first part we looked how to use SysPluginFactory, today we will talk about event delegates.

Delegates were introduced in AX 2012 but were rare used by standard code and partners.

In AX 7 delegates obtained second life.  They are widely used by MS to create customization points for customers and partners. As we can see number of them is constantly growing with each update release.

Another use of delegates is a situation when you want to access a code from another model without referencing it, for example accessing the code in a higher model from a lower model.

Create new delegate:

delegate void validateAmountDelegate(Amount _amount, EventHandlerResult _res)
{
}

Delegate does not have return value and method body.

Subscribe to:

[SubscribesTo(classStr(MyClass), delegatestr(MyClass, validateAmountDelegate))]
public static void validateAmount(Amount _amount, EventHandlerResult _res)
{
    if (amount > 0)
    {
        _res.result(true);
    }
    else
    {
        _res.result(false);
    }
}

Method signature should be the same for subscriber and delegate.

Invoke delegate:

EventHandlerResult result = new EventHandlerResult();

this.validateAmountDelegate(amount, result);

boolean res = result.result();

 

To read about usage scenarios and find out more details please refer to AX wiki
https://ax.help.dynamics.com/en/wiki/delegates-for-migration/

Advertisements