object serialization.

G

Guest

Hi,
I'm new to .net. I need some info on serialization.

What is serialization? Why do we need it? Why objects need to be serialized
if they need to be stored in session or viewstate?
 
E

Eliyahu Goldin

From the MSDN: Serialization is the process of converting the state of an
object into a form that can be persisted or transported.

Objects need to be serialized in order to be stored in persistent locations
like session or viewstate. If your object is a string, its serialization is
trivial. If it is a car, serialization is not trivial. What to store depends
on your application requirements. It could be a set of general properties
like make, year, color if you are a dealer, or the detailed list of all
parts if you are on the manufacturer's side, or maintenance history if you
are a garage manager.

Eliyahu
 
J

Johann MacDonagh

In the basic sense, serialization is the process in which an instance of a
class (an object) is converted into a text string. This is necessary because
sometimes you want to keep rich instances of classes, but you can only store
it in a certain format. Serialization provides a quick way of converting
back and forth.

ViewState stores a collection of objects that maintain their state across
postbacks. The way that ASP.NET accomplishes this is by outputting a hidden
form field named __ViewState to each page it renders (you can see for
yourself). Within the value attribute of that hidden form field contains a
set of objects that make up ViewState.

If you want a rich object to participate in ViewState, you must mark it as
serializable, applying the Serializable attribute to your class.

Hope this helps,
Johann MacDonagh
 
A

Anders Norås [MCAD]

What is serialization? Why do we need it? Why objects need to be
serialized
if they need to be stored in session or viewstate?
Serialization is the process of converting an object to a data stream. The
process consists of two phases serialization and deserialization. You can
compare these phases to dehydration and rehydration of food.
When an object is serialized, or dehydrated, the state information is
extracted. The common elements (think of water), such as methods are not
included in the data stream. When the object is deserialized, or rehydrated,
a new instance of the object is created and the state is restored form the
serialized data.

You have two important types of serialization; binary and XML. The easiest
to explain is XML serialization. When serializing an object to XML the
member names map directly to the XML element names. Consider the following
object instance.
Person
FirstName (string) "Anders"
LastName (string) "Norås"
Age (int) 28
SayHello (void) (method)

When serialized, the Person instance should result in the following XML
document.
<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<FirstName>Anders</FirstName>
<LastName>Norås</LastName>
<Age>28</Age>
</Person>

The SayHello method is not part of the object state and hence it is not
included in the XML document.

ViewState stores all objects as text, therefore an object must be serialized
to a text string before it can be stored in ViewState.

Anders Norås
http://dotnetjunkies.com/weblog/anoras
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top