Patrick Desjardins Patrick Desjardins - 28 days ago 14
C# Question

IExtensibleDataObject vs IExtensibleObject?

I am trying to figure out what is the difference between IExtensibleDataObject and IExtensibleObject.

MSDN say that the first one (IExtensibleDataObject) is to let the deserialization of object that may have added attribute and the second one (IExtensibleObject) look very similar, it does let the object add attribute too.

I am confused.

Answer

IExtensibleDataObject is about serialization, and it can be used outside of WCF's service stack. Its main purpose is round-tripping different versions of a data contract without losing information. For example, on the first version of your contract, you have this type:

[DataContract(Name = "Person")]
public class Person : IExtensibleDataObject {
   ExtensionDataObject IExtensibleDataObject.ExtensionData { get; set; }
   [DataMember(Order = 0)] public string Name;
   [DataMember(Order = 1)] public int Age;
}

You deploy your services with this data type, and you have some clients using this type. Some service operations return a Person to the client, and the client can send those objects back to the service, as in the example below.

[ServiceContract]
public interface ITest {
    [OperationContract] Person[] GetAllPeople();
    [OperationContract] void DoSomething(Person person);
}

It all works great, until a change in the business logic requires that a new member to be added to Person, and a backing database requires that field to be present (not null).

[DataContract(Name = "Person")]
public class Person_V2 : IExtensibleDataObject {
   ExtensionDataObject IExtensibleDataObject.ExtensionData { get; set; }
   [DataMember(Order = 0)] public string Name;
   [DataMember(Order = 1)] public int Age;
   [DataMember(Order = 2)] public string Address;
}

Without IExtensibleDataObject, the existing clients would receive the Person object, fill its Name / Age property and promptly discard the Address element passed to it. And when it called the DoSomething method with that object, it would pass an instance which would be invalid at the server (Address would be null).

What IEDO does is enable this scenario, where existing (legacy) clients can continue receiving new versions of data contracts from the service - the client will fill the fields it understands with the data from the service, and those elements which it doesn't understand will be stored in the ExtensionDataObject so that they can be reserialized later. In the example above, the legacy clients will only be able to read the Name and Age properties of the Person, but when it sends the object back to the server, the serialized data will contain all three properties.

That was a long story about IEDO. IExtensibleObject does not have anything to do with serialization - is about hooking up extensions to some pre-defined objects in the WCF service stack (the host, the operation context, the instance context and the context channel). Not as interesting as IEDO, so I'll stop for here :)

Edited: for completeness sake, if you want more information about IExtensibleObject, you can check the post at http://blogs.msdn.com/b/carlosfigueira/archive/2012/01/31/wcf-extensibility-iextension-and-iextensibleobject.aspx.