Dynamics 365 for finance & operation/AX 7- Chain of command
In past couple of
releases and updates from Microsoft, Dynamics 365 for finance & operation
aka AX7 came up with a lot more new Extension feature. Current version came
with a lot of hard sealed version and few are soft sealed. Hard seal packages
will not allow a developer to make any changes in the existing code but still
this flexibility is there with the soft sealed packages. But it's best
practices to avoid changes or modifications in the existing piece of code
because you never know the next version will come with all hard sealed
packages.
So there are multiple
extension features that we are already aware of like delegates, events and pre
& post event handlers. As part of update 9, Microsoft has given one more
extension possibility which is Chain of command. Now Pre & Post
functionality can be used in Extensions in more easier way. And it also
provides developers flexibility to work with Protected methods and variables
directly in the Extension class.
Now lets understand how
Chain of commands works with the following example:
Suppose we have a
standard class "TestCOC" in which one method we need some
customization.
Class TestCOC
{
public void run()
{
This.testMethod();
}
protected void testMethod()
{
info(“Inside
main testCOC”);
}
}
Now there is a child
class which extends TestCOC i.e. ChildTestCOC.
class ChildTestCOC extends TestCOC
{
protected void testMethod ()
{
info("child class before super");
super();
info("child class after super");
}
}
Lets say you want to
write some custom code in child class’ testMethod(). How you’ll achieve it
using Chain of command?
First you need to create
an extension class for your child class and have to create same method with
exact signature.
[ExtensionOf(classStr(ChildTestCOC))]
Final class ChildTestCOC_Extension
{
protected void testMethod ()
{
//Insert before actual
method execution
info("Custom logic before super()");
next
testMethod();
//insert logic after actual
method execution
info("Custom logic before super()");
}
}
The method we added here
is the new chain of command definition. We use exactly the same notation as the
original method we are extending and add a mandatory “next” keyword to the call
which will wrap our extension code around the extended method. This next
keyword separates the pre and post parts of our extension and mandatory to be
called inside the chain of command method. Next call cannot be placed in any kind of program block
like if() block or while block. When you call next(), it checks for
other extensions in the queue and runs them in random order, lastly running the
base code.
Not only methods, Chain
of commands also shares the access for protected variables from your base
code/class.
Comments
Post a Comment