This week, I wanted to take sometime and actually dive into Microsoft Dynamics AX 2012
ability, within X++ to make use of Eventing!
Now I wrote about this, as an introduction to the idea, at the start of this year, here: Microsoft Dynamics AX 2012 and Eventing
This gave a good overview of the idea of eventing with Dynamics AX 2012, and gave some of the examples with Pre, Post and Custom Raised Events in mid stream of code.
With that in mind, and for reference, lets take this now a step further, and look at some real X++ code, objects, elements, make up, to help sink this home.
For my example, I'm actually doing straight X++, with no relation to any real world application yet. The idea is to understand what the mechanics are, and then from there apply those in design and in development as needed.
You will notice, in the above project, I have a few classes, called Apples & ApplePicker. I figured, I would make this a little bit more fun, than the standard hello world!
So lets take some time, to examine the Apples class within my project.
Here we see the class declaration, in which I have two variables I'm making use of. A Set, called appleBin and an int called apple. This is setting up, for later use, within the business logic of my Apples class.
Next we move forward to the New method of the Apples class.
With it, we see that I'm setting the variables that we saw in the class declaration, to initialize these variables, for the Set and int types.
Moving forward from there, we go to the next method, called growApple(). This is where some of the fun starts.
Here we see I'm taking the init apple, and adding one to it with the ++;
call. Next we move to actually adding an "apple" to our appleBin set, with the int of apple as part of the key.
Moving from there, we see a final, what looks to be a method call, that is the following.:"this.appleGrew(apple);"
In looking at this, it would seem as if this was a normal method, and it kinda of behaves as such, however this is not a method, but a delegate I created. We can get a peak, inside the delegate I created, for the Apples class, with the following screen shot.
Notice, that this blank actually, and not doing anything. Its truly a delegate, and looking at this, within the project, we will see that it appears differently, that the rest of the methods within the class.
You can create delegates within the AOT space of a given element, as we see on the Apples class in the above image. You do this, as if you were creating a new method.
What you should also notice, is the fact that below it, we have an event handler subscriber that we have created in the same fashion. These can be created within code as well, which for now we will shelve and come back to in a later post.
Diving deeper into the event handler subscription element, we can see that it has a property page, that allows us to take and set the class, type of, being X++ or Managed, and then the method to call.
Based on this setup for the event handler subscription element, we can then see it's calling a static method, called pickApple
from the ApplePicker class.
Now it's very important to point out, that all event handler subscription elements, must be static methods. These are the only types allowed, for create such an event handler subscriber, or event listener.
So, what this is doing then, is when the Apples.growApple() is called, within it, via X++ code, we call to a delegate, of Apples.appledGrew(). This delegate, has a event handler subscription setup, for the ApplePicker.pickApple() method.
Lets now take a look inside that static method, which represents our event subscriber, called pickApple().
As you can see from the method, we are taking a parameter of int appleId, which happens to be the exact
method signature of the delegate that is firing this event handler. This method, simply takes and calls to the info log, claiming that whatever the id value, is that apple has now been picked!
Seeing this in action, as part of this class, I have a job, with the following contents.
In here we see a simple job, that takes and creates a new instance of the Apples class, and does a for loop, for 10 iterations, of growing apples. Since our delegate will be called, after an apple is added to he Set, then we see a message similar to the one below, which was fired each time, from the event handler pickApple().
Now moving beyond the mechanics of this process, this can then been applied in several instances of AX. Typically, in other languages, we see such event handlers, as when buttons are clicked, or just before or after a method is fired.
That is actually an option you can do, for any method, you can setup a pre
event handler subscription for a given method, that will fire on the pre or post processing of another given method.
We can also bring this eventing to a functional level, for say when a Sales Order or greater than $10,000.00 is invoiced, an event is fired that automatically places an order for a pizza party, for the dept. :-)
A little elementary school humor there, but if you really start to think about how this can be used, then the power of such abilities with Microsoft Dynamics AX 2012, comes to life.
Whats more, as I briefly mentioned above, is that this can fire into X++ code, or Managed code. That means C# code, that maybe calls into some WCF service, that takes the given Sales Order over $10,000.00 invoiced, and the customer and products to a cloud based Analysis database? Or fires into a WCF service, that is running on Azure, that connects to a gift card program, to automatically order a $100.00 gift card, for the given sales rep.
I'm reaching, but I'm trying to stretch the thought process into not only can this be used in the reference of given technical needs, like button clicks, or specific method calls, but also, could be used to wrap functional, business workflow events.
For your reference, the following link can be used to get the XPO file, that represents the project in AX 2012.: EventingExample_Appls.xpo
Your download and use of this is under your own terms, there is no warranty of any such for the code. It is, as is, so use in a Development environment of AX 2012, to help complete the picture of what this post explains.
That's all for now, and has we move forward, I will come back to this topic, with examples of managed code, showing off the pre and post event handler calls, as well as getting into some real world examples.
Till next time!"Visit the Dynamics AX Community Page today!"
Labels: AX 2012, Development, Dynamics AX, Dynamics AX 2012, Eventing, How to, Microsoft, X++, Xpp