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