In my last post, I talked about How To Customize Existing Visual Studio 2005 Templates for Coding Productivity. I essentially modified the existing C# Class.cs template that ships with Visual Studio 2005 to make it more useful in my development environment. This provides me a bit of code generation functionality out-of-the-box, allowing me to spend less time typing mundane code and more time solving problems.
Rather than tinkering with the existing templates that ship with VS 2005, however, Visual Studio 2005 allows you to create custom templates based on existing classes and projects in the IDE. You can create these templates using the Export Template Wizard located on the File Menu.
The VerboseCSharpClass Custom Template
I tend to add a number of regions, XML comments, and various overrides in all my C# Classes. Unfortunately, the default C# Class Template that ships with Visual Studio 2005 is pretty bare, requiring me to create the same class features over and over. And even though I tend to add these same features a lot, I am still not consistent in how I name the regions, write the XML comments, etc.All this typing distracts me from the real work and consumes precious minutes throughout the day.
My custom VerboseCSharpClass Template is to solve my problems. Now when I need a heavy duty class that requires such features, I just choose it from “Add New Item” as opposed to the generic C# Class Template that ships with VS 2005.
Creating The Template
Creating the template is nothing more than creating a C# Class with all the statements, fields, properties, methods, etc. that you want included by default when you create the class. Instead of hardcoding a class name and root namespace, however, you will instead put a couple of parameters used by Visual Studio 2005 to generate them for you within the context of the Add Item Process. Here is my quickly created class for testing purposes:
Exporting the Template
Exporting the template and adding it to Visual Studio 2005 is about as easy as you can get. Once you are happy with the template, save it and choose File > Export Template… on the menu. The export wizard will walk you through a 4-step process that saves the template as a *.Zip file and automatically imports it into Visual Studio 2005 for you. Here is a collage of images showing the process:
Whether you want to modify the existing default templates that ship with Visual Studio 2005, or even better, create new ones by using the Export Template Wizard, it would be a crime not to take advantage of these code generation features to help you be more productive in your day-to-day development efforts. These two features are in addition to the more obvious new way to improve your productivity – Code Snippets.
- DataAdapter and Database Connections and Connection Pooling – Part 2
- DataAdapter and Database Connections – Performance and Connection Pooling
- .NET 2.0 Provider Model – Polymorphism – Factory Method – ADO.NET 2.0 Data Providers
- Builder Design Pattern Part 2 – Hotel Reservation EDI Example – Design Patterns in C#
- Builder Design Pattern Part I – DBConnectionStringBuilder and SqlConnectionStringBuilder in ADO.NET 2.0
Drinking: Japanese Sencha Green Tea