Friday, August 31, 2012

Eventing In Ax2012

Eventing
Lets the user use a publisher and subscriber model when modifying MDAX2012.
Events can be modeled in the AOT or be used as a programming construct and can be handled in either X++ code or in managed code.
NOTE: Modeled events are only available on classes and not tables or forms.
Eventing Terminology
MDAX2012 events are modeled after the .NET event concepts :
Producer :- The producer is the logic that contains the code that causes a change. This means that it is the entity that emits events.
Consumer :- The consumer is the application code that manifested an interest in being notified when a specific event occurs. This means that it is an entity that receives events.
Event :- An event is a representation of a change that happened in the producer. Microsoft Dynamics AX 6.0 supports Pre and Post events that occur before and after a method is called.
Event Payload :- The event payload is the information that the event carries with it. If a person is hired, for example, the payload includes the employee's name and date of birth, and so on.
Delegate :- A delegate is the definition of the information passed from the producer to the consumer when an event happens.
Event Handlers
Event handlers are methods that are called when the delegate is called, directly through code (for the coded events) or from the environment (in the modeled events). The relationship between the delegate and the handlers can be maintained in code or in the AOT.
Delegate is a keyword.
When program conditions meet the programmer's criteria for the event, the X++ code can call the delegate, and that causes the delegate to call all the event handler methods that are added to the delegate.
To create a delegate, right-click the class and select New->Delegate.
Adding Handlers in the AOT
The user must identify a static method to handle the event on the delegate. However, when adding event handlers from code, described in the following material, instance methods are also applicable as event handlers. Event handlers can be added to the delegate by dragging the event handler to the delegate node that represents the event to be handled.
Adding Handlers in Code
Use special X++ syntax to remove or add event handlers to events. The delegate name appears on the left side of the += operator.
private void AddStaticHandler()
{
;
this.MyDelegate += eventhandler
(Subscriber::MyHandler);
}
Pre and Post Events
You can subscribe an event handler to automatically run immediately before a method is run.
The event handler can change the parameter values before they are entered into the method. You can also subscribe an event handler to run immediately after a method is run. The event handler can change the value that is returned by the method, before the return value is received by the caller of the method.

Event handlers for these before and after events are visible in the AOT as sub nodes on the methods to which they apply.

No comments: