Code Generation and Automation in Visual Studio - Guidance Automation Toolkit
by David Hayden ( .NET Developer )
Code Generation, O/R Mappers, and .NET productivity tools are a huge factor in developing maintainable .NET and ASP.NET Applications in Visual Studio.
When talking about code generation specifically in Visual Studio, I often use a combination of the following:
- Code Snippets and “Live” Templates: You can use the code snippets functionality in Visual Studio 2005 for inserting code fragments as well as live templates from visual studio add-ins like Developer Express CodeRush or JetBrains ReSharper. This essentially helps you reduce the amount of keystrokes while you type custom code in your .NET Applications.
- Visual Studio Templates: You can customize existing templates within Visual Studio or make new templates that can help you generate custom items or whole solutions: See the following tutorials:
- Code Generation Tools: One can used specific code generation tools to help with repetitive tasks. CodeSmith is one I use often. For database specific code generation, I typically use a custom code generation tool I use internally which I thought I had talked about more on my blog. Here is a snippet on it, but it has gotten pretty functional:
This is all a huge time saver, but the Microsoft Patterns and Practices Group has come out with an even better solution - Guidance Automation Toolkit.
Per the website:
“ The Guidance Automation Toolkit consists of a series of elements that work together to provide automation functionality, including:
- Recipes - Recipes automate activities that developers would usually perform manually, often by following a series of instructions. You can use recipes to ensure that repetitive and often error-prone activities are performed in a consistent manner and to simplify complex or repetitive development tasks. Recipes can be run on particular solution elements, or on a group of solution elements that share certain characteristics (for example, all C# projects).
- Actions - Actions are atomic units of work called in a defined sequence by recipes. The sequence is specified in the recipe definition. An action accepts input either from arguments that have been gathered by the calling recipes or from output received from an action run earlier in the sequence. Recipe actions are specified in the recipe definition.
- Text Template Transformation Templates - A Text Template Transformation template consists of a combination of text and scriptlets. Scriptlets are expressions in Visual Basic or C# that when run, return a string that is directly inserted into the output stream of the template. Templates are expanded by the Text Template Transformation engine included with the Guidance Automation Toolkit. Text is inserted unmodified into the template output.
- Wizards - Wizards are value gathering strategies used to gather values from recipe arguments. Any recipe can have a wizard associated with it. A wizard walks the developer through one or more steps, which are displayed as pages of the wizard.
- Type Converters - Type converters validate the value of a field and convert them from their user interface representation to a type representation.
- Visual Studio Templates - Visual Studio templates are written in XML and are used by Visual Studio to create solutions or add one or more projects or items to an existing solution. The templates are expanded by the Visual Studio template engine. Using the Guidance Automation Toolkit, you can associate Visual Studio templates with recipes. This association means that when a template is unfolded, the wizard extension calls the recipe to let it collect parameter values (arguments) for the expansion and then, after the template is unfolded, to execute actions that may further transform solution items created by the template. “
If you are big into automation and code generation and interested in the future of code generation from a Microsoft perspective, I highly recommend looking into the Guidance Automation Toolkit and the T4 Template Language. I will provide some examples of using the GAT in the near future.
Posted By: David Hayden ( .NET Developer )
Filed: Guidance Automation Toolkit