Understanding Object Serialization for Persisting the Graphics Objects


When working with an object-oriented programming language, you sometimes need to persist the state of an object. The .NET Framework class library provides four namespaces to support object serialization:

System.Runtime.Serialization, System.Runtime.Serialization.Formatters, System.Runtime.Serialization.Formatters.Binary, and System.Runtime.Serialization.Formatters.Soap.

To serialize an object, you need to either mark it with the <Serializable> attribute or implement the ISerializable interface. The following is a class that is marked with the <Serializable> attribute:

 <Serializable()> Public Class Line End Class 

If a class to be serialized contains references to other objects, the classes of those other objects must also be marked <Serializable>.

Alternatively, your class can implement the ISerializable interface. See the following section for more information about this interface.

You can see examples of how to serialize objects into both binary data and SOAP formats in the sections "The BinaryFormatter Class" and "The SoapFormatter Class".

The ISerializable Interface

Unless you mark the class with the <Serializable> attribute, all classes whose instances are serialized must implement this interface. You use the ISerializable interface if you want your class to control its own serialization and deserialization.

The ISerializable interface only has one method, GetDataObject, to which you pass a SerializationInfo object and a StreamingContext object. The GetDataObject method will then populate the SerializationInfo object with data necessary for the serialization of the target object.

Listing 4-9 illustrates a class named MySerializableClass that implements the System.Runtime.Serialization.ISerializable interface. A SerializationInfo object and a StreamingContext object are available from the GetObjectData method.

Listing 4-9: A Class Implementing the ISerializable Interface

start example
 Imports System.Runtime.Serialization Public Class MySerializableClass   Implements ISerializable   Dim info As SerializationInfo   Dim context As StreamingContext   Sub GetObjectData(info As SerializationInfo, _     context As StreamingContext) _     Implements ISerializable.GetObjectData     Me.info = info     Me.context = context     ' implementation code here   End Sub End Class 
end example

Classes that implement this interface include System.Data.DataSet, System.Drawing.Font, System.Collections.Hashtable, System.Drawing.Icon, and System.Drawing.Image.

The IFormatter Interface

IFormatter is a member of the System.Runtime.Serialization namespace. Both the BinaryFormatter class and the SoapFormatter class implement this interface. This interface has only two methods, Serialize and Deserialize.

The Serialize method serializes an object or an object containing other objects. The signature of this method is as follows:

 Sub Serialize( _   ByVal serializationStream As Stream, _   ByVal graph As Object _ ) 

The Deserialize method deserializes an object or an object containing other objects. This method has the following signature:

 Function Deserialize( _   ByVal serializationStream As Stream _ ) As Object 

Because the return value of the Deserialize method is an object of type Object, you need to downcast the object into the appropriate type.

Two formats are available to store your persisted objects: binary and Extensible Markup Language (XML). Normally, you upcast either a BinaryFormatter object or a SoapFormatter object as an IFormatter object, depending on the format you use for the serialization.

Listing 4-10 illustrates how you can serialize an object called serializableObject. First the user is asked to select a file to serialize to using a SaveFileDialog and then the object is serialized in the SOAP format.

Listing 4-10: Serializing Objects

start example
 Protected Sub Serialize()   Dim myStream As Stream   Dim saveFileDialog As SaveFileDialog = New SaveFileDialog()   saveFileDialog.Filter = "All files (*.*)|*"   If saveFileDialog.ShowDialog() = DialogResult.OK Then     ' get a Stream object     myStream = saveFileDialog.OpenFile()     If Not (myStream Is Nothing) Then       ' ready to serialize       Dim formatter As IFormatter       formatter = CType(New SoapFormatter(), IFormatter)       formatter.Serialize(myStream, serializableObject)       'serializableObject is an object whose class has       'been marked with the <serializable> attribute or       'implements the ISerializable interface       myStream.Close()   End If End If End Sub 
end example

The BinaryFormatter Class

The BinaryFormatter class is the only member of the System.Runtime.Serialization.Formatters.Binary namespace. You use this class to serialize and deserialize an object or a graph of objects in a binary file. For performance, you need this format when serializing your objects. The resulting file is also more compact than the XML format.

Listing 4-11 provides a class named SerializableClass that is marked for serialization.

Listing 4-11: BinaryFormatter Example

start example
 Imports System Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Binary <Serializable()> Class SerializableClass   Private value As Integer   Public Function GetValue() As Integer     GetValue = value   End Function   Public Sub SetValue(ByVal value As Integer)     Me.value = value   End Sub End Class Public Module modMain Public Sub Serialize()   Dim myObject As SerializableClass = _     New SerializableClass()   myObject.SetValue(888)   Dim stream As Stream = File.Create("C:\MyObject.bin")   Dim formatter As IFormatter   formatter = CType(New BinaryFormatter(), IFormatter)   formatter.Serialize(stream, myObject)   stream.Close() End Sub Public Sub Deserialize()   Dim stream As Stream = File.OpenRead("C:\MyObject.bin")   Dim formatter As IFormatter   formatter = CType(New BinaryFormatter(), IFormatter)   Dim myObject As SerializableClass   myObject = CType(formatter.Deserialize(stream), SerializableClass)   stream.Close()   Console.WriteLine(myObject.GetValue) End Sub Public Sub Main()   Serialize   Deserialize End Sub End Module 
end example

The class is very simple, containing only one private field called value. There are two methods, GetValue and SetValue, respectively, to obtain and set the private field value. The code also provides two methods, Serialize and Deserialize. The Serialize method will persist an instance of SerializableClass into a file called MyObject.bin. The Deserialize method does the reverse, reading the SerializableClass object from the file and then printing its value.

The SoapFormatter Class

This is the only member of the System.Runtime.Serialization.Formatters.Soap namespace. You use this class to serialize and deserialize an object or a graph of objects in XML format.

Listing 4-12 shows an example that uses the SerializableClass class from the previous example. This class is marked for serialization. The class is very simple, containing only one private field called value. There are two methods, GetValue and SetValue, to obtain and set the private field value, respectively.

Listing 4-12: SoapFormatter Example

start example
 Imports System Imports System.IO Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Formatters.Soap <Serializable()> Class SerializableClass   Private value As Integer   Public Function GetValue() As Integer     GetValue = value   End Function   Public Sub SetValue(ByVal value As Integer)     Me.value = value   End Sub End Class Public Module modMain Public Sub Serialize()   Dim myObject As SerializableClass = New SerializableClass()   myObject.SetValue(678)   Dim stream As Stream = File.Create("C:\MyObject.xml")   Dim formatter As IFormatter   formatter = CType(New SoapFormatter(), IFormatter)   formatter.Serialize(stream, myObject)   stream.Close() End Sub Public Sub Deserialize()   Dim stream As Stream = _     File.OpenRead("C:\MyObject.xml")   Dim formatter As IFormatter   formatter = CType(New SoapFormatter(), IFormatter)   Dim myObject As SerializableClass   myObject = CType(formatter.Deserialize(stream), SerializableClass)   stream.Close()   Debug.WriteLine(myObject.GetValue) End Sub Public Sub Main    Serialize()    Deserialize() End Sub End Module 
end example

The code provides two methods, Serialize and Deserialize. The Serialize method will persist an instance of SerializableClass into a file called MyObject.bin. The Deserialize method does the reverse, reading the SerializableClass object from the file and then printing its value.




Real World. NET Applications
Real-World .NET Applications
ISBN: 1590590821
EAN: 2147483647
Year: 2005
Pages: 82

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