Weekly Knowledge Share : Attribute in AX 2012
Hi Fellows,
Such a long time, i am here to write a new blog...
let's start with the topic, this blog is mainly for the overview of 'Attribute' in Microsoft Dynamics AX 2012. after get through the blog you can know, whats and hows about the 'Attribute'
Attribute are use the mark any method or class in AX 2012.The purpose of an attribute is to represent or store metadata about classes and methods. In X++, any class that inherits from the ‘SysAttribute’ class is called an attribute.
An attribute can be attached to the definition of the following items:
- Interface
- Class
- Method
- Method on an interface
- Method on a class
- Method on a table
In my terms, Attribute is use to identify as well as restrict the call of any method or class in the AX 2012. In general we have the control over the call of the method of any class with the help of Attribute that define in the the said method..
let’s understand the use of attribute by example:
many times we faced the situation, where we want to call our method of class by some sequence to achieve certain result, to make possible the scenario, we can use the attribute to add the metadata in every method and that metadata is use to call the method in proper sequence..
For that let first create an attribute class,
class methodSequence extends SysAttribute
{
int seqNum;
}
Add parm method to get the sequence number of the method,
public int parmSeqNum(int _seqNum = seqNum)
{
seqNum = _seqNum;
return seqNum;
}
Override new() method to take sequence number as parameter and set it with private variable seqNum,
public void new(int _seqNum)
{
super();
this.parmSeqNum(_seqNum);
}
Our attribute class is done.
Next we have to put those attributes into the method of our class.
let’s create new class called ‘SequencialTask’.
class sequencialTask
{
}
[methodSequence(1)]
public void process3()
{
info(“First Process”);
}
[methodSequence(2)]
public void process2()
{
info(“Intermediate Process”);
}
[methodSequence(3)]
public void process1()
{
info(“Last Process”);
}
Our sequenceTask class is done, According to the attributes we have set in the method.
Flow of call of method is process3(), process2() and in last process1().
Now we have to develop a code to call methods of these classes in a sequence which is defined in attribute. Here, we will use reflection. Following is the code for a job that will execute all methods.
static void startProcess(Args _args)
{
DictMethod dictMethod;
DictClass dictClass;
int methodIndex;
MethodInfo methodInfo;
Map methodBySeq;
sequencialTask sequenceTaskLocal;
methodSequence methodSequenceLocal;
//Using map here to keep method calling in order defined while tagging.
methodBySeq = new Map(Types::Integer, Types::String);
dictClass = new DictClass(className2Id(classStr(sequenceTask)));
methodIndex = 1;
while (methodIndex <= dictClass.objectMethodCnt())
{
dictMethod = new DictMethod(UtilElementType::ClassInstanceMethod,
dictClass.id(),
dictClass.objectMethod(methodIndex));
methodSequenceLocal = dictMethod.getAttribute(classStr(methodSequence));
//Check to see if the attribute was found or not on the current method being traversed
if (methodSequenceLocal != null)
{
if (!methodBySeq.exists(methodSequenceLocal.getPosition()))
{
//Insert the method name into the map with the position defined while taggin as a key
methodBySeq.insert(methodSequenceLocal.getPosition(), dictMethod.name());
}
}
methodIndex++;
}
sequenceTaskLocal = new sequenceTask();
methodIndex = 1;
while (methodBySeq.exists(methodIndex))
{
//Calling all methods of our class sequenceTask that exist in the methodBySeq
//Since methodIndex will go serially from 1 till last method found, our calling will also be sequential(as per tagging)
dictClass.callObject(methodBySeq.lookup(methodIndex), sequenceTaskLocal);
methodIndex++;
}
}
Here is the magic of Attribute in following infolog...
Cheers......
V I R A L S U T H A R
Developer\Technical Consultant,
MS Dynamics AX 2012 R1/R2,
MS Dynamics AX 2009
(Email) viral.suthar@gmail.com
(Mobile) +91 - 9924413199
Comments
Post a Comment