Serialization and Inheritance/Overriding problem

  • Thread starter Thread starter Mark Sizer
  • Start date Start date
M

Mark Sizer

Hi people,

I'm having trouble trying to achieve something in C# and win forms, and am
looking for a little advice if anyone has a moment.

I've got two classes:

1) a base class
2) a derived class that inherits from it

The base class is designed to hold data and only data.

The derived class (which inherits the base/data) class is designed to have
more of a presentation role. It performs calculations on the data in the
base class and offers out manipulated data derived from the data in the base
class. The manipulated data is often offered through overridden properties.

That all works fine so far, now for the part I'm having problems with.

I'd like to be able to serialize both classes out as xml.

So the base class can save its data to file in xml format (and it should
save only its data, none of the presentation data).

Then the derived class can save its data to xml also, for me to match up
with an xslt file and present the data in a web brower.

The problem is, keeping the two sets of properties from the two classes
(base data and presentation data) separate and only serializing out the
fields required for the class in question.

If for example I have an object in memory as the derived class, then when I
serialize it to disk, it serializes the data from the base properties and
the data from the presentation/derived/overridden properties.

If instead I have the object in memory as the base class, then it serializes
fine, but I cannot of course access the presentation properties as they
don't exist on the base class.

Incidentally, I'm pretty confident that I've gone the right path with the
class/inheriting/serialize route for data/presentation. I'm just having
trouble getting over the final hurdle.

The only solutions that i've can think of so far is, custom attribute tags
combined with custom serialization. However, I will want to do the same
thing with several other very different classes, and it seems like one heck
of a lot of work. Can anyone suggest an easier solution?

Cheers,

Mark.
 
Hi Mark,

Thanks for posting here.

I will do some research on this issue and will get back to you as soon as
possible.

Have a nice day!

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Mark,

Do you mean that you would like to serialize the "presentation" related
properties of the derived class but do not want to serialize the "data"
related properties?

We usually use the "System.Xml.Serialization.XmlIgnoreAttribute" to achieve
this. If you do not like it, maybe we can use a wrapper class to wrap the
derived class and hide its "data" properties. Would you consider this a
workaround?

On the other hand, I am thinking about another idea. We can also use
"encapsulation" instead of "inheritance" here. For example, we do not use
the derived class at all. Instead, we create a wrapper class for the base
class directly and use the wrapper class to deal with those presentation
works.

I hope this makes sense to you.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Felix,

Thanks for taking the time to look into this for me and reply.

I would ideally like to serialize the properties on both the presentation
class and the data class. The aim being to serialize the data ones for
efficient saving to disk (in a .xml file) and to serialize the presentation
ones also to an .xml file but for transformation with an .xslt file for web
browser viewing.

I have been making use of the "System.Xml.Serialization.XmlIgnoreAttribute"
so far. Admittedly it took me three hours to work out why the
"NonSerialized" attribute wasn't working, and to switch over to the xml
equivalent, but I'm sure we can all be a little slow at times.

I think that your suggestion of encapsulation within a wrapper class might
be the direction that I head in. It will unfortunaely break the nice
elegant inheritance chain that I was slowly building up, but then not all
programming solutions can be 'nice' and 'elegant' if they are going to work
properly.

Thanks for the suggestion I'll give it a try.

Cheers,

Mark.
 
Hi Mark,

Thanks for your reply. I agree with you that it is quite difficult to write
"nice" and "elegant" programs. It is also important to keep the programs
simple and easy to reuse.

Should you have any concerns or new findings regarding this issue, please
feel free to post here.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top