2

My requirement is to create a version converter design that is efficient. Let us assume the following classes are evolved in each version.

//Version 1

    internal class PatientV1
    {
        public string FullName { get; set; }
        public string PatientNr { get; set; }
        public GenderV1 Gender { get; set; }
    }

    internal enum GenderV1
    {
        MALE,
        FEMALE
    }

//Version 2

    internal class PatientV2
    {
        public Name FullName { get; set; }
        public string PatientNr { get; set; }
        public GenderV2 Gender { get; set; }
    }

    internal class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Title { get; set; }
    }

    internal enum GenderV2
    {
        MALE,
        FEMALE,
        UNKNOWN
    }

//Version 3

    internal class PatientV3
    {
        public IPerson Person { get; set; }
        public string PatientNr { get; set; }
    }

    internal interface IPerson
    {
        Name Name { get; set; }
        GenderV3 Gender { get; set; }
    }


    internal class Person : IPerson
    {
        public Name Name { get; set; }
        public GenderV3 Gender { get; set; }
    }


    internal enum GenderV3
    {
        MALE,
        FEMALE,
        MALE_TO_FEMALE_CONVERSION_INPROGRESS,
        FEMALE_TO_CONVERSION_INPROGRESS,
        UNKNOWN
    }

I have created JsonConverter to deserialise one version to another version. This has increased many combinations of converters ((n-1)!) such as.

  1. PatientV1ToV2Converter to convert from version 1 to version 2
  2. PatientV1ToV3Converter to convert from version 1 to version 3
  3. PatientV2ToV3Converter to convert from version 2 to version 3

So instead of this I would like PatientV1ToV3Converter calls PatientV1ToV2Converter to bring the Patient from PatientV1 to PatientV2 and then PatientV1ToV3Converter to work on PatientV2 to make it PatientV3.

All these I would like to do without having any of the older version of Patient (PatientV1 and PatientV2) being present in the application directory.

Any design, with or without the older dll restriction is fine with me.

keyr
  • 990
  • 13
  • 27

2 Answers2

2

I have done something similar in my project

I have set

serializerSettings.Converters = new List<JsonConverter>() { new MyAppVersionConverter() };

In this MyAppVersionConverter, I have overriden ReadJson and written the following code

JObject jObjectV = JObject.Load( reader );
//Get the type information from JObject by getting the "$type" property and strip it to get the version
string objectVersion = GetVersion(jObjectV.GetValue("$type"));

Now either modify the existing jObjectV by adding or removing the property to translate to the desired version then

e.g. from my code

JProperty firstNameProp = new JProperty( "FirstName", firstName );
JProperty lastNameProp = new JProperty( "LastName", lastName );
JProperty typeProperty = new JProperty( "$type", "MyDetailV2.EmployeeDetail, MyDetailV2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" );

JObject jObjectV2 = new JObject();
jObjectV2.Add( typeProperty );
jObjectV2.Add( firstNameProp );
jObjectV2.Add( lastNameProp );

return jObjectV2.ToObject<EmployeeDetail>();

I am still refining things. I shall post once i have done the complete change.

Let me know if you have any problem in this regard

bandu
  • 58
  • 5
  • This looks fine. I also thought so but was having trouble in constructing the JObject and deserialising into object and .... I am really looking forward to see your complete solution. – keyr Nov 29 '12 at 17:33
  • One more thing. You have mentioned `JProperty typeProperty = new JProperty( "$type", "MyDetailV2.EmployeeDetail, MyDetailV2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" );`, We do not have to be specify this as detailed as a fully qualified type name right? – keyr Nov 30 '12 at 04:35
0

Your approach is sound. Some other projects are also using similar methods how to handle upgrades.

For example, MythTV project is using same idea how to upgrade database schema live from any older version. So far, they have been able to seamlessly handle over 300 revisions.

mvp
  • 111,019
  • 13
  • 122
  • 148
  • Any idea as to how to do it using Json.net with or without (prefered) the older dll restriction? – keyr Nov 16 '12 at 10:53