WCF Tutorial
Lección 37 de 236 · Part 6 WCF DataContract and DataMember · Bases de datos
Part 6 WCF DataContract and DataMember
Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists
To understand DataContract and DataMember attributes in WCF, first let’s understand what is meant by Serialization.
With respect to WCF, Serialization is the process of converting an object into an XML representation. The reverse process, that is reconstructing the same object from the XML is called as Deserialization.
By default, WCF uses DataContractSerializer.
For a complex type like Customer, Employee, Student to be serialized, the complex type can either be decorated with
1. SerializableAttribute or
2. DataContractAttribute
With .NET 3.5 SP1 and above, we don’t have to explicitly use DataContract or DataMember attributes. The Data Contract Serializer will serialize all public properties of your complex type in an alphabetical order. By default private field and properties are not serialized.
If we decorate a complex type, with [Serializable] attribute the DataContractSerializer serializes all fields. With [Serializable] attribute we don’t have explicit control on what fields to include and exclude in serialized data.
If we decorate a complex type with [Datacontract] attribute, the DataContractSerializer serializes the fields marked with the [DataMember] attribute. The fields that are not marked with [DataMember] attribute are excluded from serialization. The [DataMember] attribute can be applied either on the private fields or public properties.
In WCF, the most common way of serialization is to mark the type with the DataContract attribute and mark each member that needs to be serialized with the DataMember attribute.
If you want to have explicit control on what fields and properties get serialized then use DataContract and DataMember attributes. 1. Using DataContractAttribute, you can define an XML namespace for your data
2. Using DataMemberAttribute, you can
a) Define Name, Order, and whether if a property or field IsRequired
b) Also, serialize private fields and properties