Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsReference property in data contract

What is the purpose of IsReference property in DataContract? How does the request and response vary with this property applied?

like image 774
web dunia Avatar asked Jun 24 '09 08:06

web dunia


People also ask

What is data contract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.

What is data contract in MVC?

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.

What is data contract and data member?

DataContact. A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.


2 Answers

It determines how objects are serialized, by default, IsReference=false.

Setting IsReference = true allows the serialization of trees of objects that can reference each other. So with a list of Employees that each have a property for Manager (who is also an Employee), a reference to the Manager for each Employee can be held rather than embedding the Manager within each Employee node:

IsReference=false would produce:

<Employee>        <Manager i:nil=“true“ />        <Name>Kenny</Name>  </Employee>  <Employee>        <Manager>              <Manager i:nil=“true“ />              <Name>Kenny</Name>        </Manager>         <Name>Bob</Name>  </Employee>  <Employee>        <Manager>              <Manager i:nil=“true“ />              <Name>Kenny</Name>        </Manager>         <Name>Alice</Name>  </Employee>  

Where as IsReference=true would produce:

<Employee z:Id=“i1“ xmlns:z=“http://schemas.microsoft.com/2003/10/Serialization/“>        <Manager i:nil=“true“ />         <Name>Kenny</Name>  </Employee>  <Employee z:Id=“i2“ xmlns:z=“http://schemas.microsoft.com/2003/10/Serialization/“>        <Manager z:Ref=“i1“ />         <Name>Bob</Name>  </Employee>  <Employee z:Id=“i3“ xmlns:z=“http://schemas.microsoft.com/2003/10/Serialization/“>        <Manager z:Ref=“i1“ />         <Name>Alice</Name>  </Employee>  

Snippets taken from this weblog that has a full explanation along with examples of the generated XML with the property applied.

MSDN - IsReference Property provides details as well as Interoperable Object References.

like image 145
Tanner Avatar answered Sep 20 '22 11:09

Tanner


Also IsReference does not exist in .NET Framework 3.5. So you could get errors when using it with that Framework version - it only exists in 4.5, 4, 3.5 SP1 and Silverlight.

"Error 297 'System.Runtime.Serialization.DataContractAttribute' does not contain a definition for 'IsReference' "

like image 34
Andriy Buday Avatar answered Sep 20 '22 11:09

Andriy Buday