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

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 21.05.2018, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
erconsult: Internal iterator design pattern in X++
Источник: http://erconsult.eu/blog/internal-it...ern-in-d365fo/
==============

A couple of times in my carrier I stumbled upon a development requirement to perform a set of loosely connected actions with a similar interface upon a certain business object depending on entry conditions:
  • Sometimes do this
  • Sometimes do that
  • Sometimes do this and that
  • In future, you may do something else in addition.
In Dynamics 365 for Finance and Operations, this usually results in a hierarchy of classes. The classes are typically bound to some parameter table containing checkboxes or – better – an enumeration field. This enumeration directs a SysExtension class factory which classes to instantiate.
A simple while_select loop traverses the parameter table and executes the instantiated objects one by one. However, if the classes implement more than one action i.e. method, this external loop may become repetitive. I ended up with a certain design pattern which turned out to have a name in object-oriented programming: an internal iterator.
The constructor instantiates an object and gives it the previous one as a parameter, thus forming a linked chain of objects. The external actor only needs to take one parameter – the topmost object in the chain – and to call one operation .doThis() upon it. This .doThis() method is implemented in the parent class in the hierarchy, and the parent class knows how to traverse the objects.

Adding a new grid with a parameter table into an existing form is more difficult than adding new field(s) into an existing table via extensions. If the number of possible actions is countable and small, you may think of de-normalizing the parameter table and making an array field with possible options. Instead of iterating records, the constrictor will be iterating fields in one record.

Below is an application of this pattern to a simple task: calculation of the total nutrition value of one meal, while the meal may consist of an Entrée, and Entrée and a Main course, a Main course and a Dessert and so on:

class MealCourseAtrribute extends SysAttribute implements SysExtensionIAttribute
{
MealCourseEnum mealCourseEnum;
public void new(MealCourseEnum _mealCourseEnum)
{
super();
mealCourseEnum = _mealCourseEnum;
}
public str parmCacheKey()
{
return classStr(MealCourseAtrribute) + enum2Value(mealCourseEnum);
}
public boolean useSingleton()
{
return true;
}
}


abstract public class MealCourse
{
private MealCourseEnum mealCourseType;
private MealCourse prevMealCourse;
abstract protected MealKcal kcal()
{}
final public MealKcal kcalTotal()
{
MealKcal ret = this.kcal();
if (prevMealCourse)
{
ret += prevMealCourse.kcalTotal();
}
return ret;
}
private MealCourse prevMealCourse(MealCourse _prevMealCourse = prevMealCourse)
{
prevMealCourse = _prevMealCourse;
return prevMealCourse;
}
protected void new()
{
}
public static MealCourse construct(Meal _meal)
{
MealCourse mealCourse, prevMealCourse;
MealCourseAtrribute attr;
MealCourseEnum mealCourseEnum;
int i;
for (i = 1; i
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
Старый 21.05.2018, 23:10   #2  
belugin is offline
belugin
Участник
Аватар для belugin
Сотрудники Microsoft Dynamics
Лучший по профессии 2017
Лучший по профессии 2015
Лучший по профессии 2014
Лучший по профессии 2011
Лучший по профессии 2009
 
4,622 / 2922 (107) +++++++++
Регистрация: 16.01.2004
Записей в блоге: 5
С моей точки зрения больше похоже на chain of responsibility.

Но пример, мне кажется, не очень удачный - зачем блюду знать про предыдущее блюдо. То есть это не для этого случая.
За это сообщение автора поблагодарили: EVGL (5).
Старый 21.05.2018, 23:27   #3  
EVGL is offline
EVGL
Banned
Соотечественники
Лучший по профессии 2017
Лучший по профессии 2015
Лучший по профессии 2014
 
4,445 / 3001 (0) ++++++++++
Регистрация: 09.07.2002
Адрес: Parndorf, AT
Благодарю! На самом деле, похоже на помесь "итератора" с "декоратором". Хотелось передавать функцию извне, что типично для "внутреннего итератора", но обнаружил, что на практике проще реализовать, поделив на две функции: одна возвращает значение, вторая реализует операцию: сложить, умножить, выполнить логическое И.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
erconsult: Configuring Austrian and Norwegian per diems in Dynamics 365 Blog bot DAX Blogs 0 03.05.2018 04:28
Navigate Into Success: Module Binder Pattern proposal Blog bot NAV: Blogs 0 15.10.2016 01:36
Navigate Into Success: Gentlemen’s agreement pattern, or handling the “Handled” pattern Blog bot NAV: Blogs 0 04.10.2016 12:11
wiki.dynamicsbook: Changes Made in Navision Attain 3.60 Blog bot Dynamics CRM: Blogs 0 02.09.2008 13:23
wiki.dynamicsbook: Changes Made in Navision Attain 3.01B Blog bot Dynamics CRM: Blogs 0 21.08.2008 20:43
Опции темы Поиск в этой теме
Поиск в этой теме:

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

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

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

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