Advice on deserializing
Posted on 2006-07-07
I have a class representing a company that looks something like the below. It has the code for the company, its name, address, 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:
<Name>cne name here</Name>
<Address1>802 UNIT 403 PUCHON TECHNOPARK</Address1>
<Address2>193 YAKDAEDONG WONMIKU </Address2>
<StreetAddress>802 UNIT 403 PUCHON TECHNOPARK</StreetAddress>
<City>193 YAKDAEDONG </City>
<CompanyCode>ABC</CompanyCode> <!-- remember, this code is NOT in the DB -->
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?