Data Versioning


The types that hold the data are always subject to the .NET rules of versioning, [4] but that doesn't really help you when it comes to reading and writing old versions of the data using new versions of the object. One way to support versioned data formats is to write a version ID into the stream as part of a custom implementation of ISerializable:

[4] The details of .NET type versioning are covered in Essential .NET (Addison-Wesley, 2003), by Don Box, with Chris Sells.

 
 <Serializable()> _ Class MyData   Implements ISerializable   Dim s As String = "Wahoo!"   Dim n As Integer = 6   Dim oldStrings As ArrayList = New ArrayList() ' v2.0 addition   Shared currentVersion As String = "2.0"   ...   #Region Implementation of ISerializable   Public Overrides Sub New(info As SerializationInfo, _       context As StreamingContext)       ' Read the data based on the version       Dim streamVersion As String = info.GetString("Version")       Select Case streamVersion           Case "1.0"               s = info.GetString("MyString")               n = s.Length           Case "2.0"               s = info.GetString("MyString")               n = s.Length               oldStrings = CType(info.GetValue("OldStrings", _                   GetType(ArrayList)), ArrayList)       End Select   End Sub   Public Sub GetObjectData(info As SerializationInfo, _       context As StreamingContext) _ Implements ISerializable.GetObjectData ' Tag the data with a version       info.AddValue("Version", currentVersion)       info.AddValue("MyString", s)       info.AddValue("OldStrings", oldStrings)   End Sub   #EndRegion End Class 

This implementation writes a Version string on all the data it writes , and then it uses that string to decide which data to read back in at run time. As the data in a class changes, marking it with a version gives you a way to migrate old data forward (or to save old versions, if you'd like).

Notice also that the new hunk of data added is an ArrayList. Just like the simple types, the collection classes (along with a large number of other classes in the .NET Framework) can be serialized, making this model useful for all kinds of things, from storing user settings (as discussed in Chapter 11: Applications and Settings) to saving document state.



Windows Forms Programming in Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net