In continuation to my previous post on Basics of attributes, I would like to take this journey further to discuss the use DataMemberAttribute and DataContractAttribute classes provided by AX2012. My idea in this post is not to show how to create a new service or a batch process using attributes but it is to understand the underlying reason for using attributes in these areas.
In short DataMemberAttribute and DataContractAttribute classes are used to define data contracts. However I would like to talk about data contracts in this post so that we clearly understand the meaning of one liner said above .So let\’s begin with some initial questions which come to mind when we hear a term like \”Data Contract\”:
What is a data contract?
If try to understand it word by word in a simple way, contract means an agreement to exchange something between two parties, similarly data contract means that data is being exchanged between two parties.
Who are the two parties which exchanges data?
Now let\’s start moving towards technology, the parties involved in a data exchange are client and a service.
Who provides framework for data contract?
WCF (Windows Communication Foundation) provides the framework for data contracts.
In which format the data is exchanged?
Data exchange happens in XML. The data is serialized and deserialized (converted to and from XML) during the process of data exchange.
Who does data serialization and deserialization?
There is a serialization engine called \”Data Contract Serializer\” which used by WCF to convert the data to and from XML.
So now when we know about the basics of data contract along with a technical flavor so the next questions which come to mind are:
How system identifies what data needs to be exchanged?
Here comes the roles of Attributes. There are 2 attributes which are used to decorate/mark/tag/identify the data which needs to be part of a data contract. These attributes are DataContractAttribute and DataMemberAttribute.
Where to define DataContractAttribute?
To keep it simple DataContractAttribute is defined on classes. More details can be found on MSDN.
Where to define DataMemberAttribute?
DataMemberAttribute is defined on each of the members of the class which needs to be exchanged.
How to define these attributes?
In the same way we define a normal attribute. You can refer to my last post Basics of attributes as well.
Standard AX2012 out of the box provides us with these attribute classes:
Examples can be found in standard AX where these attributes are used, an example is shown below:
DataContractAttribute and DataMemberAttribute also play vital role in SysOperationFramework introduced in AX2012 and there are some very good blogs available to understand how we use these attributes in SysOperationFramework and creation of services. Some really nice articles I would recommend are:
Thanks for reading the blog.