473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Serialization and interfaces

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
Nov 11 '05 #1
2 10736
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" <fa*****@inf.fu-berlin.de> wrote in message
news:bj************@uni-berlin.de...
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

Nov 11 '05 #2
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]" <ch********************@austin.rr.com> wrote in
news:On**************@TK2MSFTNGP12.phx.gbl...
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" <fa*****@inf.fu-berlin.de> wrote in message
news:bj************@uni-berlin.de...
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


Nov 11 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel Lidström | last post by:
Hello, how can I control the order of which elements are serialized. For example, my class looks like: public __gc class Author { public: Author(); // get/set methods
0
by: Andy Searls via .NET 247 | last post by:
Trying to get business objects to work as return values for webservices. The primary problem is that the business objects arelate bound, requiring interfaces for property types. I tryusing the...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
0
by: Art | last post by:
Hi, I've got a lot of confusion about XML serialization. 2 specific questions: I'm using a book by Robin Reynolds-Haertle. In her section on serialization she starts with binary and...
4
by: Joe | last post by:
I would like to call a method of an object before the object is serialized. I currently have a surrogate that I use for deserilization and was thinking I could use one for serialization but that...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
5
by: raj | last post by:
I know what interfaces are and what they used for etc. Today i am learning about serilization. I know to mark the class "serializable" and implement ISerializable interface, and then implement...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
by: JosAH | last post by:
Introduction Upon hearing the word, "Serialization", the first question which comes to mind is ... "What is Serialization?" We know that we can create resusable objects in Java. But the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.