Solved

Advice on deserializing

Posted on 2006-07-07
2
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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