Serialization
Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache. Serialization is the technology that enables an object to be converted into a linear stream of data that can be easily passed across process boundaries and machines. This stream of data needs to be in a format that can be understood by both ends of a communication channel so that the object can be serialized and reconstructed easily.
If you are using Custom Objects to store data, you can maintain state of the object across postbacks by storing the object in the ViewState.
To do this your object should be Serializable.
Also, at times, if you want to store data temporarily, it is easier to serialize your object into xml and store it rather than storing it in db.
Following namespaces are involved in serialization process :
- System.Runtime.Serialization
- System.Runtime.Serialization.Formatters.Binary
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
public class SerialTest
{
public void SerializeNow()
{
ClassToSerialize c=new ClassToSerialize();
File f=new File("temp.dat");
Stream s=f.Open(FileMode.Create);
BinaryFormatter b=new BinaryFormatter();
b.Serialize(s,c);
s.Close();
}
public void DeSerializeNow()
{
ClassToSerialize c=new ClassToSerialize();
File f=new File("temp.dat");
Stream s=f.Open(FileMode.Open);
BinaryFormatter b=new BinaryFormatter();
c=(ClassToSerialize)b.Deserialize(s);
Console.WriteLine(c.name);
s.Close();
}
public static void Main(string[] s)
{
SerialTest st=new SerialTest();
st.SerializeNow();
st.DeSerializeNow();
}
}
public class ClassToSerialize
{
public int age=100;
public string name="bipin";
}
Explanation
Here we have our own class named ClassToSerialize. This class has two public valiables name and age with some default values. We will write this class to a disk file (temp.dat) using SerializeTest class.
SerializeTest class has two methods SerializeNow() and DeSerializeNow() which perform the task of serialization and deserialization respectively.
The general steps for serializing are :
- Create an instance of File that will store serialized object.
- Create a stream from the file object.
- Create an instance of BinaryFormatter.
- Call serialize method of the instance passing it stream and object to serialize.
The steps for de-serializing the object are similar. The only change is that you need to call deserialize method of BinaryFormatter object.
Now, let us see an example where we have used 'real' class with public and shared members and properties to encapsulate them. The class also uses another supporting class. This is just to make clear that if your class contains further classes, all the classes in the chain will be serialized.
refer :
***
Serialize .NET objects to store and retrieve them from a file or viewstate in ASP.NET
Everything is .NET is an object. But sometimes you need to store an object to a file, ASP.NET's viewstate etc. then get it back from the file or viewstate and continue to use it as the object it originally was.
***
The following are the basic advantages of serialization: · Facilitate the transportation of an object through a network · Create a clone of an object The primary disadvantage of serialization can be attributed to the resource overhead (both the CPU and the IO devices) that is involved in serializing and de-serializing the data and the latency issues that are involved for transmitting the data over the network. Further, serialization is quite slow. Moreover, XML serialization is insecure, consumes a lot of space on the disk and it works on public members and public classes and not on the private or internal classes. Therefore, it compels the developer to allow the class to be accessed to the outside world. Serialization can be of the following types: · Binary Serialization · SOAP Serialization · XML Serialization · Custom Serialization refer : http://aspalliance.com/983_Introducing_Serialization_in_NET.allAdvantages and Disadvantages of Serialization Types of Serialization
0 Response to "Serialization"
Post a Comment