Menu

Upgrading Serialized Classes

Low Key
2003-03-15
2003-03-15
  • Low Key

    Low Key - 2003-03-15

    I like the object prevalence concept but was wondering - What is a good way to go about upgrading/modifying existing serialized objects?  If I decide to add a property to a class later on, how do I deal with existing instances of that class?  Is implementing the ISerializable interface the best way to go or can I design my classes in another way that would allow for easy upgrades?  Does anyone have a solution that they've found works well?

     
    • Rodrigo B. de Oliveira

      Yes. There's definitely a good solution. Well, at least it's been working great for me until now: Bamboo.Prevalence.VersionMigration. You'll find it in the Tools/VersionMigration folder. I strongly recommend you to get the cvs version though.

      Unfortunately I hadn't the time to document its usage properly.

      Basically, it works like this:
      1) You don't write any special code in your classes to handle version upgrades or anything
      2) You write a xml document (a MigrationPlan) that documents the differences between the new class schema and the old one, for instance:
      <!--
          xml:lang attribute controls how
          const values are interpreted
      -->
      <migrationPlan xml:lang="pt-br">   

          <typeMapping type="SamplePrevalentSystem.LibrarySystem">
              <alias>SamplePrevalentSystem.Library</alias>
          </typeMapping>

          <!--
              A typeMapping specifies how a type should be
              loaded from a serialized stream.
          -->
          <typeMapping type="SamplePrevalentSystem.Title">
         
              <!--
                  Each field definition specifies how
                  the specific field should be
                  initialized.
                 
                  Unspecified fields are loaded directly
                  from the stream.
                 
                  An unspecified field that is not present
                  in the stream represents an error.
              -->   
              <field name="_description">
                  <fromField>_summary</fromField>
              </field>
             
              <field name="_publishDate">
                  <!-- September 10th, see xml:lang at the top -->
                  <const>10/09/2002</const>
              </field>
             
              <field name="_reviews">
                  <newObject />
              </field>
             
          </typeMapping>        

      </migrationPlan>

      3) You feed this migration plan to the VersionMigratorGUI tool and there you go.

      Bamboo.Prevalence.VersionMigration is able to handle the more common refactoring like "rename field", "delete field", "change field type", "rename class", "move class to a different assembly". But the architecture is pretty extensible and it is my hope that with more people using it and contributing code to it, well, you know...

      Rodrigo

       

Log in to post a comment.