Saturday, March 24, 2012

why serializing class?

Hi,

i read several articles about serialization. I know now that it 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 and that the serializable tag before the class makes the class
serializable.

Now i did some tests in order to understand it better, based on a example i found in on internet".
I first executed the code below (this (summarized) code produces a virtual simple shopping cart which is put in the Profile of the user) with the
attribute "<Serializable()>" and with what follows in web.config:
<profile><properties>
<add name="myCart" serializeAs="Binary" type="eCommerce.elist"/>
</properties></profile>

After that, i executed it again without the attribute "<Serializable()>" and without "serializeAs="Binary" from web.config.

I couldn't notice any difference (nor in table 'Profiles', nor in the table 'Orders' where the orders are put, nor in the shopping cart, nor in speed
, nor in CPU ..).

So my question is:
------
what happens (physically on the client/server computer) in this present application when using serialization, what not (physically) happens when not
using it?
With other words: what's the practical difference in this application between using <serialisation> _ and not using it? In both cases, data comes into the sql server table and in both cases, data are retrieved when needed.I tested it.
Is the difference the way data is put (and retrieved) into (from) the table (with serialization: binary stream, without:?) Or is the attribute
<serialisation> _ just ignored when using Profiles, or ...?

Demonstrate me that using <serialisation> _ is better than not using it.

I thank you in advance
Bob.

The (part of) code:
------

<Serializable()> _
Public Class listitem
Private _description As String


Public Sub New()
End Sub


Public Property description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
End Class
'--------
<Serializable()> _
Public Class elist
...
Public Sub New()
_items = New List(Of listitem)
End Sub
....
Public Sub Insert(ByVal Price As Decimal, ByVal description As String)
...
End Class

Apply theSerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have theSerializableAttribute attribute applied.

It's an attribute that extends your Class with the Methods required to Serialize it. Without this attributes the methods cannot be found and therefore not serialized.

So without serialization, the conversion cannot work. I don't know why it worked for you, maybe some caching or something else VB.NET and/or the config file and/or your Database Provider does.

Cheers,
Matthias :)


Hi Matthias,

thanks for replying.

you say: "So without serialization, the conversion cannot work"

but maybe there is no conversion at all. Maybe is the data put into the tables in the 'normal' way? Is that possible?


Counterquestion: How'd you convert an object to string without ToString or casting? And as "binary serialized data" is not a native datatype in CLI's ECMA-335 3rd Edition, and this conversion has to be done by somebody or something, I find it pretty hard to believe this is what the CLI's devs would call the 'normal' way :)

It's just a way to save the current object's state. This saving is done by a formatter. It's not a image of the current representation in memory. It's a formatted, reusable binary representation of your object. Binary serialization is just one way to maintain your objects state in memory. Two other ways are XML Serialization and SOAP Serialization. The "normal" way of saving a object does not exist (afaik). I don't know everything about .NET and CLI, and I'm open to conviction, but I don't think there is a normal way of saving an object that is interpreted by a runtime enviroment.

Cheers,
Matthias :)


What i would like to know is:

is serialization required to put the shopping cart content in the Profiles table of the database?

If not, how then is it put into the table?


It depends on how you save the content. If you use a standard-query to insert the fields in the database you are saving the values (i.e. the column Name = 'GeForce', Prize = '200'...). If you'd use Serialization you'd use one field in the database to store the complete object (column ShoppingCard='00-00-01...').

You have to follow the Design Guidelines for your Project. If you have influence on the DB-Design: Db-Fields are prettier imho... You don't have to pay the costs for serialization and you can use the full set of functionality of your Database.

0 comments:

Post a Comment