Solved

serialize and deserialize, is it really necessary?

Posted on 2010-11-23
8
569 Views
Last Modified: 2012-05-10
I have a method in which sets the value of an object with some properties objBandeja
1. Serialize this object: strobjBandeja = CFunciones.SerializeObject (Of EBandeja) (objBandeja), whose code is
public static string SerializeObject<T>(T obj)
        {
            try
            {
                string xmlString = null;
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, System.Text.Encoding.UTF8);
                xs.Serialize(xmlTextWriter, obj);
                memoryStream = (System.IO.MemoryStream)xmlTextWriter.BaseStream;
                xmlString = UTF8ByteArrayToString(memoryStream.ToArray());
                return xmlString;
            }
            catch
            {
                return string.Empty;
            }
2. The following line of code is this
objEGeneral = WFGDHUtilitario.DeserializeObject(Of EGeneral)(oLSolicitud.fentListarBandejaWFGDH(strobjBandeja))

in which sending the serialized object. FentListarBandejaWFGDH method code is on file: 2 fentListarBandejaWFGDH.txt
3. analyzing the code does the following
3.1 deserialize the xml variable objEBandeja = CFunciones.DeserializeObject (Of EBandeja) (pstrOBJEBandeja)
3.2 gets the value of a property and that value calculates the value of some variables that will be sent to a stored procedure

MY QUESTION IS, in this case in order to serialize and deserialize is to calculate the parameter of the stored procedure?
0
Comment
Question by:enrique_aeo
8 Comments
 
LVL 5

Expert Comment

by:buraksarica
ID: 34203187
I could not understand exactly what your question is.
0
 

Author Comment

by:enrique_aeo
ID: 34204324
do not understand why serialize and deserialize. I understand the concept but I do not understand its use
0
 
LVL 16

Accepted Solution

by:
kris_per earned 334 total points
ID: 34204911

I think Serialize/Deserialize is used to create another copy of the object objBandeja. If some operations change the internal state of an object and if you dont want keep this change in the object, then the way is to create a copy of the object and do the operation on the copy; so internal changes will be only in the copy and first object will remain the same.

In your question:
step 1: serialize orginal object to get it as xml string
step 3.1: deserialize the xml string to get an object which will be a copy of the original object
step 3.2: operation in this step I think changes some value in the second object.

Thus by doing step 3.2 on the second object, the original object remain unchanged I think which is the purpose.

See if this fits.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34205113
It is useful when you are passing object over network to improve performance. Because serialization reduce the use of bandwidth.
0
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 166 total points
ID: 34207745
In this case, the purpose of deserialization is most likely the transfer of the data object between two dissimilar applications or systems, or incompatible languages. Since the object can be represented in xml, the provider or client simply provides it in a textual representation of the class.

The consuming application simply deserializes the stream back into an object that it can consume (which should implicitly validate the properties based on type) and has probably implemented some validation as well (and as you say, calculations in the code based on other properties) before it calls a stored procedure on the database. This allows them to re-use the same class in multiple locations in their code without having to write a parser and without the overhead of the XML DOM.

"I think Serialize/Deserialize is used to create another copy of the object "
That would appear to be a particularly expensive way of creating a copy. Simpler to implement an ICloneable interface and copy the properties in memory.

"It is useful when you are passing object over network to improve performance"
Not with XML serialization - it's effectively string parsing, which is notoriuosly slow. For a performant solution, binary would be significantly better.
0
 

Author Comment

by:enrique_aeo
ID: 34234462
Thanks experts, i travel and recently can respond. Their responses are very interesting, i am atachando the method that lack, with that we are going to have a better idea, i hope their support.
0
 

Author Comment

by:enrique_aeo
ID: 34234468
the file with method fentListarBandejaWFGDH
2-fentListarBandejaWFGDH.txt
0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 334 total points
ID: 34238831
This function does the following:

   Deserialize the xml string parameter to get objEBandeja(EBandeja type) object.

   Use the Filtro member of objEBandeja object to use in sql parameters in the stored proc.

   Results of stored proc is stored in objEGeneral(EGeneral type) object.

   Serialize the objEGeneral object to get xml string and return xml string.


I think the reason why it uses Deserialize/Serialize depends on the requirement of the caller and what input the caller has.

If the caller already has objEBandeja(EBandeja type) object, then this function can be changed to take objEBandeja(EBandeja type) object as parameter; then there is no need to do the first Deserialize.

If the caller needs the return result as objEGeneral(EGeneral type) object, then the function can just return objEGeneral(EGeneral type) object instead of xml string; then there is no need for the final serialize.

It looks this function is a helper fuction for the caller to suit its need. I dont see any other special use here (like to create a copy or to transfer the object across network or systems)

Hope this helps.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question