Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>could you not create a new version of your data structure class each time the structure changes, and have the constructor for the new class take an instance of the previous class, and populate itself from there. To load the newest class you try and create the earliest class from the serialised file until one succeeds, and then pass that into the constructor of the next class in the chain repeatedly until you get the latest version of the data structure then you can save that.</p> <p>Having a new class for each change in format would avoid having to change any existing code when the data structure changed, and your app could be ignorant of the fact that the save file was some older version. It would allow you to load from any previous version, not just the last one.</p> <p>This sort of thing implemented by a <a href="http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" rel="nofollow noreferrer">chain of responsibility</a> can make it easy to slot in a new format with minimal changes to your existing code.</p> <p>Whilst not a textbook chain of responsibility you could implement with something like this:</p> <p>(NOTE: untested code)</p> <pre><code>public interface IProductFactory&lt;T&gt; where T : class { T CreateProduct(string filename); T DeserializeInstance(string filename); } public abstract class ProductFactoryBase&lt;T&gt; : IProductFactory&lt;T&gt; where T : class { public abstract T CreateProduct(string filename); public T DeserializeInstance(string filename) { var myFormatter = new BinaryFormatter(); using (FileStream stream = File.Open(filename, FileMode.Open)) { return myFormatter.Deserialize(stream) as T; } } } public class ProductV1Factory : ProductFactoryBase&lt;ProductV1&gt; { public override ProductV1 CreateProduct(string filename) { return DeserializeInstance(filename); } } public class ProductV2Factory : ProductFactoryBase&lt;ProductV2&gt; { ProductV1Factory successor = new ProductV1Factory(); public override ProductV2 CreateProduct(string filename) { var product = DeserializeInstance(filename); if (product==null) { product = new ProductV2(successor.CreateProduct(filename)); } return product; } } public class ProductV2 { public ProductV2(ProductV1 product) { //construct from V1 information } } public class ProductV1 { } </code></pre> <p>this has the advantage that when you want to add ProductV3 you only need to change the class you are using in your app to be a ProductV3 type, which you need to do anyway, then you change your loading code so that it uses a ProductV3Factory, which is basically the same as a ProductV2Factory, but it uses a ProductV2Factory as the successor. You don't need to change any existing classes. you could probably refactor this a bit to get the commanality of <code>CreateProduct</code> into a base class, but it gets the idea across.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload