Monday, April 30, 2012

What is Serialization in .NET, types of Serialization and why we need it while developing an application?


Serialization is the process of converting complex objects into stream of bytes for storage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form. The namespace which is used to read and write files is System.IO. For Serialization we are going to look at the System.Runtime.Serialization namespace. The ISerializable interface allows you to make any class Serializable.
Here are the following steps that we are going to do to create a serializable class and test it.
  • Create a custom class named Employee and assign properties.
  • Define the serialization functions.
  • Create a main class and instantiate our Employee class.
  • Serialize the object to a sample file.
  • Deserialize the values by reading it from the file.
Defining Employee class and properties
Our custom class Employee should be derived from the ISerializable interface and should hold the Serializable attribute. Here is the code snippet.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; namespace MyObjSerial
{
[Serializable()] //Set this attribute to all the classes that want to serialize
public class Employee : ISerializable //derive your class from ISerializable
{
public int EmpId;
public string EmpName;

//Default constructor
public Employee()
{
EmpId = 0;
EmpName = null;
}
}
}

Different types of serialization.
The different types of serialization are
·         Binary Serialization
·         XML Serialization
·         SOAP Serialization
Binary Serialization
Binary serialization is the process where you convert your .NET objects into byte stream. In binary serialization all the public, private, even those which are read only, members are serialized and converted into bytes. So when you want a complete conversion of your objects to bytes then one can make use of binary serialization.
XML Serialization
In XML serialization only the public properties and fields of the objects are converted into XML. The private members are not taken into consideration in XML serialization.
SOAP Serialization
Similar to XML serialization. When you serialize object to SOAP format it conforms to the SOAP specification.
Need of Serialization
Most uses of serialization fall into two categories: persistence and data interchange. Persistence allows us to store the information on some non-volatile mechanism for future use. This includes multiple uses of our application, archiving, and so on. Data interchange is a bit more versatile in its uses. If our application takes the form of an N-tier solution, it will need to transfer information from client to server, likely using a network protocol such as TCP. To achieve this we would serialize the data structure into a series of bytes that we can transfer over the network. Another use of serialization for data interchange is the use of XML serialization to allow our application to share data with another application altogether. As you can see, serialization is a part of many different solutions within our application.

1 comment: