Ads

Monday, 1 December 2014

Data Contract and Data Member in WCF - 5

Before knowing this, lets know some of the terms used in WCF.



Serialization: It’s the process of converting .net objects to XML representation format.
De-serialization: The reverse of serialization. i.e to reconstruct the same .net object from XML format.
By default WCF uses DataContractSerializer serialized.
For complex types to be serialized, it can either use Serializable attribute or DataContract attribute.

Eg. of complex type can be Employee, student …….




By default DataContract serializer will serialize all public properties of complex type in alphabetical order. We don’t have to use these attributes explicitly.
Private fields and properties are not serialized by DataContract serializer.

If we see this serialization of items in WSDL document then we must clear about this.




Two ways to serialize complex types:
·         Decorate complex type with [Serializable] attribute
o   Serializes all fields, No granular control of fields.
o   Not have explicit control on what field we exclude and include from serialized data
·         Decorate with [DataContract] attibute (preferred)
o   We have control over which fields we need to include and exclude
o   It only serializes the fields which are marked as [DataMember] attributes
o   It will not serialize which are not decorated with [DataMember] attribute
o   You can define XML namespace for our data
o   You can also define Name, Order , IsRequired inside [DataMember] attribute
o   Also serialize private fields and properties


Here we can see
Name attribute in data member: used to give explicit name to fields.
Order attribute in data member: used to define custom order of fields.

No comments:

Post a Comment

Ads