How Serialization Works

Team Fly 

Page 60

How Serialization Works

Let's say you have a class named Person, which stores information about persons (customers, contacts, and so on). The Person class obviously exposes properties such as Name, Address, PhoneNumber, and so on. Depending on the application, the Person class may store a person's date of birth, a customer's credit limit, or just about any property you will need in your code. For each person you want to manipulate in your code, you'll create an instance of the Person class and populate this instance with different data. Each instance of the Person class is a Person object and its state is determined by the values of its properties. To persist an object of the Person type, you need only save the values of its properties—this is what serialization is all about. The class may also expose methods that act on the data, but code is not serialized.

To read back the persisted values into the same application, you simply create new instances of the Person class and populate them with the values of the serialized properties. Once all properties and fields have been assigned values, you have an object that's identical to the original one (the object you serialized) and you can call any of the object's methods to manipulate them.

Assuming that the application that deserializes the object has access to the code of the Person class, it's almost trivial to serialize and deserialize objects. You create an instance of the appropriate Serializer class and call its Serialize and Deserialize methods. Serialized objects can also be used by applications that don't have access to the code of the class that produced the objects. The remote application can't recreate identical objects, but it can use the serialized data to reconstruct an object with a similar structure. Of course, the remote application will never call the methods of the original class, because it can't access them. As you will see, it's possible to serialize an object in XML format. XML contains a description of the object's values, making it possible to create a new class with similar properties. For example, the remote application can create a Customer class, which can read some of the serialized properties (it can use the Name and Address properties, say, but skip the birth date). When you serialize objects in XML, the resulting document holds not only data, but its structure as well. Another application can take advantage of the self-descriptive nature of the XML document and reuse the data.

Serialization Types

There are three types of serialization: binary serialization, SOAP serialization, and XML serialization. Binary and SOAP serialization are very similar; XML serialization is a little different, but it allows you to customize the serialization process. Binary serialization is performed with the Binary-Formatter class and it converts the values of the object's properties into a binary stream. The result of the binary serialization is very compact and the serialization/deserialization process is as fast as it can get. However, binary serialized objects can be used only by applications that have access to the code of the class that produced the objects and can't be used outside .NET. Another limitation of binary serialization is that the output it produces is not human-readable and you can't do much with a file that contains a binary serialized object without access to the original class's code. Because binary serialization is very compact and very efficient, it's used almost exclusively to persist objects between sessions of an application, or between applications that share the same classes. For example, you can create an ArrayList of Person objects, serialize them to a file and reload the collection of the serialized objects from the file in a later session of the same application.

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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