Thursday, Aug 11, 2005, 4:05 PM
What should I do w/ the metadata?
Imagine I've got some "metadata" that describes some functionality of my system, e.g. the command line parameters to a tool:
<args description="Two wrongs don't make a right, but three lefts do">
<arg name="lefts" description="Number of left turns" type="int" default=4" />
<arg name="attitude" description="Driver attitude" required="true" type="string" />
</args>
Once I have this "metadata" in place, should I a) use it to generate a class at compile-time that implements the command line parsing, or should I b) use it to drive a command-line parsing framework at run-time?
If I do a), generate the code, the result might look like this:
class Args {
public void Parse(string[] args) {...}
public string Usage { get {...} }
public int Lefts { get {...} set {...} }
public string Attitude { get {...} set {...} }
}
I've got better perf, but I have to generate the code in some way and .NET doesn't come with any built-in tools to do that (no, I don't count ASP.NET or XSLT as good codegen tools). Plus, the command-line args are now baked in and require a re-compile to change and who cares about perf to parse the command line? Finally, I'm much more likely to have most of the work done in a base class, e.g.
class Args : ArgsBase {
public void Parse(string[] args) { return base.Parse(args, ...); }
public string Usage { get {...} }
public int Lefts { get {...} set {...} }
public string Attitude { get {...} set {...} }
}
In my experience, most of the work of code-gen is generating the smallest possible bit of code to provide a nice wrapper around a base class that does most of the work in a very interpretive manner.
If I do b), have a run-time interpretter of the "metadata," I've got to build a command-line argument interpreter, but, as I've said, you almost always have that anyway. However, I also give up a develop-time wrapper aka "Intellicrack" which will be pried from my cold, dead fingers.
What do you guys do?
12 comments on this post
Phil Weber:
Thursday, Aug 11, 2005, 5:30 PM
Gareth Jones (http://blogs.msdn.com/garethj):
Actually a nice example of a miniature factory. I seem to remember you asking for one of those once ;-)
Thursday, Aug 11, 2005, 6:05 PM
John Rusk:
Friday, Aug 12, 2005, 1:36 AM
Hermann Klinke:
Friday, Aug 12, 2005, 2:33 AM
Chris Hollander:
just like xsd.exe...
Friday, Aug 12, 2005, 7:19 AM
BrandonFurtwangler:
The base class should use reflection on itself to discover all of it's properties (which the derived type will create), and then set them from the metadata. Similar to how XAML works.
Hell, I'd just use xaml and be done with it.
Friday, Aug 12, 2005, 10:37 AM
John Rusk:
More here: http://steve.emxsoftware.com/ReflectionEmit+vs+SystemCodeDOM
and here: http://www.agilekiwi.com/on_the_fly.htm
Saturday, Aug 13, 2005, 1:24 AM
Robert Pickering:
http://strangelights.com/blog/archive/2005/08/13/1210.aspx
It even contains an implemetation in F# and C#.
Saturday, Aug 13, 2005, 8:16 AM
William Bartholomew:
1. Compiler "sponsored" syntax checking
2. Strong-typing
Monday, Aug 15, 2005, 4:43 AM
Kenneth Kasajian:
The advantage of codegen is that the result code is plainly viewable during debugging and you know exactly what's going on.
Sunday, Sep 18, 2005, 11:15 AM
fkyqb lfaygnodw:
Sunday, Apr 22, 2007, 4:53 PM
viryumwx whodrcbql:
Sunday, Apr 22, 2007, 4:53 PM




