Contract First and XSDObjectGen

I have been a fan of the concept of "Contract First" development, particularly where XML - based messages can encapsulate the data. This works not only for webservices, but for most any type of business object. The general idea is that if you can "Schematize" the objects at the message level, your customers or partners can get a copy of the schema and begin their part of the work about consuming your stuff even before you write any code.

This concept has not been lost on the folks at Microsoft. Not only have they incorporated the Application Connection Designer in VS.NET 2005 Team System, there has been significant work along these lines by some of the XML and webServices luminaries such as Christian Weyer and others. The MS XML Team Blog is another good place to start for resources and links.

Aaron Skonnard has a series he is doing on this at MSDN.

Enter XSDObjectGen from Colin Cole and Dan Rogers at Microsoft. This is a big step in the right direction, because it really fills in a lot of the missing links between Contract First (schema) and Code When? (your classes). Developers who are building applications that use XML based messaging need tools to "Start with the Schema" as the modelling language, and be able to quickly and accurately generate the code implementations that allow them to stay in the Object Oriented Programming World they are comfortable in.

Feed it your schema(s) and get back XSD.EXE - style classes - but on steroids! Everything you need (well, almost everything). Since the generated code consists of ordinary classes, you can even choose to ignore the fact that XML is even involved and simply use them as, well -- classes!

XSDObjectGen produces a new Project Type in VIsual Studio.NET - "XSDObjectGenerator" which uses the new library as a Visual Studio.NET wizard, making the process almost a no-brainer. Or, you can choose to use it on the command line and generate your classes "by hand". Either way, you get a lot for your trouble, including support for XML schema constructs, Enumerator and Collection behavior, a programming model that actually matches the orginating Schema, automatic subclass construction, and much more including a "MakeSchemaCompliant" method that addresses one of the shortcomings with this technique so that any required fields you haven't populated will be filled with at least default (empty) values. You also get depth-wise traversal events that you can hook. In short, if you can write a schema, which you can do very easily in the Visual Studio .NET XSD Schema Designer, you are gonna get a whole bunch of very usable code for your effort!

For Webservices, this thing is a dream - the generated sample classes are attributed to work well in document literal WSDL contracts, and you can directly use the generated classes as method arguments and return types in webservice method bodies. It even supports XMLInclude if your schema is in multiple "pieces" (only one level deep, however).

I have only one regret at this point: This thing would have saved me some 10 to 15 hours of tedious coding against somebody's industry spec schemas, had I found it two weeks earlier! Highly recommended.

NOTE: for those seeking a robust JAXB - style Schema Compiler for .NET, look up "DINGO" on Sourceforge.net Snippet:

"Dingo is a pluggable Schema Compiler for .NET and will generate C# code. The goal is to provide a simple way to generate Domain Objects. .NET XSD currently only generates Data Objects. Dingo can delegate code generation with high granularity."

N.B. Fellow MVP Daniel Cazzulino, who never ceases to amaze me because his batteries never seem to run down, is also working with a similar idea for VS.NET as an add-in, here.