Connecting Tech Pros Worldwide Help | Site Map

Serialization and interfaces

Daniel Faensen
Guest
 
Posts: n/a
#1: Nov 11 '05
As a good OO programmer that I hopefully am I prefer to implement against
interfaces rather than classes. This is especially useful when it comes to
multiple inheritance which is as you know an issue and not supported in
languages as Java or C# - for good reasons.

I have an object graph I want to persist using the XML serialization
features of the .NET framework. The interface/class hierarchy of my business
model resembles the following example that I extracted from the UML
specification:




ownedElements * ----------------
|------------>| ModelElement |
| ----------------
| ^ ^
| | |
| ---------- -----------
| | |
| ------------- ------------------------
|-<>| Namespace | | GeneralizableElement |
------------- ------------------------
^ ^
| |
--------- -----------
| |
---------
| Class |
---------

Since multiple inheritance is no problem with interfaces I can easily create
an interface hierarchy that corresponds to the business model above. (I
prefix interface names with the letter 'I'.)

For each of the iterfaces I have an implementing class that additionally
inherits from (extends) the implementing class of the parent interface.

Example:

IModelElement
^ ^
| |
| -----------------------
| |
| IGeneralizableElement
| ^
| |
ModelElementImpl |
^ |
| |
----------------------- |
| |
GeneralizableElementImpl

For the multiple inheritance case I decided that the Namespace features of
Class are less important than the features of GeneralizableElement.
ClassImpl therefore extends GeneralizableElementImpl. The features of
Namespace (e.g., the list of owned ModelElements) are realized by
aggregation and delegation. That is, ClassImpl has an aggregate association
to NamespaceImpl (essentially a role class). Each feature of INamespace
(properties as well as operations) is implemented by delegating the call to
the NamespaceImpl instance.

The ownedElements of Namespace are implemented as a collection of
IModelElement (the interface!) since beneath Classes there is a number of
other children of ModelElement that can be owned by a Namespace.

So far nothing special - this is a widely used design pattern.
The problem is when I try to serialize the root object (an instance of
ModelElement) I get an exception at runtime because the SoapSerializer fails
to serialize interfaces. This is documented flaw but I am sure somehow I can
get around it - I only don't now how.

Do you?

Thank you in advance

Daniel Faensen


Christoph Schittko [MVP]
Guest
 
Posts: n/a
#2: Nov 11 '05

re: Serialization and interfaces


Daniel,

I am assuming you are talking about the XmlSerializer, since there is no
such thing as a SoapSerializer in the .NET Framework.

Serializing Interface references is simply not support since the goal for
the XmlSerializer is to bind to strongly structured XML data compatible with
the type system defined by XML Schema. Since there is no notion of an
interface in Xml Schema, there is no need for to serialize and deserialize
interfaces. I have to somewhat agree with this, because an interface is not
a complete class. The serializer would have to embed extra information in
the generated Xml document in order to deserialize the full instance later
on. This extra information could easily invalidate documents against their
schema.

The only alternative you currently have if you need to serialize to Xml is
the SoapFormatter, which isn't as flexible as the XmlSerializer (which
should be called XsdDataBinder), but it produces enough information to
re-hydrate the serialized interface.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Daniel Faensen" <faensen@inf.fu-berlin.de> wrote in message
news:bjq3ab$lj0ao$1@uni-berlin.de...[color=blue]
> As a good OO programmer that I hopefully am I prefer to implement against
> interfaces rather than classes. This is especially useful when it comes to
> multiple inheritance which is as you know an issue and not supported in
> languages as Java or C# - for good reasons.
>
> I have an object graph I want to persist using the XML serialization
> features of the .NET framework. The interface/class hierarchy of my[/color]
business[color=blue]
> model resembles the following example that I extracted from the UML
> specification:
>
>
>
>
> ownedElements * ----------------
> |------------>| ModelElement |
> | ----------------
> | ^ ^
> | | |
> | ---------- -----------
> | | |
> | ------------- ------------------------
> |-<>| Namespace | | GeneralizableElement |
> ------------- ------------------------
> ^ ^
> | |
> --------- -----------
> | |
> ---------
> | Class |
> ---------
>
> Since multiple inheritance is no problem with interfaces I can easily[/color]
create[color=blue]
> an interface hierarchy that corresponds to the business model above. (I
> prefix interface names with the letter 'I'.)
>
> For each of the iterfaces I have an implementing class that additionally
> inherits from (extends) the implementing class of the parent interface.
>
> Example:
>
> IModelElement
> ^ ^
> | |
> | -----------------------
> | |
> | IGeneralizableElement
> | ^
> | |
> ModelElementImpl |
> ^ |
> | |
> ----------------------- |
> | |
> GeneralizableElementImpl
>
> For the multiple inheritance case I decided that the Namespace features of
> Class are less important than the features of GeneralizableElement.
> ClassImpl therefore extends GeneralizableElementImpl. The features of
> Namespace (e.g., the list of owned ModelElements) are realized by
> aggregation and delegation. That is, ClassImpl has an aggregate[/color]
association[color=blue]
> to NamespaceImpl (essentially a role class). Each feature of INamespace
> (properties as well as operations) is implemented by delegating the call[/color]
to[color=blue]
> the NamespaceImpl instance.
>
> The ownedElements of Namespace are implemented as a collection of
> IModelElement (the interface!) since beneath Classes there is a number of
> other children of ModelElement that can be owned by a Namespace.
>
> So far nothing special - this is a widely used design pattern.
> The problem is when I try to serialize the root object (an instance of
> ModelElement) I get an exception at runtime because the SoapSerializer[/color]
fails[color=blue]
> to serialize interfaces. This is documented flaw but I am sure somehow I[/color]
can[color=blue]
> get around it - I only don't now how.
>
> Do you?
>
> Thank you in advance
>
> Daniel Faensen
>
>[/color]


Daniel Faensen
Guest
 
Posts: n/a
#3: Nov 11 '05

re: Serialization and interfaces


Thank you for your quick reply.
The SoapFormatter is indeed pretty easy to use. Anyway, I dived a little
deeper into the documentation and found a solution that gives me full
control over serialization.
The problem seems to be that the XmlSerializer doesn't know the structure of
the classes implementing my interfaces on initialization. If I add the
following attributes to the class of the root object everything works well:

[XmlInclude(typeof(ClassImpl)), XmlInclude(typeof(ClassifierImpl)),
XmlInclude(typeof(ModelElementImpl))]
public class NamespaceImpl : ModelElementImpl, INamespace {
....
}

Daniel

"Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
news:OnCnOOKeDHA.2076@TK2MSFTNGP12.phx.gbl...[color=blue]
> Daniel,
>
> I am assuming you are talking about the XmlSerializer, since there is no
> such thing as a SoapSerializer in the .NET Framework.
>
> Serializing Interface references is simply not support since the goal for
> the XmlSerializer is to bind to strongly structured XML data compatible[/color]
with[color=blue]
> the type system defined by XML Schema. Since there is no notion of an
> interface in Xml Schema, there is no need for to serialize and deserialize
> interfaces. I have to somewhat agree with this, because an interface is[/color]
not[color=blue]
> a complete class. The serializer would have to embed extra information in
> the generated Xml document in order to deserialize the full instance later
> on. This extra information could easily invalidate documents against their
> schema.
>
> The only alternative you currently have if you need to serialize to Xml is
> the SoapFormatter, which isn't as flexible as the XmlSerializer (which
> should be called XsdDataBinder), but it produces enough information to
> re-hydrate the serialized interface.
>
> --
> HTH
> Christoph Schittko [MVP]
> Software Architect, .NET Mentor
>
> "Daniel Faensen" <faensen@inf.fu-berlin.de> wrote in message
> news:bjq3ab$lj0ao$1@uni-berlin.de...[color=green]
> > As a good OO programmer that I hopefully am I prefer to implement[/color][/color]
against[color=blue][color=green]
> > interfaces rather than classes. This is especially useful when it comes[/color][/color]
to[color=blue][color=green]
> > multiple inheritance which is as you know an issue and not supported in
> > languages as Java or C# - for good reasons.
> >
> > I have an object graph I want to persist using the XML serialization
> > features of the .NET framework. The interface/class hierarchy of my[/color]
> business[color=green]
> > model resembles the following example that I extracted from the UML
> > specification:
> >
> >
> >
> >
> > ownedElements * ----------------
> > |------------>| ModelElement |
> > | ----------------
> > | ^ ^
> > | | |
> > | ---------- -----------
> > | | |
> > | ------------- ------------------------
> > |-<>| Namespace | | GeneralizableElement |
> > ------------- ------------------------
> > ^ ^
> > | |
> > --------- -----------
> > | |
> > ---------
> > | Class |
> > ---------
> >
> > Since multiple inheritance is no problem with interfaces I can easily[/color]
> create[color=green]
> > an interface hierarchy that corresponds to the business model above. (I
> > prefix interface names with the letter 'I'.)
> >
> > For each of the iterfaces I have an implementing class that additionally
> > inherits from (extends) the implementing class of the parent interface.
> >
> > Example:
> >
> > IModelElement
> > ^ ^
> > | |
> > | -----------------------
> > | |
> > | IGeneralizableElement
> > | ^
> > | |
> > ModelElementImpl |
> > ^ |
> > | |
> > ----------------------- |
> > | |
> > GeneralizableElementImpl
> >
> > For the multiple inheritance case I decided that the Namespace features[/color][/color]
of[color=blue][color=green]
> > Class are less important than the features of GeneralizableElement.
> > ClassImpl therefore extends GeneralizableElementImpl. The features of
> > Namespace (e.g., the list of owned ModelElements) are realized by
> > aggregation and delegation. That is, ClassImpl has an aggregate[/color]
> association[color=green]
> > to NamespaceImpl (essentially a role class). Each feature of INamespace
> > (properties as well as operations) is implemented by delegating the call[/color]
> to[color=green]
> > the NamespaceImpl instance.
> >
> > The ownedElements of Namespace are implemented as a collection of
> > IModelElement (the interface!) since beneath Classes there is a number[/color][/color]
of[color=blue][color=green]
> > other children of ModelElement that can be owned by a Namespace.
> >
> > So far nothing special - this is a widely used design pattern.
> > The problem is when I try to serialize the root object (an instance of
> > ModelElement) I get an exception at runtime because the SoapSerializer[/color]
> fails[color=green]
> > to serialize interfaces. This is documented flaw but I am sure somehow I[/color]
> can[color=green]
> > get around it - I only don't now how.
> >
> > Do you?
> >
> > Thank you in advance
> >
> > Daniel Faensen
> >
> >[/color]
>
>[/color]


Closed Thread