P&P Summit Day Four: TDD and Guidance Automation

The last real session was presented by Chris Tavares talking about Guidance automation. Chris is a software developer on the P&P team and the lead developer on the web service software factory.

Guidance automation is what to do, what not to do, and why you should follow their guidance. Why have automated guidance? To make the right thing easy. For example, passing datasets back from a web service is not necessarily the correct thing to do, but it’s easy. If you can automate the right way then maybe more people will use it.

The GAT (guidance automation toolkit) is the automation of VS.Net to help deliver your guidance. GAX is the runtime while GAT is the authoring tool. You can create your own guidance for your developers and distribute it. These can be things like templates for your websites, security code, etc., etc..

A guidance package is a combination of:

  • Recipes - automation
  • Projects and Item Templates
  • Snippets
  • Written Guidance
  • Example Code

Most of the talk consisted of the automation of the guidance package, the recipe. Recipes are described in XML and made up of :

  • References

    • Answers the question “Can this recipe be run now?”
    • Needs to query on the current state of the project such as what is selected, what does it contain and the rest of the project.
  • Arguments

    • Dictionary of name/value pairs
    • Arguments are typed
    • Each argument can have a
      • ValueProvider
      • TypeConverter
  • Wizard

    • “standard” pages are built into GAX that you can construct XML to display your page.
    • Custom pages to pull in our own data or collect data.
    • They provide the standard wizard frame (next, back, etc.)
  • Actions

    • Object that actually perform the work.
      • Code Gen
        • You can use either CodeDom or T4 engine (a CodeSmith like template code gen tool that apparently sometimes has a mind of its own.)
        • Change config files
    • You can reuse actions across recipes.

Chris then walked through a simple recipe.

Why is unit testing guidance packages hard? The GAT sometimes uses objects out of the DTE (the VS.Net API), which are hard to deal with since you’d have to have VS.Net loaded into your test harness. Other problems: not only do you have to test the generator, but you also have to unit test the generated code.

So how should you create testable Guidance packages? Here are some ideas:

  • Create a layer of abstraction between you and VS.Net/GAX. If you depend on something you can’t test, then replace it with something you can (i.e., Mock objects). For example, instead of using the DTE objects directly they wrapped them with a small wrapper called IProjectModel and then provided a mock object for it.
  • Do all the real work in separate objects and make your CodeGen stupid. One of the rules of TDD is test everything that can break. If you have a problem finding a way of testing something, then make it so stupid that it can’t break. Instead of putting the code to completely walk the objects into the template, they abstracted it out into a separate object and then just used the object in the template to produce the result. The object could then be tested easily and the template is now at a point where it can’t break.
  • MVP in the GUI - The Model in the MVP wraps the GAX dictionary. Also, don’t use standard pages if you have to since you can test it and validation sucks.

Chris admits that the GAT and GAX has a pretty steep learning curve and the documents are not really that good.

This was pretty cool and I think there is great potential here. Not only can you pass out guidance to your developers, but it’s also possible to leverage the T4 engine in other places of your development process (I wonder if this is a real threat to CodeSmith, but probably not at this time).

All this code is available in the source you get when you download the Web Service Software Factory CTP.

On a side note, from my own thoughts, the T4 engine may be a cheap way to perform code gen if you can pull it out from the GAT. There are syntax highlighting editors for T4 as well.

Another interesting thing was that Chris talked about Presenter First usage of MVP. Check out whitepapers by Atomic Objects on this variant of the pattern.