Solved

Advice on deserializing

Posted on 2006-07-07
2
320 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

9 Experts available now in Live!

Get 1:1 Help Now