Pages

  • Home
  • D365 Impementation
  • AX 2012
  • Dynamics CRM
  • Azure
  • ...

Tuesday, March 5, 2019

Microsoft Dynamics 365


D365 Extension Approaches:

D365 Extension Approaches:


Add methods or customized existing functionality through extension
·       Delegates.
·       Chain of commands.
·       Event handlers.


Delegates:
Delegates can only be created for Classes. These are generally used to customize existing method logic.
The implementation is shown below
    public void printLine()
    {
        EventHandlerResult res;
        info("Str");
        this.printLine_deligate("Str", res);
    }

    /// <summary>
    ///
    /// </summary>
    delegate void printLine_deligate(str _info, EventHandlerResult _res)
    {
    }

In the above code, For printLine() method we have created delegate printLine_deligate()   and this method will not contain any logic.
The next step is to create the event handler for this delegate which subscribes to it as shown below
    ///
    /// </summary>
    /// <param name="_info"></param>
    /// <param name="_res"></param>
    [SubscribesTo(classStr(Test), delegateStr(Test, printLine_deligate))]
    public static void Test_printLine_deligate(str _info, EventHandlerResult _res)
    {
    }
To get the above code, we can just right click the delegate and Click copy event handler and then paste in a separate class which will works as event handler class.

COC (Chain of commands)
Delegates will not be a good option if we are upgrading to update 8 or higher as we need to use chain of commands framework.

The comparison between delegates and chain of commands is shown below
Chain Of commands

Consider below class
class Extensibilty_Demo
{
    CustTable cust;

    public Name custName()
    {
        select firstonly cust;

        return cust.name();
    }

}
Suppose we want extend the feature custName() method through chain of commands
1)     Create an extension class for the above class as shown below
[ExtensionOf(ClassStr(Extensibilty_Demo))]
final class Demo_Extension
{
    public Name custName()
    {
        Name newName = next custName();       

        newName = cust.name() + " hello";

        return newName;
    }

}
2)     ExtensionOf keyword is must for the extension class and the name should have suffix as _Extension as shown in above example.
3)     The extension class must be “final”.
4)     Method name must have the same signature as the parent method.
5)     Next keyword must be used to call the parent method.
6)     As we can see the original method custName() prints only the customer name, but the method in extension class prints customer name + Hello.
Advantages of Chain of commands
1)     Global variables of the class, form or any object can be accessed in the extension class which was not possible through delegates.
2)     We can create new methods in table which was not possible earlier.
3)     We can also access global variables declared in form which was not possible earlier.
4)     Code readability is good compared to delegates.

Event handlers (pre-post event handlers)
 Using event handlers, you can add or write business logic in the different class by passing arguments. You have pre or post event handlers for writing extension logic example: During insertion of Invent table we having two event handlers ’Oninserting , ’Oninserted’ implemented below logic as an example.

class InventTableMy_EventHandler
{
    [DataEventHandler(tableStr(InventTable), DataEventType::Inserting)]
    public static void InventTable_onInserting(Common sender, DataEventArgs e)
    {
        InventTable inventTable = sender as InventTable;
        // Call the method as if it was defined directly on InventTable.
        inventTable.myDefaultInventLocationId();
    }
}
Example: 2 We have requirement to write business logic one’s form button got clicked. Using Click event on the form we write business logic .



1 comment:

  1. what is fantastic post? this is so chock full of useful information I cannot wait to dig deep and start utilizing the resource give me.your exuberance is refreshing.
    Portal Development

    ReplyDelete