Friday, December 20, 2013

Using Data Contracts in X++ [AX 2012]


Microsoft Dynamics AX supports the use of .NET and X++ types as data contracts for service operation input and return parameters. This enables you to pass complex data types without having to explicitly implement XML serialization and deserialization. The data contract serialization and deserialization is handled by Windows Communication Foundation (WCF).
In order to serialize complex data types, we must define them as data contracts.
X++ Types
X++ provides support for data contract serialization through the following attributes.
·         DataContractAttribute – This attribute is applied to an X++ class and specifies that the class can be used as a data contract (that it should be serialized).
·         DataMemberAttribute – This attribute is applied to a parm method on an X++ data contract class and specifies that the data member should be serialized. The attribute can be applied to public and instance methods, as long as the method uses a get/set pattern that contains one optional parameter. The data type of the return type and the parameter should be the same. For example, [DataMemberAttribute] Int parmXX(int _x = x)
The X++ data contract attributes function just as the data contract attributes for .NET types.
The following X++ constructs can be used implicitly as data contracts and do not require markup with the data contract attributes:
·         Primitive types such as str, int, int64, real, guid, utcdatetime, and date. Primitive types are shared between services on the same integration port and stored in a shared types .xsd file.
·         Extended data types.
·         Enumerations including X++ boolean.
·         Collections in which all elements are the same type and all elements are of a type that is a valid data contract. This includes the Array class, which is part of Collections.
·         Tables and views. All columns and array fields are exposed as a System.Data.Dataset.
.NET Types
Any .NET type that can be serialized by WCF can be used as a parameter or return type by a service in Microsoft Dynamics AX. By default, WCF uses the Data Contract Serializer to serialize and deserialize data.

To use .NET types as parameters or return types in X++, we must first create a .NET assembly that contains the .NET data contract classes that have been decorated with the data contract attributes. We then add this assembly as a reference in the AOT, and we can use the data contracts in X++ service operations.

When using the DataContractSerializer, WCF serializes DateTime values as Coordinated Universal Time (UTC). When you use DateTime values in a data contract, you must pass them as UTC values and convert to local time in the client that calls the service.


No comments:

Post a Comment