Why I like Boo

Boo is a very simple programming language.

I am not sure that I would use it in a production application for fear of the maintenance problems (where do you get Boo programmers from?).

Things that I particularly like are:

Booish – being able to interactively play with practacally any .NET class without the need for a heavyweight IDE to be open.

The fact that you can post the source code to a blog like this without messing up the formatting!

I am planning a series of topics on Boo and the .NET 3 Pillars:

  • WF – windows workflow
  • WPF – windows presentation foundation
  • WCF – windows communication framework

At first I will be using Boo plus Nant but may need to stray into MSBuild.

Domain Specific Languages and XML

There has been a lot of thinking about the use of Domain Specific Languages to solve a particular problem.
This is even a major feature of Visual Studio 2005 and above.

Typically these are things like Ruby on Rails which is essentially a DSL for creating dynamic websites quickly.

However there is a much simpler solution.
State your problem in XML and use xslt to generate the solution.
If done carefully you can eliminate a lot of easy to write, but easy to get wrong code.

This is really what Kathleen Dollard has been talking about in her Code Generation in .net book.

Here is an example that almost all applications have to deal with:

How much work is it to add another maintenance screen to the system?
or even
How much work is it to add one field to one maintenance screen?

If you get the principle working for one screen you can add another by adding a few lines to an xml document and regenerating the script.

Boo Generic Support

The following blog listed generic support details for boo: link

states that generic type definitions in Boo are handled thus:

 MyType of MyGenericParam

or for multiple generic parameters

MyType[of X,Y,Z]

I find Boo to be a great exerimentation language, ideal for turning a class into a commandline tool.

wmi in boo

# The following is a very simple example of using wmi from boo
import System
import System.Collections
import System.Data
import Boo.Lang
import System.Management
import System.Windows.Forms

class WmiApp:
    _tb as TextBox
    _dgv as DataGridView
    [STAThread]
    def Run():
        f = Form(Text: “Hello, boo!”)
        _tb = TextBox(Text: “SELECT * FROM Win32_Service”, Dock: DockStyle.Top)
        _dgv = DataGridView(Dock: DockStyle.Fill)
        b = Button(Text: “Click Me!”, Dock: DockStyle.Top)
        b.Click += ButtonClick
        f.Controls.Add(_dgv)
        f.Controls.Add(b)
        f.Controls.Add(_tb)

        Application.Run(f)
    def ButtonClick(args, sender):
        WMIQuery(_tb.Text)
    def WMIQuery(query as string):
        qry = SelectQuery(query)
        ds = DataSet()
        table = ds.Tables.Add(“WMI”)
        mos = ManagementObjectSearcher(qry)
        loaded = false
        moc as ManagementObjectCollection
        moc = mos.Get()
        for prop as PropertyData in (array(moc)[0] as ManagementObject).Properties:
            table.Columns.Add(prop.Name)
        moa = array(moc)
        mo as ManagementObject
        for i in range(0, moc.Count):
            mo = moa[i]
            row = table.NewRow()
            for prop as PropertyData in mo.Properties:
                row[prop.Name] = prop.Value
            table.Rows.Add(row)

        _dgv.DataSource = table

WmiApp().Run()

VB 6 is trying too hard to be helpful

I have had the dubious pleasure recently of maintaining a VB6 Application.
The fun part is how helpful it tries to be in the background.
VB6 goes out of it’s way to hide any technical details from the user.

For example the app uses access as it’s reporting suite.  To call access it uses the COM API’s.
Our set up is odd in that we need an application that supports both Acesss 2000 and Access 2003.
For various reasons my dev machine has been upgraded to 2003.  I changed the reference to point to the new typelibrary and checked the code into our version control software.  I can now no longer point to the access 2000 olb and rebuild the exe – the two are just not compatible.  SO I get a colleage who still has Access 2000 to pull the code from version control.  As soon as he opens the app in VB6 it has automatically fixed up the OLB back to the Access 2003 version.  How the hell are we meant to control which version is released?  I guess my friend is now buildmaster!

I oftern wondered why the VB project file had to be writeable every time we pulled it from version control.  Apparently it updates the paths to any com objects to record those on the machine.  Someone needs to explain to people that is why we had COM in the first place…

Scalable Codegen Process : Application Minature

I have been working on code generation lately and have finally made a breakthough that will make life easier for most projects. I have been following the techniques found in Kathleen Dollard’s (KD) “Code Generation in .NET” book.
The intent of the book was to demonstrate code generation techniques with a sample generator.  A lot of people have gone with her generator as if it were a product.  This misses the point – the generator is easy to write it is the base classes, templates and techniques that are important.  The book was about the process not the product!

I have gone for an even more bare-bones generator than KD.

My transformation process goes:

model.codegen  + template – XSLT ->  batch file

batch file runs a series of:

model.codegen + transform + parameters -> generated code/scripts &c

The big thing is that all generation comes from one document via xslt.

So far I am generating:

  • Table creation scripts
  • Data access layer
  • Stored Procedures
  • Model to dal mapping code

I am planning on generating:

  • Model
  • View
  • Controller
  • View to Model Mapping code

The beauty of this is it all uses XSLT and requires only one custom exe. I have also found that performing the transform as a single match makes it easier to read the transform.  That is don’t call other templates – all in one complex document.

The real trick is to develop an “application minature”.
This is a small application that has one or two screens that demonstrate each of the classes that will be used in the finished application.  This is handcrafted.  This is used to create and fix the templates and the metadata document.
you may need to adjust it to match the slightly weaker coding standards that must be applied to XSLT.

You create this small application that provides just enough functionality to provide the principles involved.  This may only have a handful of model classes.  New techniques can be experimented with easily and if they generate well they can be rolled out the full application.  This has the benefit of making the entire application to be consistent.  There will be none of we experemented with that but were stuck with it.  The best idea is to have no exceptions – everything is one of the standard options. Of course you can add handcrafted code – but this must be for the truely exceptional cases.

The big stumbling block is the application framework (that is how to solve the problem). If you have an application then you should be able to extract a framework from the best parts.  If not look for a framework that solves your problems or build or buy one.  You must understand the template, the generation process and your framework.  If something does not fit then change it.  This allows new ideas to be put into production rapidly.

These techniques work very well when combined with a very pure MVC or MVP framework.  If you avoid explicit duplication in your code this will make your life very easy.

Another book on codegen

This is a book about code generation.

From the two chapters that are publically available it seems that he knows what he is talking about.

My current take on codegen is to simply use xslt.

I have an extended xslt utility that I use.

The syntax is:

xslt source transform target [name value]*

This allows one root document to be used to create a batch file that generates the used code from the one core document.

It has a few other tricks (handling or header comments and logging changes), but that is about it.
It does include some string  manipulation extensions.

Composite UI Application Block

The Composite UI Application Block (CAB) is touted as a MVC framework for large scale applications.

I have had a look at the design and it seems to be rather lacking from a MVC or a MVP perspective.

I am not saying that it does not solve certain problems.  It does provide inversion of control details.  It does allow a number of forms to be wired together in a losely coupled manner.

The documented definitions of MVC are rather woolly.

The Model component has been ignored.  The model component of the CAB is any data container.
The Controller base class does not seem to be a controller in any sense.

There does not seem to be any provided support for validation.

If this is what people think MVC is it is no wonder there are no decent in-depth articles moving models or interacting controllers.