[X++] Caching of display methods via Attribute

Attributes in Methods are introduced since AX 2012 and a lot of methods use them already.
Regarding Display-Methods there is also a way to cache them via attributes in the method header instead of adding the call to FormDataSource.cacheAddMethod() in the init-Method of the FormDataSource.

Shout out to my collegue here who made me aware of this today!

Old fashioned way

Method:
display LogisticsAddressName dspDlvAddrName()
{
    LogisticsAddressName    dlvName;
    LogisticsPostalAddress  dlvAddress;
    LogisticsLocation       dlvLocation;

    dlvAddress  = LogisticsPostalAddress::findRecId(this.DeliveryPostalAddress);
    dlvLocation = LogisticsLocation::find(dlvAddress.Location);
    dlvName     = dlvLocation.Description;

    return dlvName;
}
init() on FormDataSource:
void init()
{
	super();
    this.cacheAddMethod(tableMethodStr(WMSOrder, dspDlvAddrName));
}

New way with attribute

Method:

Notice the attribute in the header!

[SysClientCacheDataMethodAttribute(true)]
display LogisticsAddressName dspDlvAddrName()
{
    LogisticsAddressName    dlvName;
    LogisticsPostalAddress  dlvAddress;
    LogisticsLocation       dlvLocation;

    dlvAddress  = LogisticsPostalAddress::findRecId(this.DeliveryPostalAddress);
    dlvLocation = LogisticsLocation::find(dlvAddress.Location);
    dlvName     = dlvLocation.Description;

    return dlvName;
}
init() on FormDataSource:

No need to make a change here anymore!

Conclusion

  • +1 for making use of attributes to look into the future
  • Saving code and keeping it out of forms
  • Not having to modify or override the init method on the form.
  • Less locations to customize