Happy stress free coding

Feb 26, 2012

Note to self–how to remove the need for knowing about enum types when streaming WCF


The data contract surrogate is an advanced feature built upon the Data Contract model. This feature is designed to be used for type customization and substitution in situations where users want to change how a type is serialized, deserialized or projected into metadata. Some scenarios where a surrogate may be used is when a data contract has not been specified for the type, fields and properties are not marked with the DataMemberAttribute attribute or users wish to dynamically create schema variations.

Serialization and deserialization are accomplished with the data contract surrogate when using DataContractSerializer to convert from .NET Framework to a suitable format, such as XML. Data contract surrogate can also be used to modify the metadata exported for types, when producing metadata representations such as XML Schema Documents (XSD). Upon import, code is created from metadata and the surrogate can be used in this case to customize the generated code as well.

How the Surrogate Works

A surrogate works by mapping one type (the "original" type) to another type (the "surrogated" type). The following example shows the original type Inventory and a new surrogate InventorySurrogated type. The Inventory type is not serializable but the InventorySurrogated type is:


Post a Comment

Subscribe to Post Comments [Atom]

<< Home