21.04.2017, 03:30 | #1 |
Участник
|
stoneridgesoftware: Delegates in Dynamics 365 for Operations
Источник: https://stoneridgesoftware.com/deleg...or-operations/
============== Dynamics 365 for Operations is well on its way to making over-layering of code a thing of the past. The application is moving towards and extension model approach. This approach should allow for a faster reaction to provide and push hotfixes and continuous upgrades. One approach Dynamics 365 for Operations is taking to make the application more extensible is the utilization of delegates. By adding these delegates, they are adding the ability for customers and/or partners to add customizations that can be stored in their own package. A few things to know about x++ Delegates: • Cannot have modifier. It is always protected. • Return type must be “Void” • Defining method cannot contain code • Subscribing methods must have the same signature as delegate the delegate definition. Below are a couple delegate examples for Dynamics 365 for Operations. Create delegate definition The following code shows an example of a delegate definition: delegate void defineDelegate() { } Create delegate handler Open the class in the visual studio designer right click on the delegate definition and click “Copy event handler method” from the menu. The will automatically create the handler method for the delegate. The following code shows example of a handler method. [SubscribesTo(classStr(DelegateExample), delegateStr(DelegateExample, defineDelegate))] public static void DelegateExample_defineDelegate() { } Full code example Below I have provided a set of classes that show examples of different ways you can use delegates. There are three classes: 1. DelegateExamples – Contains delegate definitions 2. DelegateExampleSubscriptions – Contains handler methods 3. DelegateExampleRun – Job that will run and product the output DelegateExamples /// /// Example of delegates. Dynamics for Opertions X++ /// class DelegateExamples { /// /// Delegate definition. /// /// delegate void addTwoNumbersDelegate(real a, real b) { } /// /// Delegate definition that provides a way to return a result from the delegate handler. /// /// delegate void addTwoNumbersReturnResultDelegate(real a, real b, EventHandlerResult result ) { } /// /// Call to the addTwoNumbersDelegate delegate, since actual call is protected. /// /// /// public void triggerAddTwoNumbersDelegate(real a, real b) { this.addTwoNumbersDelegate(a,b); } /// /// Uses delegate in a method which will return a result from the delegates handler. /// /// /// public real returnDelegateAdditionResults(real a, real b) { EventHandlerResult result = new EventHandlerResult(); real total = 0; this.addTwoNumbersReturnResultDelegate(a,b,result); total = result.result(); return total; } } DelegateExampleSubcriptions /// /// Contains the delegate handler examples to the delegates created in the DelegateExample class. /// Dynamics for Operations X++ /// class DelegateExampleSubscriptions { /// /// Delegate handler. This is where to add your business logic to do something. /// For example insert/update values in a table. /// /// /// [SubscribesTo(classStr(DelegateExamples), delegateStr(DelegateExamples, addTwoNumbersDelegate))] public static void DelegateExample_addTwoNumbersDelegate(real a, real b) { real total = 0; total = a + b; info(strFmt("Invoking delegate addTwoNumbersDelegate: total %1", total)); } /// /// Delegate handler. This will allow you to return a result to the method that /// triggered the delegate. /// /// /// /// [SubscribesTo(classStr(DelegateExamples), delegateStr(DelegateExamples, addTwoNumbersReturnResultDelegate))] public static void DelegateExample_addTwoNumbersReturnResultDelegate(real a, real b, EventHandlerResult result) { real total = 0; total = a + b; result.result(total); } } DelegateExampleRun /// /// Class with main method /// class DelegateExampleRun { /// /// Runs the class with the specified arguments. /// /// The specified arguments. public static void main(Args _args) { DelegateExamples dExample = new DelegateExamples(); real totalResult = 0; dExample.triggerAddTwoNumbersDelegate(5,3); totalResult = dExample.returnDelegateAdditionResults(1.5,3.5); info(strFmt("Invoking delegate returnDelegateAdditionResults: total %1", totalResult)); } } Output To Run: • Copy all three classes into visual studio. • Compile • Right click on the DelegateExampleRun class and set as start up object as shown below. • Then click start in visual studio to run Источник: https://stoneridgesoftware.com/deleg...or-operations/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
|
|