Solved

serialize and deserialize, is it really necessary?

Posted on 2010-11-23
8
567 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
Comment Utility
I could not understand exactly what your question is.
0
 

Author Comment

by:enrique_aeo
Comment Utility
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
Comment Utility

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
 
LVL 21

Expert Comment

by:Alpesh Patel
Comment Utility
It is useful when you are passing object over network to improve performance. Because serialization reduce the use of bandwidth.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 166 total points
Comment Utility
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
Comment Utility
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
Comment Utility
the file with method fentListarBandejaWFGDH
2-fentListarBandejaWFGDH.txt
0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 334 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now