Thursday 6 May 2010

Xsd2Code

If you have used xsd.exe before to generate C# code from XSD schema files you may have bore witness to some of the most horrendous looking serialization code that's ever been generated:
  • A repeating element becomes an array property with full get and set. (How are you supposed to add elements?)
  • Child object properties of generated classes must explicitly be created and assigned when creating a new instance of said class. (A recipe for NullReferenceException hell)
Just to name a few.

I was scouring the web hoping that there would be a better way to do this. I was on the verge of giving up until I encountered this link from stackoverflow:


In the short time I've played with this tool, I am already impressed at the quality of code that it generates. Just to illustrate how good this is:

Here's the Parameter property for the MaestroAPI FeatureSource class as it currently stands (generated by xsd.exe):

[System.Xml.Serialization.XmlElementAttribute("Parameter")]
public NameValuePairTypeCollection Parameter {
get {
return this.m_parameter;
}
set {
this.m_parameter = value;
}
}

If we created a new instance of FeatureSource and tried to access this property a NullReferenceException is thrown because you have to manually new the NameValuePairTypeCollection and assign it to this property yourself!

Here's the same snippet as generated by Xsd2Code

[System.Xml.Serialization.XmlElementAttribute("Parameter")]
public List<NameValuePairType> Parameter
{
get
{
if ((this.parameterField == null))
{
this.parameterField = new List<NameValuePairType>();
}
return this.parameterField;
}
set
{
if ((this.parameterField != null))
{
if ((parameterField.Equals(value) != true))
{
this.parameterField = value;
this.OnPropertyChanged("Parameter");
}
}
else
{
this.parameterField = value;
this.OnPropertyChanged("Parameter");
}
}
}
Creating a new instance of this FeatureSource, we can safely access this property because it has lazy generation logic built in! Xsd2Code does other things as well, such as providing built-in serialization/deserialization logic, object cloning, and much more!

I'm a bit disappointed that the property still has a setter (collection properties should almost never be assignable!), but already this code is much cleaner and easier to work with than the one generated by xsd.exe

Just seeing some of the code that's generated with some of the mapguide xsds, I knew that I have found my saviour. So long xsd.exe!

I will definitely be using this tool for the next (post-2.1) version of the Maestro API and any other projects that involve working with xsd files.

Tuesday 4 May 2010

WebTier-less editing with Maestro

Just landed in trunk, is experimental support for using Maestro using the offical MapGuide API (known as LocalNativeConnection in Maestro). What this means is that when Maestro starts up you are now greeted with a slightly different login dialog.


Notice the two radio buttons Connect via HTTP and Connect via TCP/IP, when the Connect via TCP/IP button is clicked, you get a different user interface


If you ever used the offical MapGuide API, you should know how this works. Give your login details, supply the path to the webconfig.ini and away we go.


Where is the value in having offical MapGuide API support? It means it is now possible to edit resources in a MapGuide server without the need for a web server and web extensions installed on your machine!

For people (like me) who test bleeding edge versions of MapGuide built from source this is a very useful feature to have. This doesn't work perfectly yet. It bombed out on me when opening feature sources (some xml serialization bug), but it's a promising start.