Microsoft Dynamics AX 2012:Simple Use of Eventing Framework:Part 1:Use of Coded Events

Hi,All

First Lets start with the introduction of  why this framework has been implemented in AX 2012.

Why need

After the release of Microsoft Dynamics customers that were using AX required sort of customization to full fill there business needs.So in order to do that,what they did before is to customize the source code (that shipped with the product) on the above layer let say “CUS” layer ,which caused lots of customer cost and also  invest significant time in browsing and understanding the source code that is shipped by Microsoft.But the real issue was,on a certain point of time when Microsoft some how changed there source code than the customization on the ‘CUS’ layer would be affected/broken as well,which in turn causing lots of time and pain consuming task also fixing earlier customizations of the next version upgrade can be expensive for partners as well.

Now with the introduction of eventing  framework in AX 2012.It reduces  time to time change/fixing of customization of the code by segregating the code with the actual source code.So when at certain point of  time if the source code is changed by Microsoft guys, the customized code for partners or ISVs wont be effected much.When the customized functionality is tied to an event, then the source code can then be rewritten and have less  impact on the customization, as long as the same events are raised in the same sequence from one version to the next.

I am assuming that all you guys are well know about what are events,event handlers and delegates.If still not  then you can first understand this.You guys can google it for you own ease.

Now I am assuming that guys understand the basic of eventing.Now some terminologies that are used in eventing framework are.

  • Event : Something happen or some change has happened (like sms prompt,phone ringing….)
  • Produce : It is a code or logic(can be class or something) that initiates the event.
  • Consumer : It is a code or logic that receives the event
  • Delegate : Delegate is the function pointers(event handlers).It is the definition of the information passed from producer to consumer when an event take place.
  • Event Payload : It is the actual information passed b/w produce and consumer.

The more i will be digging in to the eventing framework,the  more you guys will get the idea of terminologies.

So lets start our part 1

Coded Events

In this event we can do following things:

  • Create a delegate ,Can assign delegate to a class
  • Create Event handlers
  • Subscribe/Attach these event handler to the delegate.
  • We can assign the event handlers to the delegate using AOT property or using  ‘+=’ operator and eventHandler keyword

A delegate is identified by a ‘delegate’ keyword.It should not have any return type and should not have any code in the body but can have input parameters.like

delegate void Insert(str name, UtcDateTime startingDate) {}

Now, i have created a class ‘AsimDelegateAutoClass’

AutoClass

right click the class and New and select delegate and gave it name ‘firstDelegate’

Delegate

Below is the delegate with the keyword delegate, returning nothing and have no body code but taking two input parameters,which are our event payload that will be transfer to our eventhandler.

firstDelegate

Now as i told you before,delegate are function pointers(event handlers) so now we need to associate eventHandlers(which i have not created yet ) to the delegate.

Create a class called ‘AsimDelegateEventHandler’

class AsimDelegateEventHandler
{
}

Created a method which is  actually an eventHandler

eventHandlerCode

Note above,that this event Handler/Method has the same signature as that of delegate which is necessary.Also we need to keep in mind that eventhandlers should be static and public as well.This event handler will sum up the two values and show infolog

After creating an event Handler , i need to attach/subscribe this to the delegate i have created before.so here it is

Right click delegate and click on ‘New Event Handler Subscription’ say it ‘EventHandler1’

SubscribeEventhandler

Click on the ‘EventHandler1’ and right click it for properties

EventHandlerProperties

What i did above is gave it a name called EventHandler1.Now the actual associating part is this

On class property value  i gave the name of the class which is ‘AsimDelegateEventHandler’ in my case and selects its static method which is ‘addEventHandler’.Also note a property

‘EventHandlerType = X++’   which is a default value and i leave it as it is.

So now i have associate the event handler to the delegate.

Subscription

Finally to run.I have created a Main method in ‘AsimDelegateAutoClass’ , Initialize the class instance and call the delegate via instance and finally passed to parameters values as an event payload .

Main

So now on running this class we would have the result like.

infolog

 

Now there is another way to associate the event Handler to the delegate which is using ‘+=’ operator.

I have created a class called ‘AsimDelegateCodeClass’ and create a delegate ‘delegate1’ for the class (same way as it did above) and a main method.

CodeClass

Here is the delegate.

delegate1

 

Main method.

EventHandlePlusOperator

Note the above highlighted line.Here is the code which adds the event Handler to the delegate.On running this class the result would be like this.

Infolog2

 

So that’s all for the today.Please let me know for any queries.

Advertisements

15 responses to “Microsoft Dynamics AX 2012:Simple Use of Eventing Framework:Part 1:Use of Coded Events

  1. Pingback: Microsoft Dynamics AX 2012:Simple Use of Eventing Framework:Part 2:Use of Automatic Events « amazingAX·

    • Thanks, One of the scenario that i have worked on was, there was a requirement of complex customization of number Sequence for all the procurement documents.
      On which i create post event Handler and updated the automatice generated numbersequence value.

  2. What if i want to call my event handler method from any standard class method?

    is there any example available in standard?

    • For this, you need to explicitly call the event method via delegate, becuase at times there is a situation where we want to call the event in the middle of the code ; instead of pre/post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s