Link to home
Start Free TrialLog in
Avatar of jasww
jasww

asked on

Advice on deserializing

Hi
I have a class representing a company that looks something like the below.  It has the code for the company, its name, address, etc.

Object.CompanyCode
Object.Name
Object.Address
Object.Email
etc

In the SET code for the CompanyCode property, I take the company code that the consumer of the class has assigned, and look-up the name and address fromt he DB.  (the aim being that the web front end that will use this class can simply ask the user for a code and pull out the details).

THis is great.  

They can type in a company code, we try to find the details, and if nothing is found the user can type in the name and address, overwriting the missing details from the DB.  (note we do not want to save this data)


The problem arises however when I have serialized an instance of this class and then try to deserialize it. Suppose the code the user typed as ABC and we didn't find a company.  The user went on to supply the name and address.  These details were serialized to XML. Grand, all ok so far. Our xml looks like:


        <Consignee >
          <Name>cne name here</Name>
          <Address>
            <Address1>802 UNIT 403 PUCHON TECHNOPARK</Address1>
            <Address2>193 YAKDAEDONG WONMIKU </Address2>
            <Address3>PUCHON KOREA</Address3>
            <Address4 />
            <StreetAddress>802 UNIT 403 PUCHON TECHNOPARK</StreetAddress>
            <City>193 YAKDAEDONG  </City>
            <State>PUCHON </State>
            <Zip >123</Zip>
            <Country>KR</Country>
          </Address>
          <Telephone />
          <Email>0</Email>
          <CompanyCode>ABC</CompanyCode>  <!-- remember, this code is NOT in the DB -->
        </Consignee>


Now when I deserialise, the SET block for the CompanyCode  property is called, and we try to find ABC in the DB.  Nothing is found, so the name, address, email etc are overwritten with blanks.  Thus the object we load has a code but no name or address, despite the name and address being in the source XML.

Thus I am losing data.


Do you have any advice on how to do this in a better way?  Is there a way that the CompanyCode SET property can tell that it is being called as part of a deserialization, and thus not perform the lookup?


Thanks
Daniel
-jasww

ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasww
jasww

ASKER

Great, that trick did the job.  I set the flag to false in the default  constructor, and in the overloaded constructor that I use I set the flag to true.

Cheers!