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

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 30.06.2016, 12:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
goshoom: Custom code snippets in AX 7
Источник: http://dev.goshoom.net/en/2016/06/custom-code-snippets/
==============

I already explained how to use code snippets and a problem you might run into, but what if you want some additional snippets?

The good news is that you can easily build new snippets by yourself. Let’s use a concrete example.

Let’s say that you often write queries with the query framework and execute them with QueryRun and you’re tired of writing the boilerplate code. A snippet would help.

First of all, create a text file and name it queryRun.snippet. Then fill it with the following content:

</span span class="re0"version/span=span class="st0""1.0"/span span class="re0"encoding/span=span class="st0""utf-8"/spanspan class="re2"?> </span> </span>queryRun</span> </span>Creates a query with a single table, executes in and fetches records.</span> </span> </span>Expansion</span> </span> </span> </span> </span> </span>table</span> </span>Table name</span> </span>MyTable</span> </span> </span>buffer</span> </span>Table variable name</span> </span>myTable</span> </span> </span> QueryBuildDataSource qbds = query.addDataSource(tableNum($table$));QueryRun queryRun;$table$ $buffer$; queryRun = new QueryRun(query);while (queryRun.next()){ $buffer$ = queryRun.get(tableNum($table$));$end$}]]> </span> </span></span>






In short, the header describes the snippet and can provide additional information. Literals define places that can be modified after putting the snippet into code editor, such as the name of the table. The code structure will be the same in all cases, but you’ll likely want to use various tables, therefore the table name is a good candidate for a literal.

The last section, Code, contains the actual source code. Literals are represented by their IDs surrounded by dollar signs (by default).

You can learn more about the syntax in Code Snippets Schema Reference.

When you have your snippet ready, open Visual Studio, go to Tools > Code Snippet Manager, change Language to X++ and click Import.



Select the snippet file and put it into Snippets folder.



Confirm dialogs and look at snippets in code editor – you should see your new queryRun there:



Use the snippet and you should get the following code, where you’ll set the table type and the name of the variable.



Note that the problem with replacements applies here, therefore you have to be careful where you use the snippet. It seems that the while loop inside the snippet triggers code indentation (although I haven’t investigated it in detail), therefore you should use the snippet in some invalid context, such as directly in class declaration or in a method with a compilation error. This workaround won’t be needed in future, after the bug gets fixed.

After typing in just two words (names of the table and the variable), you have a simple query ready to run and you can focus on your actual business logic:



Another note: If you follow this particular example with worker, you’ll notice that when you type worker and press Tab, code completion kicks in and replaces worker with WorkerSessionType. To get the intended result, you have to press Esc instead of Tab.

When you write code, think about what you do often and consider turning it into a snippet, so you don’t have to write it by hand again. You also don’t have to build snippets just for yourself – you can share them with your team or even the whole world.

Editor scripts in MorphX allow any arbitrary code, which clearly isn’t the goal of code snippets in Visual Studio. But it doesn’t mean that you can’t do that – if you need something more complex, such as generating a whole class based on input from a dialog, you can create a Visual Studio extension.



Источник: http://dev.goshoom.net/en/2016/06/custom-code-snippets/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Microsoft Dynamics CRM Team Blog: Debugging custom JavaScript code in CRM using browser developer tools Blog bot Dynamics CRM: Blogs 0 29.11.2015 23:11
goshoom: Custom rules for Code Upgrade Tool Blog bot DAX Blogs 0 15.12.2014 02:11
Axilicious:Hosting custom WPF calendar control in AX 2012 Blog bot DAX Blogs 0 20.05.2013 18:11
emeadaxsupport: AX for Retail 2012: Working with Custom Fields Blog bot DAX Blogs 0 07.08.2012 07:12
sumitsaxfactor: Create Your First Custom Service [AX 2012] Blog bot DAX Blogs 0 19.05.2012 19:14

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

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

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