Solved

Advice on deserializing

Posted on 2006-07-07
2
323 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 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