Search code examples
wcfdatacontract

WCF datacontract vs class serialize


I understand that we can have more controls on a class if we use datacontract, however, consider the following 2 cases

[DataContract]
public class Customer
{
    [DataMember]
    public string CustomerName {get; set;}

    [DataMember]
    public int Age{get; set;}
}

and

public class Customer
{
    public string CustomerName {get; set;}
    public int Age{get; set;}
}

They both get serialized correctly on .net client. And personally I do not use second example. Can anybody point me the differences in the 2 classes? I meant to send all public properties in both classes.


Solution

  • The second version is the POCO (plain old CLR object) version of your data contract and can be used with WCF since 3.5 sp1.

    I would not recommend using it though as it gives you very little control on the serialization (namespace attributes...) and it couples your service entities with your business entities (which can be same with POCO)