WCF Crib Sheet – Data Contract and Data Member

Data Contract Attribute

[DataContract]
public class ProductDto
{
    ...
}
  • .NET primitive types are serializable.
  • User defined types are by default not serializable.
  • The data contract attribute marks a user defined type as being serializable by the WCF data contract serializer.
  • If a user defined type is not decorated with a data contract attribute, and is a public type, then WCF will infer a data contract attribute and will serialize all members of the type.

Data Contract Inheritance

[DataContract]
public class ProductDto
{
    ...
}

[DataContract]
public class PremiumProductDto : ProductDto
{
    ...
}
  • The data contract attribute is not inheritable and so must be added to all levels in a hierarchy.

Data Member Attribute

[DataContract]
public class ProductDto
{
    [DataMember]
    public int ProductId { get; set; }

    [DataMember]
    public string Name { get; set; }
}
  • The data member attribute denotes which members of a type are serializable by the data contract serializer.
  • Can be applied on fields and properties. Best practice is to only apply to properties.
  • Properties must have both a getter and setter.
  • Can be applied on any field or property irrespective of visibility

Known Type Attribute

public class ContractService : IContractService
{
    public ContractDto GetAll()
    {
        ...
    }
}

[DataContract]
[KnownType(typeof(CreditContractDto))]
[KnownType(typeof(PrepaymentContractDto))]
public class ContractDto
{
    [DataMember]
    public int ContractId { get; set; }

    [DataMember]
    public DateTime StartDate { get; set; }

    [DataMember]
    public DateTime EndDate { get; set; }
}

[DataContract]
public class CreditContractDto : ContractDto
{
    [DataMember]
    public decimal CreditLimit { get; set; }
}

[DataContract]
public class PrepaymentContractDto : ContractDto
{
    [DataMember]
    public decimal PrepaymentAmount { get; set; }

    [DataMember]
    public PrepaymentPeriod PrepaymentPeriod { get; set; }
}
  • The known type attribute indicates the types of sub-class objects that can be expected for the data contract.

Known Type Configuration

<system.runtime.serialization>
  <dataContractSerializer>
    <declaredTypes>
      <add type="ContractDto,BikeStore.WcfServiceLibrary">
        <knownType type="CustomerContractDto,BikeStore.WcfServiceLibrary"/>
        <knownType type="PrepaymentContractDto,BikeStore.WcfServiceLibrary"/>
      </add>
    </declaredTypes>
  </dataContractSerializer>
</system.runtime.serialization>
  • An alternative to using the known type attribute is to denote known types in the service or client’s configuration file.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s