473,471 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Backward compatibility issue of Serialization

Hi everybody,

I'm planning to use serialization to persist an object (and possibly
its child objects) in my application. However, I'm concerned about the
backward compatibility issue. I'm evaluating if we can easily resolve
this issue.

For example, I have a class MyClass consisting of 100 fields.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
}

The basic serialization can be done simply by adding [Serializable]
attribute to the class. Suppose an object of this class has been
serialized to MyClass.bin using BinaryFormatter. Later, we have a new
version of this class, with the addition of newField101.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
private int newField101;
}

If we use the basic deserialization to retrieve this object, the
following runtime exception will be thrown.

System.Runtime.Serialization.SerializationExceptio n: Possible Version
mismatch. Type MyClass has 101 members, number of members deserialized
is 100.

My question is how I can resolve this problem. I attempt to solve this
problem using the following approach.

[Serializable]
public class MyClass: ISerializable
{
private int oldField1;
private int oldField2;
...
private int oldField100;

protected MyClass(SerializationInfo info, StreamingContext context)
{
oldField1 = info.GetInt32("oldField1");
oldField2 = info.GetInt32("oldField2");
...
oldField3 = info.GetInt32("oldField100");

if (info.MemberCount == 100)
newField101 = 0;
else
newField101 = info.GetInt32("newField101");
}
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("oldField1", oldField1);
info.AddValue("oldField2", oldField2);
...
info.AddValue("oldField100", oldField100);
info.AddValue("newField100", newField101);

}
}

Does the above approach always solve the backward compatibility issue
of (de-)serialization?

Even if it does, it is a hassle that I need to list all 100 fields (or
101 fields) in the MyClass constructor and GetObjectData. Is there an
easier way to achieve backward compatibility?

Even if the class structure is not changed, do you think that a new
version of .NET framework will create similar compatibility problem?

Thanks
Dominic
Nov 15 '05 #1
2 6172
So you have two things to worry about:
1. MyClass is going to change
2. MS is going to change the serialization format

1. Don't change MyClass at all, instead derive a new type from MyClass and use
that to add new fields. If you still want your application to return MyNewClass
types, then you can use a SerializationBinder to map instances of MyClass in the
stream to MyNewClass. There are many ways to control this entire process, and
some are more scalable/complex versus easy/quick to implement.
2. MS isn't going to change the format. If they do, the BinaryFormatter should
be able to handle both old and new formats.

What I would be worried about is contained in my blog entry:
http://weblogs.asp.net/justin_rogers.../02/66508.aspx

Check that out and figure if you still want to use the BinaryFormatter
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Dominic" <do****@hotmail.com> wrote in message
news:2b**************************@posting.google.c om...
Hi everybody,

I'm planning to use serialization to persist an object (and possibly
its child objects) in my application. However, I'm concerned about the
backward compatibility issue. I'm evaluating if we can easily resolve
this issue.

For example, I have a class MyClass consisting of 100 fields.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
}

The basic serialization can be done simply by adding [Serializable]
attribute to the class. Suppose an object of this class has been
serialized to MyClass.bin using BinaryFormatter. Later, we have a new
version of this class, with the addition of newField101.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
private int newField101;
}

If we use the basic deserialization to retrieve this object, the
following runtime exception will be thrown.

System.Runtime.Serialization.SerializationExceptio n: Possible Version
mismatch. Type MyClass has 101 members, number of members deserialized
is 100.

My question is how I can resolve this problem. I attempt to solve this
problem using the following approach.

[Serializable]
public class MyClass: ISerializable
{
private int oldField1;
private int oldField2;
...
private int oldField100;

protected MyClass(SerializationInfo info, StreamingContext context)
{
oldField1 = info.GetInt32("oldField1");
oldField2 = info.GetInt32("oldField2");
...
oldField3 = info.GetInt32("oldField100");

if (info.MemberCount == 100)
newField101 = 0;
else
newField101 = info.GetInt32("newField101");
}
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("oldField1", oldField1);
info.AddValue("oldField2", oldField2);
...
info.AddValue("oldField100", oldField100);
info.AddValue("newField100", newField101);

}
}

Does the above approach always solve the backward compatibility issue
of (de-)serialization?

Even if it does, it is a hassle that I need to list all 100 fields (or
101 fields) in the MyClass constructor and GetObjectData. Is there an
easier way to achieve backward compatibility?

Even if the class structure is not changed, do you think that a new
version of .NET framework will create similar compatibility problem?

Thanks
Dominic

Nov 15 '05 #2
JS
I'm in the middle of a fairly large application and was using
serialization to save object data. However, this became a huge
headache for a lot of reasons. So I changed all of my persistence to
XML. Note, I did not use an XML serializer, I just added Save/Load
methods to the classes I needed to persist.

This has worked out great. No more SoapFormatter problems. No more
versioning problems.
From my experience I would recommend avoiding serialization for saving

data. Serialization is still needed for remoted-by-value objects, of
course, but I found that I really don't need to implement ISerializable
any more.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run...
1
by: Vyacheslav Lanovets | last post by:
Hello, All! Installed VS 2005... What is annoying is that project files are not backward compatible. For instance, EVC4 vcp files are backward compatible with EVC3 and can be used...
2
by: Neal Becker | last post by:
How can I write code to take advantage of new decorator syntax, while allowing backward compatibility? I almost want a preprocessor. #if PYTHON_VERSION >= 2.4 @staticmethod ....
1
by: Dev | last post by:
Dear Friends, I have created VC++.NET dll by VS.NET2003.Is it possible to use this dll(vs.net2003) into C# version (Vs.net2002)? Is there backward compatibility? If so ..How do to this?..If...
1
by: MLibby | last post by:
I'm a Netscape newbie and am using it for backward compatibility testing. How do I debug javascript in Netscape? I set Netscape as the default debugger (design mode | file | Browse With) and I am...
3
by: Madhu | last post by:
We are having a dll backward compatibility issue since we migrated to .NET 2005. As part of our product, we build a dll which is used by other products. Everything was fine when we were build it...
0
by: bhargav mandlem | last post by:
hi friends i have created VC++.NET dll by VS.NET2003.Is it possible to use this dll(vs.net2003) into C# version (Vs.net2002)? Is there backward compatibility? If so ..How do to this?..If anyone...
2
by: Ernesto Bascón | last post by:
Hi: I've read some about the pimpl idiom and I know that it provides safe ABI backward compatibility on shared libraries. Let's consider the following definitions: template <class T> class...
7
by: Ernesto Bascón | last post by:
Hi everybody: I want to develop a library that uses heavily templates. Is there an idiom or some tips about things that I should care of in order to provide backward compatibility with my...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.