| | After the failure of NCodeX, I still had need of a (more practical ;) code generator. Later that year, after learning about Visual Studio’s hidden T4 tool from a post on Hanselman’s blog and reading through Oleg Sych’s outstanding comprehensive guide to everything T4, I set to work on the new tool. Boilen (short for “Boilerplate Generator”) is a T4 template for easily implementing properties and common patterns on C# classes and structs, with the first among these being its support for equivalent functionality of dependency properties in both WPF and Silverlight. I won’t spend time outlining all of Silverlight’s deficiencies here. Suffice it to say that such functionality is non-trivial to implement but, if you use Boilen to declare your dependency properties for you, you basically get it for free :)
Although “implementing properties and interfaces” may sound trivial, it actually requires knowledge of many aspects of a type in order to generate members correctly. For example, you have to know: - whether or not a type is sealed before declaring any protected members
- if a type is a class or a struct before trying to generate a default constructor
- what members of an interface a base type implements before trying to declare them yourself
- etc, etc
Add to this cross-cutting features, such as properties raising the PropertyChanged event only if the type is implementing INotifyPropertyChanged, and it becomes decidedly non-trivial :) NCodeX attempted to handle this complexity by “taking over” as the primary code development tool, requiring you to represent the structure of your code in XAML rather than C#. This is how it knew whether a type is a class or struct, sealed or unsealed, what it derived from, what interfaces it implemented. Since it was generating partial types, you could always add a new interface or change a class to sealed in code, but the tool would have no idea this change occured until you updated the corresponding XAML file. Boilen takes the opposite approach, treating the compiled code as the primary source, and using reflection to discover the existing structure. This guarantees Boilen always has an accurate picture of your code. Moreover, since Boilen is implemented as just another T4 file, you can use standard assembly references to include any needed dependencies in your templates. Of course, this method is not fool-proof. For one, you must always remember to compile your code once before generating a template file (otherwise, there will be no assembly for the template to reference!). For another, T4 has a bad habit of caching assembly references. So if you build a template once, you are stuck with those type definitions for any future template execution, until you restart Visual Studio. For general use, when adding or changing members on existing types, this is not a problem. However, it can be quite annoying when adding several new types to your assembly, or changing the derivation hierarchy. On the whole, Boilen has proved to be a very useful little tool (and invaluable for working around many of Silverlight’s little . . . excentricities ;). | |