this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Monday, May 3, 2010

Serialization with .net (Part -1)

Serialization is the process to stream an object, after that store it to file, or transfer in network. Now a days there are so many requirements that we need to store object in file or we want to pass our object to some application running on remote location, even some time requirement exists to consume object in another application which is written in languages other than supported by .net framework.

Microsoft .net provided a very nice way to serialize object with just a few lines. For serialization you need to add System.Runtime.Searlization in your project.

Microsoft .net provides many types of serialization process like Binary, Soap, XML or custom serialization. Each type of serialization stands at their own place depends on requirement we can use any of the supported serialization.

We can serialize any of the data of .net primitive type or our class created by user.

Let’s check how it can be done.

Whenever you are clear that my serialization will be occurred between applications written in .net, you can use binary serialization. Binary serialization is very efficient and takes less space compared to other serialization method.

I have scenarios to store my string data to file.

àCreate an instance of stream class, where you will store your string data.

FileStream fs = new FileStream("Serialized.Data", FileMode.Create);

àI have string variable

string myData = “Hi! This is my string data”;

àI want to use Binary serialization, so I need to add System.Runtime.Serialization.Formatters.Binary and create an instance of it.

BinaryFormatter bf = new BinaryFormatter();

àCall Serialize method of BinaryFormatter object.

bf.Serialize(fs,myData);

Now if you open this “Serialized.Data” file you can able to see myData variable’s content in it, there might be some other binary data will be also added ,which is useful when we deserialized object.

At the time of deserialization create instance of BinarayFormmter and call deserialized method.

àdeserialization can be done by..

myData = (String) bf.Deserialize(fs);

Above example shows when we use primitive data type, what about class. We can do same thing with it, but we need to take care of something when creating class.

To serialize class object add [Serializable] attribute to class. So when ever we do serialize of that class all public and private members will be serialized. Some time we have calculation field in our class and so its not required to serialized it.

At that time we can add [NonSerialized] attribute to calculated field of class.

But when we deserialize object we don’t have calculated field value so at that we require it, we can do that calculation when object is deserialized.

Inherits class with IDeserializationCallback and implement OnDeserialization method, this method will be called exactly when our class is deserialzed, we need to call it explicitly.

Some time we face, version problem, say for example we have stored object earlier in file, and later on we added some new fields to our class so it will not find those newly added field when we deserialize object.

In that situation we can use [OptionalField] attribute to our new fields or members added to class once object is serialized. At the time of deserialize we can assign some default value to this newly added field at IDeserializationCallback’s OnDeserialization method.

Same thing can be done with SoapFormatter class instead of BinaryFormatter but it takes more space. When we want to pass object across network and firewall, SoapFormatter is better to use.

No comments: