Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2006

Tip: Looking for answers? Try searching our database.

Inheritied Serializable Classes (OOP Design)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Techno_Dex - 16 Oct 2006 23:02 GMT
I have base classes defined in a hierarchy structure which all have
XMLElementAttributes on them and marked as Serializable.  I then have
classes which inherit from the base classes and hide some of the base class
properties using the "new" keyword in order to redefine the schema.  Every
time I go to serialize the object I get exceptions about hiding base class
member X of type Y.  Use XmlElementAttribute or XmlAttributeAttribute to
specify a new name.  What do I need to do to overcome this in the serializer
or is this an MS limitation in the XML Serializer???  The code compiles fine
and works great as a standard C# OOP design but barfs all over the place
when it comes to inheritance.

Ex

[XmlRoot("Foo")]
public class BaseClass
{
    private int miCount;

    [XmlElement("Count", Namespace="BaseClass")]
    public int Count
    {
         get {return miCount;}
         set {miCount = value;}
    }
}

[XmlRoot("Bar")]
public class SubClass : BaseClass
{
    private int miCount;

    [XmlElement("Count", Namespace="SubClass")]
    public new int Count
    {
         get {return miCount;}
         set {miCount = value;}
    }
}
John Saunders - 17 Oct 2006 17:13 GMT
>I have base classes defined in a hierarchy structure which all have
>XMLElementAttributes on them and marked as Serializable.  I then have
[quoted text clipped - 6 lines]
>compiles fine and works great as a standard C# OOP design but barfs all
>over the place when it comes to inheritance.

I don't know the answer to your problem, but I'd disagree with your
characterization of this technique as "a standard OOP design". I'd question
why you have a property called Count in the derived class which means
something different than Count in the base class? Perhaps you meant for
Count to be virtual?

Do you understand that your SubClass has two "Count" properties and two
"miCount" fields? This is not polymorphic.

John
Techno_Dex - 17 Oct 2006 21:48 GMT
Bad example on my part as I used a simple type consisting of ints.  I was
trying to keep is as simple as possible but that isn't going to work in this
case.  Instead of using ints I have other custom classes which are defined
and returned.  These custom classes are either redefined in the XSD or
extended in the XSD so the example holds true for what I was intending but
the delivery of the example was bad.  The intended purpose is to hide the
base Implementation of ObjectA (thus the new keyword) and replace it with
ObjectB which is defined to extend ObjectA while at the same time reusing
the XmlElement name.

So far from what I can tell it appears that I can't hide a base XmlElement
and might have to use an XmlAttributes and XmlAttributeOverrides, but I'm
not seeing how this works with an XmlElement only XmlAttributes, XmlEnums,
etc....

namespace A
{
 [XmlRoot("Foo")]
 public class BaseClass
 {
    private ObjectA miCount;

    [XmlElement("Count", Namespace="BaseClass")]
    public ObjectA Count
    {
         get {return miCount;}
         set {miCount = value;}
    }
 }

 public class ObjectA
 {
    [XmlAttribute("FavNum")]
    public int FavoriteNum
   {
       get;
       set;
    }
 }

}

namespace B
{
 [XmlRoot("Bar")]
 public class SubClass : A.BaseClass
{
    private ObjectB miCount;

    [XmlElement("Count", Namespace="SubClass")]
    public new ObjectB Count
    {
         get {return miCount;}
         set {miCount = value;}
    }
 }

 public class ObjectB : A.ObjectA
{
    [XmlAttribute("FavLetter")]
   public int FavoriteLetter
   {
       get;
       set;
    }
 }

}

>>I have base classes defined in a hierarchy structure which all have
>>XMLElementAttributes on them and marked as Serializable.  I then have
[quoted text clipped - 17 lines]
>
> John
John Saunders - 17 Oct 2006 22:59 GMT
> Bad example on my part as I used a simple type consisting of ints.  I was
> trying to keep is as simple as possible but that isn't going to work in
[quoted text clipped - 5 lines]
> replace it with ObjectB which is defined to extend ObjectA while at the
> same time reusing the XmlElement name.

This is where your design doesn't seem very object-oriented. If ObjectB
extends ObjectA, do you intend that code should be able to refer to an
instance of ObjectB as though it were an ObjectA? In other words, are you
trying for polymorphism?

Or, are you just using implementation inheritance, in which case I need to
remind you that C# doesn't have private inheritance like C++ does. For
instance, in C++ you could do:

public class ObjectB : private ObjectA /* Please excuse any bad syntax. It's
been a while */

In the above case, no user of ObjectB could ever detect that it based some
of its implementation on ObjectA. But in C#, you have

public class ObjectB : ObjectA {}

In this case, there is no way to hide from a caller the fact that ObjectB is
based on ObjectA.

In particular, if you define Count in both classes, there really are two
separate and independant "Count" properties, and code can use them both:

ObjectB ob = new ObjectB();
int cb = ob.Count;
int ca = ((ObjectA) ob).Count;

If "Count" were virtual, then the two assignments above would be equivalent.

John
Techno_Dex - 18 Oct 2006 18:03 GMT
Possibly both need to be available.  As for the intention of the example I
have given, I'm looking for Inheritence and hiding.  The XSD is redefining
the ObjectA to extend it, which results in ObjectB.

Both of these need to be available
BaseClass oBC = new BaseClass();
oBC.Count.FavoritNum = 43;

which generate XML Serialized output of
<Foo>
 <Count FavNum=43 />
</Foo>

SubClass oSC = new SubClass();
oSC.Count.FavoriteNum = 60;
oSC.Count.FavoriteLetter = 1;

which generate XML Serialized output of
<Bar>
 <Count FavNum=60 FavLetter=1 />
</Bar>

>> Bad example on my part as I used a simple type consisting of ints.  I was
>> trying to keep is as simple as possible but that isn't going to work in
[quoted text clipped - 37 lines]
>
> John
John Saunders - 18 Oct 2006 23:37 GMT
> Possibly both need to be available.  As for the intention of the example I
> have given, I'm looking for Inheritence and hiding.  The XSD is redefining
[quoted text clipped - 17 lines]
>  <Count FavNum=60 FavLetter=1 />
> </Bar>

What happened to BaseClass.Count?

Perhaps you could post your schema? I bet you're using derivation by
extension in a way that doesn't map to OOP.

John
Techno_Dex - 19 Oct 2006 17:01 GMT
BaseClass.Count (which returns something of type ObjectA) was redefined to
be SubClass.Count (which returns something of type ObjectB which inherits
from and extends ObjectA)

The schema looks something like the following

<xsd:schema xmlns:xsd="">
 <xsd:complexType>
   <xsd:sequence>
     <xsd:element name="Foo">
       <xsd:attribute name="Count" type="int" />
     </xsd:element>
   </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

<xsd:schema xmlns:xsd="">
 <xsd:redefine schemalocation="BaseClass.xsd">
   <xsd:complexType name="Bar">
     <xsd:complexContext>
       <xsd:extension base="Foo">
           <xsd:attribute name="FavLetter" type="int" />
       </xsd:extension>
     </xsd:complexContext>
   </xsd:complexType>
 </xsd:redefine>
</xsd:schema>

>> Possibly both need to be available.  As for the intention of the example
>> I have given, I'm looking for Inheritence and hiding.  The XSD is
[quoted text clipped - 24 lines]
>
> John
John Saunders - 20 Oct 2006 12:15 GMT
> BaseClass.Count (which returns something of type ObjectA) was redefined to
> be SubClass.Count (which returns something of type ObjectB which inherits
[quoted text clipped - 23 lines]
>  </xsd:redefine>
> </xsd:schema>

Yeah, like I said, this doesn't map to a standard OO paradigm in a
strongly-typed language. There are many parts of XSD which don't map that
way.

C#, for instance, doesn't support covariant return types (at least not in
1.1), which is kind of what you are describing in your text.

OTOH, although I don't use xsd:redefine, is that really what you're defining
above? Or, are you adding the FavLetter attribute and not removing Count?

John
Techno_Dex - 25 Oct 2006 16:19 GMT
It could be both.  I hoping to develop some possible inheritance using
classes so I wouldn't have to rewrite all the base code in every xsd
extension/redefinition.  In .NET 2.0 there appears to possibly be an XML
class which lets you override/replace existing elements and attributes in
the schema hierarchy but that looks to be painful.  I guess I will just have
to cut and paste code.  It's cleaner than writing a 5000 line method of
XmlWriteElement statements.

>> BaseClass.Count (which returns something of type ObjectA) was redefined
>> to be SubClass.Count (which returns something of type ObjectB which
[quoted text clipped - 36 lines]
>
> John

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.