Solved

Advice on deserializing

Posted on 2006-07-07
2
321 Views
Last Modified: 2010-04-16
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

0
Comment
Question by:jasww
2 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 17059300
This is one of the big problems with using setter level validation (you never know the circumstance of the set and you may not want validation!). A trick I have used in the past ...

public class foo {
   private bool validate;

   public int something {
       get { ... }
       set { if(!validation || validate(value)) { internal = value; }}

   public foo() {
       validate = false;
   }

   public bool foo(bool _validate) {
        validate = _validate;
   }

since the default constructor is called by serialization you work around having validation called on your setters.

In general this is not a great way of doing things but can be a nice stop gap solution. In the long run you probably want to change your validation structure to work in a more flexible manner. Jimmy Nilsson has a great discussion on the very problem when discussing the specification pattern in Applying Domain Driven Design and Patterns. Rocky also uses a similar pattern is CSLA which is worth a read up on.

You can also implement custom serialization which will alleviate this problem.

Cheers,

Greg
0
 

Author Comment

by:jasww
ID: 17059370
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!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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