title: Backwards compatibility in .NET with BinaryFormatter
body: <p>We use BinaryFormatter in a C# game, to save user game progress, game levels, etc. We are running into the problem of backwards compatibility.</p>
  
  <p>The aims:</p>
  
  <ul>
  <li>Level designer creates campaign (levels&amp;rules), we change the code, the campaign should still work fine. This can happen everyday during development before release.</li>
  <li>User saves game, we release a game patch, user should still be able to load game</li>
  <li>The invisible data-conversion process should work no matter how distant the two versions are. For example an user can skip our first 5 minor updates and get the 6th directly. Still, his saved games should still load fine.</li>
  </ul>
  
  <p>The solution needs to be completely invisible to users and level designers, and minimally burden coders who want to change something (e.g. rename a field because they thought of a better name).</p>
  
  <p>Some object graphs we serialize are rooted in one class, some in others. Forward compatibility is not needed.</p>
  
  <p>Potentially breaking changes (and what happens when we serialize the old version and deserialize into the new):</p>
  
  <ul>
  <li>add field (gets default-initialized)</li>
  <li>change field type (failure)</li>
  <li>rename field (equivalent to removing it and adding a new one)</li>
  <li>change property to field and back (equivalent to a rename)</li>
  <li>change autoimplemented property to use backing field (equivalent to a rename)</li>
  <li>add superclass (equivalent to adding its fields to the current class)</li>
  <li>interpret a field differently (e.g. was in degrees, now in radians)</li>
  <li>for types implementing ISerializable we may change our implementation of the ISerializable methods (e.g. start using compression within the ISerializable implementation for some really large type)</li>
  <li>Rename a class, rename an enum value</li>
  </ul>
  
  <p>I have read about:</p>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms229752" rel="noreferrer">Version Tolerant Serialization</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ideserializationcallback.aspx" rel="noreferrer">IDeserializationCallback</a></li>
  <li>[OptionalField(VersionAdded)]</li>
  <li>[OnDeserializing], [OnDeserialized], [OnSerializing], [OnSerialized].</li>
  <li>[NotSerialized]</li>
  </ul>
  
  <p><strong>My current solution</strong>:</p>
  
  <ul>
  <li>We make as many changes as possible non-breaking, by using stuff like the OnDeserializing callback.</li>
  <li>We schedule breaking changes for once every 2 weeks, so there's less compatibility code to keep around.</li>
  <li>Everytime before we make a breaking change, we copy <em>all</em> the [Serializable] classes we use, into a namespace/folder called OldClassVersions.VersionX (where X is the next ordinal number after the last one). We do this even if we aren't going to be making a release soon.</li>
  <li>When writing to file, what we serialize is an instance of this class: class SaveFileData { int version; object data; }</li>
  <li>When reading from file, we deserialize the SaveFileData and pass it to an iterative "update" routine that does something like this:</li>
  </ul>
  
  <p>.</p>
  
  <pre><code>for(int i = loadedData.version; i &lt; CurrentVersion; i++)
  {
      // Update() takes an instance of OldVersions.VersionX.TheClass
      // and returns an instance of OldVersions.VersionXPlus1.TheClass
      loadedData.data = Update(loadedData.data, i);
  }
  </code></pre>
  
  <ul>
  <li>For convenience, the Update() function, in its implementation, can use a CopyOverlappingPart() function that uses reflection to copy as much data as possible from the old version to the new version. This way, the Update() function can only handle stuff that actually changed.</li>
  </ul>
  
  <p>Some problems with that:</p>
  
  <ul>
  <li>the deserializer deserializes to class Foo rather than to class OldClassVersions.Version5.Foo - because class Foo is what was serialized.</li>
  <li>almost impossible to test or debug</li>
  <li>requires to keep around old copies of a lot of classes, which is error-prone, fragile and annoying</li>
  <li>I don't know what to do when we want to rename a class</li>
  </ul>
  
  <p>This should be a really common problem. How do people usually solve it?</p>