AX7:Removing overlayered code from foundation models using inheritance
Updated on: 15/02/2017 10:50 PM IST
We can do most of our customizations using extensions, in same cases overlayering unavoidable. I was working on a code upgrade from AX 2012 to AX7 update 1 (currently D365forOps). I had customizations in foundation models (Application Platform and Application Foundation). After few days i had to upgrade the code base to update2. In update 2 customization in Application Platform model was blocked. I had to refactor my code using extensions.
Using class extensions adding new methods, variables is possible. If we want customize a existing method, we have to go for evnethandlers or inheritance depending on our requirement.
In this post I’ll show how to remove overlayering from classes using inheritance when extensions and eventhandlers approach is not possible.
I have created a class named FoundationClass. I will change one of method’s code without changing the actual code.
class FoundationClass { str name; static FoundationClass construct() { return new FoundationClass(); } void printName() { info(name); } void printModel() { info('Application Foundation'); } }
I have also created a sample runnable class to show the output.
class RunnableClassTest { public static void main(Args _args) { FoundationClass foundationClass = FoundationClass::construct(); foundationClass.printModel(); foundationClass.printName(); } }
In the output, there is a blank Infolog as the “name” variable since it wasn’t initialized anywhere in the foundation class.
Now i will assign some value to name variable and change info of any one method. For this i have created a class SuiteClass which extends FoundationClass. I’ve overridden the printModel() method that was there in FoundationClass in the overridden method i have assigned a value to the “name” variable.
If you notice SuiteClass has the post event handler for construct method of FoundationClass, this post event handler makes sure whenever construct method of FoundationClass is called it returns SuiteClass’s object.
class SuiteClass extends FoundationClass { public void printModel() { name = 'Jake'; info('Application Suite'); } [PostHandlerFor(classStr(FoundationClass), staticMethodStr(FoundationClass, construct))] public static void FoundationClass_Post_construct(XppPrePostArgs args) { args.setReturnValue(new SuiteClass()); } }
Note that name variable isn’t declared anywhere in the SuiteClass. Yet I’m able to assign a value to it, since it was declared in the base class i.e. FoundationClass. It was all possible by inheritance. We can access all the global variables unless their access modifier is private. Same is the case for methods, we can’t override private methods in the derived class and also static methods.
For simplicity i have written the eventhandler in derived class itself, it’s not mandatory you can write it anywhere, you can also have separate event handler class for event handlers.
After making all these changes if i run the same runnable class. I’ll get the below output.
Hope you have understood this post. If you have any questions please write them down in comments.
This was originally posted here.
*This post is locked for comments