Code Generation

Flit generates your fluent interface in C# using the provided T4 template. The template is meant as a starter, it's expected that you may wish to tweak it or even write a new one that generates something else, like javascipt or python.

The provided T4 template

Flit's samples project contains three samples. Each sample's directory contains a *.tt file. You'll see that these T4 files simply include the default template: "FlitGen.ttinclude". This include file is where all the work is done.

FlitGen.ttinclude expects three parameters:
  • A string defining the text file with the state definition.
  • A string with the namespace to put generated code into.
  • A boolean representing whether or not to generate Visitor pattern support code.

This included template:
  • generates a property on the state's class for each of a state's parameters.
  • generates a constructor on each class that requires each parameter
  • generates a method for each transition that:
    • requires all of the target state's parameters
    • requires all of the transition's parameters
    • passes the state's parameters from the method arguments into the states
    • passes the state's and the transition's parameters into a partial method (ready for you to provide a handler for)

Depending on the fluent interface you are generating, you might want to customise this template. There are plenty of resources on the internet about T4, and Scott Hanselman links to most of them

How to handle calls to your fluent interface

Ok, so Flit can generate all the boilerplate code for you, meaning that you've now got an API that's nice for other people to use. But how do you handle these calls into your fluent interface? You can customise the template to change this, but by default there's two options: partial methods and the visitor pattern.

Partial Methods

For each transition in the generated classes, Flit generates a partial method declaration that gets called whenever a transition is taken. You can see an example of how to do this in the Validation sample.

The Visitor Pattern

The visitor pattern is essentially a way for languages like C# and Java to cleanly provide a way to handle different subclasses in different ways. Note that we've stubbornly (noobishly) called the "accept" method "visitCallBack". You can see an example of using the visitor pattern in Flit's GivenWhenThen sample. Not everyone likes the visitor pattern, so Flit's template makes it easy to turn this off.

Last edited Oct 18, 2009 at 9:14 PM by robfe, version 4


No comments yet.