3.4. Data Contract Equivalence
Two data contracts are
The most common way of defining an equivalent data contract is to use the
[DataContract]
struct Contact
{...}
[DataContract(Name = "Contact")]
struct Contact
{...}
In fact, the full name of the data contract always includes its namespace as well, but as you have seen, you can assign a different namespace. In the case of the DataMember attribute, the Name defaults to member name, so these two definitions are identical: [DataMember] public string FirstName; [DataMember(Name = "FirstName")] public string FirstName; By assigning different names to the contract and the members you can generate an equivalent data contract from a different type. For example, these two data contracts are equivalent:
[DataContract]
struct Contact
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
}
[DataContract(Name = "Contact")]
struct Person
{
[DataMember(Name = "FirstName")]
public string Name;
[DataMember(Name = "LastName")]
public string Surname;
}
In addition to having identical names, the types of the data members have to match.
3.4.1. Serialization Order
Equivalent data contracts must serialize and
[DataContract]
class Contact
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
}
[DataContract]
class Customer : Contact
{
[DataMember]
public int CustomerNumber;
}
the members will be serialized in the following order: FirstName , LastName , CustomerNumber . The problem now is that combining data contract hierarchy with aliasing contracts and members might break the serialization order. For example, the following data contract is now not equivalent to the Customer data contract:
[DataContract(Name = "Customer")]
public class Person
{
[DataMember(Name = "FirstName")]
public string Name;
[DataMember(Name = "LastName")]
public string Surname;
[DataMember]
public int CustomerNumber;
}
because the serialization order is CustomerNumber , FirstName , LastName . To resolve this conflict, you need to provide WCF with the order of serialization by setting the Order property of the DataMember attribute. The Order property defaults to -1, meaning the default WCF ordering, but you can assign to it values indicating the required order:
[DataContract(Name = "Customer")]
public class Person
{
[DataMember(Name = "FirstName",
Order = 1
)]
public string Name;
[DataMember(Name = "LastName",
Order = 2
)]
public string Surname;
[DataMember,
Order = 3
)]
public int CustomerNumber;
}
If another member has the same value for its
Order
property, WCF will order them
[DataContract(Name = "Customer")]
public class Person
{
[DataMember(Name = "FirstName",Order =
1
)]
public string Name;
[DataMember(Name = "LastName",Order =
1
)]
public string Surname;
[DataMember,Order =
2
)]
public int CustomerNumber;
}
|