Making your Objects Serializable – Part 1 (Overview)


Before I get ahead of myself, lets start by defining what serialization is.  In a nutshell its basically a way to convert your object into another form such as XML or binary.   There is also the opposite notion of DeSerialization which is the act of taking your object that is in XML or binary form into an actual .Net object.  Here is wikipedia’s definition of Serializatoin/DeSerialization.

The point of serialization is to store or transfer your objects in or through another medium.  Examples of this are taking your objects and turn it into XML format to be transfered over the internet through a WCF service.  Or another example is to store your object in binary format to be stored in a database or over a local network via network packets.  As it stands there is no way to tranfer your objects and maintain its form outside the .Net CLR.  And so Serialization is here to help with the mission.

Now you might be asking yourself, whats the big deal.  Its easy to Serialize my objects, isn’t it?  Well, for most purposes, all you have to do is stick a the Serializable or DataContract attribute over your class.

_
Public Class MyOjbect

End Class
or

_
Public Class MyOjbect

End Class
But the one thing about Serialization is that it only converts properties. Your functions, constructors, events, calculated properties, etc. will not get converted. And when you recieve one of your serialized objects on the other end of the wire, its much less functional than where it was initially created. Of course this is assuming you proxy your objects (WCF, Webservices, etc…). It is possible to deserialize to the actual objects assuming you have access to the code. Most of the time this isn’t the case.

So now that you understand what serialization is about, there are different ways that you can serialize your objects. The .Net library has a bunch of ways to do it, but the most common ways are XML Serialization, Binary Serialization & DataContract Serialization. The XML & DataContract serialization methods are very similar. One is strictly for adhering to the XML standard, the other does still convert your object to XML, but has more smarts behind it for transferring your objects over WCF and recalling those objects on the client retaining your class hierarchy such as any base classes. In fact, its smart enough to know your base class hierarchy and maintain this hierarchy via a proxy class. Binary serialization turns your object into 1’s and 0’s and maintains a perfect replica of your object in that form. Its great for storage and for high speed transactions. Storage in a database, file system, or even transferring over the network to enable a low bandwidth is where its strength lies.

I’ll be posting more on Serialization and where things can get hairy.  Look for more in the coming weeks!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s