views:

119

answers:

2

I was reading Microsoft's Best Practices: Data Contract Versioning, and they state:

Do not remove data members in later versions, even if the IsRequired property was left at its default property of false in prior versions.

Can anyone suggest any reason for this? They don't elaborate. Since they say it's fine to add data members in a later version, it seems that removing would be fine too -- effectively, the older version would see it as an add.

The difference, I suppose, is that you're supposed to add new members at the end (using the Order property on the DataMemberAttribute), whereas the property getting removed would probably not be at the end. But they also say that missing members will be left at their default value during loading, so it's clear that missing members are OK.

What am I missing? What version-interop problems would I cause (both forward-compatibility and backward-compatibility) if I obsoleted a feature of my product and removed the [DataMember] property that goes with it?

Also, if I decided that I wasn't interested in forward-compatibility (i.e., if I wasn't concerned about older versions opening newer files), would any such problems still apply?

A: 

Simply because outside service consumers may provide/use that data (they were created before you removed some members). In case you've changed service method signature, DataContractSerializer won't be able to recognize DataContract anymore, because of unknown data members.

So if your service consumers are all known, you can easily manipulate data members at your own will as long as you:

  • don't break consumers or
  • properly inform them of the change
Robert Koritnik
+1  A: 

One issue is that even if it doesn't break during serialization/deserialization, you could be throwing away data - meaning that you can't successfully round-trip data back to the caller. i.e. given the simple method:

public SomeType Echo(SomeType obj) {
    return obj;
}

If the caller is passing you the old object with the extra property, they might want that value back. You can enable this (separately) with the extension data API, but frankly people rarely bother with this.

Marc Gravell