Chain of Command is the term that describes how we customize, or extend, base Microsoft code in Microsoft Dynamics 365. You can now wrap logic around methods that are defined in the base class that you’re augmenting. You can extend the logic of public and protected methods without having to use event handlers. Microsoft’s base objects and code cannot be changed directly in D365. However, we are able to make changes to separate objects and classes that are then combined with the existing base object to form the final version.
When To Use Chain of Command
In order to modify a base Microsoft object in Dynamics 365, we need to use the tools that Microsoft provides to ‘extend’ the base objects. Table and form AOT nodes can be extended using additional Extension nodes in the AOT. However, if you want to extend x++ code, you need to use either Event Handlers or Chain of Command.
While Event Handlers still have their place and are supported. As a developer, I personally find that using Chain of Command is easier to write and read.
examples
For example, a model contains the following code.
class BusinessLogic1
{
str doSomething(int arg)
{
// ...
}
}
You can now augment the functionality of the doSomething method inside an extension class by reusing the same method name. An extension class must belong to a package that references the model where the augmented class is defined.
[ExtensionOf(classStr(BusinessLogic1))]
final class BusinessLogic1_Extension
{
str doSomething(int arg)
{
// Part 1
var s = next doSomething(arg + 4);
// Part 2
return s;
}
}
In this example, the wrapper around doSomething and the required use of the next keyword create a Chain of Command (CoC) for the method. CoC is a design pattern where a request is handled by a series of receivers. The pattern supports loose coupling of the sender and the receivers.
We now run the following code.
BusinessLogic1 object = new BusinessLogic1();
info(object.doSomething(33));
When this code is run, the system finds any method that wraps the doSomething method. The system randomly runs one of these methods, such as the doSomething method of the BusinessLogic1_Extension class. When the call to the next doSomething method occurs, the system randomly picks another method in the CoC. If no more wrapped methods exist, the system calls the original implementation.
Objects That Can Use Chain Of Command
There are several different objects in Microsoft Dynamics 365 that can use Chain Of Command.
- Classes
- Tables
- Forms
- Datasource on a form.
- Data field on a form.
- Control of a form.
- Data entities
Summary
We learned about Chain Of Command (CoC) is and how it allows a developer to add custom code without changing the base Microsoft code. You have learned how to write a basic class. In the next article, I will show you more detailed examples of how to create Chain Of Command classes for each base object type.