Wednesday 29 August 2012

Dynamics AX 2012 Eventing Framework



Introduction:


In Dynamics AX each customer has a set of requirements in the product which are customized by the partners. These Customizations can be broken when a newer version is released by Microsoft. The use of events has the potential to lower the cost of creating and upgrading customizations.

Key Terms:


 Producer:


 The producer is the logic that contains the code that causes a change. It is an entity that emits events.

 Consumer:


The consumer is the application code that represents an interest in being notified when a specific event occurs. It is an entity that receives events.

 

Event:

An event is a representation of a change having happened in the producer.

 

Event Payload:

 
The event payload is the information that the event carries with it.

 

Delegate:

 
 
A delegate is the definition of the information passed from the producer to the consumer when an event takes place.

 
Events provide the following benefits:


Observation:

 Events can be used to generate alerts on any specific behavior.

 Information dissemination:

 Events can deliver the right information to the right consumers at the right time.

Decoupling:
Events produced by one part of the application can be consumed by a completely different part of the application. There is no need for the producer to be aware of the consumers, nor do the consumers need to know details about the producer. One producer's event can be acted upon by any number of consumers. Conversely, consumers can act upon any number of events from many different producers

 

 Coded Events:


  Delegates can be added as members of class.  The syntax for defining a delegate is the same as the syntax used for defining a    method, except that:

1.    The "delegate" keyword is used.

2.    Neither "public" nor any other accessibility specifier is allowed.

3.    The return type must be "void."

4.    The body must be empty; that is, it can contain neither declarations nor statements.

 

Implementation of Coded Events:

 
The picture below shows a simple example of implementing Coded Events in Ax.

 
 


 

 
 
 
 
 


In the above project there are two classes EmployeeSelector and Employee.

Ø  In the Employee class EmployeeSelected is the delegate and EmployeeSelected_SelectedEmployee is the Event handler subscribed to it.

Ø  Adding a delegate is as simple as adding a new method.

 



 
 
 
 
 
 
 
 
 
 
 
 
 
 


Ø  This is how the EmployeeSelected delegate looks like.

 



 

 
 
 
 
 

 
 
Ø  The properties of EmployeeSelected_SelectedEmployee Event handler should be changed like this.
 
 

 



 

 

 

 
 
Ø  Here EmployeeSelector is the class that the EmployeeSelected_SelectEmployee Event Handler points to and SelectEmployee is the method.

      Ø In the AddEmployee Method   this. EmployeeSelected (employee) is a call to
            the EmployeeSelected delegate.

     

 

 

 

 
 
 
 
 
Ø  This is how the SelectEmployee method looks like. The method pointed by the EventHandlerSubscriber should be Static and its return type should be similar to the delegate as shown below.
 


 

 

 

 
 
 
Hope it was helpful to you…

 

 

  

 

1 comments:

Unknown said...

How we can return a value from subscriber method using coded events/delegates ?

Post a Comment