AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 15.11.2017, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,448 / 846 (79) +++++++
Регистрация: 28.10.2006
sertandev: AX7 (D365) Chain of command with example
Источник: https://sertandev.wordpress.com/2017...-with-example/
==============

As of platform update 9 of Dynamics AX for Operations, we have a new extension possibility called chain of command. Now we are able to add pre and post functionality to extensible methods in a much easier and readable manner than the previously used event handlers, also we are now able to access protected methods and variables directly in the extended class without problems.

Now let’s check how it works with an example. Create two classes, one is the base class of the other and add a method we want to extend called ‘testMe’. Add infolog calls inside the method to track the execution :

class COCBaseClass{void new(){}protected void testMe(){info("base class call");}public void runTest(){ this.testMe();}}class COCChildClass extends COCBaseClassclass{protected void testMe(){ info("child class before super"); super(); info("child class after super");}}Create a new model, add reference for the class model we created above, and add an extension class for our child class.

[ExtensionOf(classStr(COCChildClass))]final class COCChildClassCOCExt_Extension{protected void testMe(){ info("Extension 1 before next call"); next testMe() info("Extension 1 after next call");}}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.

Sounds complicated enough? Let’s run our example by calling runTest method and see the outcome (Note : All tests in this blog are executed in Update11 version) :



As you see, it runs nearly the same as pre and post handlers, pre being before the next call and post after it. The advantage of chain of command is you can share the same method variables in the pre and post (before/after next() call) or share the same tts block inside your COC method. COC also supports return value and parameter modification of the extended method in a much more readable manner. Extending a method with return value and parameters in COC is like below :

class COCBaseClass{protected int returnMe(int _me){return _me;}}class COCChildClass extends COCBaseClass{protected int returnMe(int _me){return super(_me);}}[ExtensionOf(classStr(COCChildClass))]final class COCChildClassCOCExt_Extension{protected int returnMe(int _me){ int ret; ret = next returnMe(_me); return ret;}}Now let’s examine some more complex scenarios. Let’s create some other models and add some more chain of command extensions to our testMe method. Here are our two new extension classes in two new models :

[ExtensionOf(classStr(COCChildClass))]final class COCChildClassCOCExt2_Extension{protected void testMe(){ info("Extension 2 before next call"); next testMe() info("Extension 2 after next call");}}[ExtensionOf(classStr(COCChildClass))]final class COCChildClassCOCExt3_Extension{protected void testMe(){ info("Extension 3 before next call"); next testMe() info("Extension 3 after next call");}}When we run our code this time, it creates the following result, calling all before next () parts of chain of command extension methods, calling the base method, then calling code after the next() calls. :



Realize that, as described by Microsoft, the chain of command extension methods are called in random order, as 3,1 and 2. And the base method is always called in between the next calls. However the code blocks after next() calls are always executed in a mirrored order of the preceeding calls, as 2,1 and 3.

Now let’s add some pre and post eventhandlers and call them together with our chain of command methods to test how they relate to each other. I will add the eventhandlers to our new extension classes. Remember to make our protected ‘testMe’ method hookable by adding [HookableAttribute(true)] on top of the method before adding your eventhandlers:

[ExtensionOf(classStr(COCChildClass))]final class COCChildClassCOCExt2_Extension{[PreHandlerFor(classStr(COCChildClass), methodStr(COCChildClass, testMe))]public static void COCChildClass_Pre_testMe(XppPrePostArgs args){info("Extension 2 pre-eventhandler");}[PostHandlerFor(classStr(COCChildClass), methodStr(COCChildClass, testMe))]public static void COCChildClass_Post_testMe(XppPrePostArgs args){info("Extension 2 post-eventhandler");}}Repeat the same for extension class 3, and run the test again to see the result :



As you see the pre and post eventhandlers run after and before the chain of command calls. So chain of command wraps the base method, and its pre and post eventhandlers all together. Just as chain of command, the eventhandler methods are also called in a random order, and not mirrored on pre and post like the chain of command calls.

You can also change the return value and the parameters of the base method call using chain of command. However you should be really careful with the fact that the execution order is random and the return value set by the latest COC post call is returned on the chain. Now lets change our ‘returnMe’ method to echo the parameter on infolog and add the following chain of command methods to our 3 extension classes for returnMe method manipulating the parameter and return value before and after next call :

protected int returnMe(int _me){info(strfmt("Base class value : %1", _me));return _me;}protected int returnMe(int _me){ int ret;_me = 1;ret = next returnMe(_me);ret = 11;return ret;}Then we test the code by running it with a parameter of ’99’ and displaying the return value in the runnable class:

public static void main(Args _args){COCChildClass test = new COCChildClass();//test.runTest();info(int2str(test.runReturnMe(99)));}

The parameter has changed to 1 and the return value exited with the extension on class2.

Hope you enjoyed reading. You can download the test projects of the blog from the link below:

https://github.com/sertanyaman/Serta...er/COCTest.zip
















Источник: https://sertandev.wordpress.com/2017...-with-example/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
Старый 15.11.2017, 19:38   #2  
mazzy is offline
mazzy
Участник
Аватар для mazzy
Лучший по профессии 2015
Лучший по профессии 2014
Лучший по профессии AXAWARD 2013
Лучший по профессии 2011
Лучший по профессии 2009
 
29,472 / 4494 (208) ++++++++++
Регистрация: 29.11.2001
Адрес: Москва
Записей в блоге: 10
Цитата:
Сообщение от Blog bot Посмотреть сообщение
However the code blocks after next() calls are always executed in a mirrored order of the preceeding calls, as 2,1 and 3.

...

Just as chain of command, the eventhandler methods are also called in a random order, and not mirrored on pre and post like the chain of command calls.
как это мило.
http://coub.com/view/8lv40
__________________
полезное на axForum, github, vk, coub.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Вот мы и нашли этих 43 AX7 клиентов? Или это не AX7/D365? ax_mct Microsoft и системы Microsoft Dynamics 259 06.04.2022 08:56
sertandev: Using 3rd party Web components in AX7; an introduction to extensible controls Blog bot DAX Blogs 0 28.08.2017 19:11
sertandev: AX7 Extensibility – Part 3 : Event handlers and delegates (hooks) Blog bot DAX Blogs 0 28.08.2017 19:11
sertandev: AX7 Extensibility Overview – Part 2 : Code extensions Blog bot DAX Blogs 0 28.08.2017 19:11
patrickmouwen: AX7 – My top 12 highlights Blog bot DAX Blogs 0 27.02.2016 13:11
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 09:35.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.