IsReference property in data contract

web dunia picture web dunia · Jun 24, 2009 · Viewed 23.2k times · Source

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

Answer

Tanner picture Tanner · Jun 24, 2009

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.