I am a supporter of using tools where appropriate and making the development experience as pleasant as possible. There has been lots of discussion on XML-OO mappings (Google is your friend if you want to find more) and this post adds to it.
I don’t have a problem with using object-oriented languages for building contracts. If writing a class in an object-oriented language is your thing, go for it. Tooling makes it very easy. However, sometimes architects/system designers/developers have to consider the cost of tool-automation. Here’s a simple example attempting to illustrate the mismatch (at least with the tooling I am using):
[XmlTypeAttribute(TypeName="Person", Namespace = "urn:savas:person")]
[XmlRootAttribute(ElementName="person", Namespace = "urn:savas:person")]
public partial class Person
{
private string firstName = null;
[XmlElement(ElementName="first-name")]
public string FirstName
{
get { return this.firstName; }
set { this.firstName = value; }
}
}
This is a very simple class with some C# attributes to help with the object to XML serialisation (.NET tooling support). We compile the class into a library and then we use the xsd.exe tool to generate the schema for this class…
<xs:schema xmlns:tns="urn:savas:person"
elementFormDefault="qualified"
targetNamespace="urn:savas:person"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person" type="tns:Person" />
<xs:complexType name="Person">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="first-name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Does anyone see a problem with this? But hold on. Before we go there, let’s do the reverse. Let’s use the tooling with this schema to generate a class. So, xsd.exe /c <schema filename> and voila… We get a partial (with .NET 2 beta) class that looks very similar to the one we wrote by hand. Cool one may think. Year, fair enough… this is cool. The tooling makes it really easy for us. We don’t even need to understand XML Schema. We just get this schema and give it to someone. Or, if we use Web Services automation, this schema will be automatically included in our WSDL file and so we never have to touch XML. “How cool is that?” one may still think.
Now, let’s replace the highlighted line in our schema with the following line:
<xs:element minOccurs="1" maxOccurs="1" name="first-name" type="xs:string" />
The result, using xsd.exe on this, is a class that is identical to the one produced when minOccurs=”0″. Eh? Surely not! I see two problems with this:
Obviously the above is just a very simple example and it is based on a particular tool limitation (I do hope I haven’t missed something obvious :-). I can see how an ‘IsOptional’ property on the XmlElement attribute could solve this problem. In fact, the [DataContract] serialisation mechanism in .NET 2.0 does exactly that. The point, however, still remains: unless we start thinking in terms of contracts and declarative means for building distributed, service-oriented applications the XML-OO mismatch will haunt us for ever. We need to build domain-specific languages and tools to help us with contract authoring and validation (modelling anyone?).SSDL is an experiment in this space albeit the tooling support is not great at the moment 🙂
See "BrainExpanded - Introduction" for context on this post. Notes and links Over the years,…
This is the first post, in what I think is going to be a series,…
Back in February, I shared the results of some initial experimentation with a digital twin.…
I am embarking on a side project that involves memory and multimodal understanding for an…
I was in Toronto, Canada. I'm on the flight back home now. The trip was…