Monday, June 11, 2007
Posted on Monday, June 11, 2007 10:00:28 PM (Mountain Daylight Time, UTC-06:00)  Comments [0] | 
Categories: .NET | Devt Tools

One way to increase developer efficiency is to leverage automation where possible, including in your Visual Studio item templates. In this post I will review how to create some simple Visual Studio templates, and how to share them with a team of people. 

About Item Templates

For every type of item you can create in Visual Studio, there exists a template, and when you select the item to add to your project, the template gets executed, the new item is produced and the file(s) are added to your solution. Sounds simple, and for the most part it is. Of course there are some wrinkes.

Before we get into that, you can find your existing templates at:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates. There is also a ProjectTemplates folder at the same level. Templates for each language (Visual Basic, C# etc) are located below these folders.

Zip Files and the Template Cache

The first wrinkle is that templates are stored in zip files. The second wrinkle is that the actual templates that are used by visual studio are stored in a cache folder. This is located in the same folder, and not suprisingly named ItemTemplatesCache.

This is all well and good, but it can make debugging your templates a little bit of a pain.

Editing Templates

The first thing to do is decide where you want to actually edit your templates - I would not edit them in the ItemTemplates folder. Since these end up being part of your codebase, create a source controlled location just like you would for any other code. 

Custom Class Template

In this example I'm going to show how to add some useflu extras to all your classes. We'll add a header that will have a copyright statement, the developer's name and the date it was created, as well as a default namespace. To do this, in Visual Studio, we just create a new class library project, and add a class. We then edit the class so that it has the header we want, and the regions.

We also need to add some parameters into the class that Visual Studio will replace when the item is added into the solution. These are of the form $parameter$, and there are 13 that are automatically present. You can also create custom parameters if you need more control. See the MSDN page on Template Parameters for more information.

Option Explicit On

Option Strict On

'======================================================

' Copyright Statement...

'======================================================

' Author: $username$

' Date Created: $time$

'======================================================

' Description:

'

'======================================================

#Region " Includes"

 

#End Region

 

Namespace YourDefaultNamespace

 

    Public Class $safeitemrootname$

 

 

    End Class

 

End Namespace

Icons

If you want your new class to have a saucy new icon, then you'll need to create or locate a suitable icon and have it handy when we export the class from the project.

Exporting to a Template

Once yoiu have the file the way you like it in Visual Studio, simply use File --> Export to Template to start the export Wizard. The wizard is very simple and easy to follow. What's really nice is that you can have the template automatically include references into the target project for you. This is very convenient when working with ArcObjects.

The final step of the wizard is shown below - essentially it will export the template, create the vstemplate metadata file, store the output and import it into Visual Studio all at once.

Using the Template

Once the template has been imported into Visual Studio, it's ready to be user. Just add an item to a project, and scroll down in the "Add New Item" dialog to the My Templates section. There you will see your shiny new tempalte.

Select it, give it a name, and Visual Studio will do the replacement, and open it in the editor.

Option Explicit On

Option Strict On

'======================================================

' Copyright Statement...

'======================================================

' Author: Dave

' Date Created: 06/11/2007 21:46:32

'======================================================

' Description:

'

'======================================================

#Region " Includes"

 

#End Region

 

Namespace YourDefaultNamespace

 

    Public Class SomeNewClass

 

 

    End Class

 

End Namespace

That's it. You now have a way to easily create classes that have consistent headers and organzaiton. This is a very simple example, but you can look at the ArcGIS templates provided by ESRI and see some examples doing more complex things.

Sharing Templates

It's  one thing to write your own templates, but the real benefit shows up when you have a whole team using the same templates - this ensures that the code is more consistent, and thus it's easier for other team members to work on. Sharing templates  is a two step process. First, put your shared templates on the network, and use the Tools --> Options, Projects and Solutions and set the location of your User Item templates. Next, open the Visual Studio Command Prompt (not a normal command prompt!) and run devenv -installvstemplates. This copies all the templates from the User Item Templates location into the ItemTemplate cache. Whenever the templates are changed, this step must be re-run to load the items into the local cache.

In a following post I'll show some more complex templates that simplify the Supervising Controller pattern I talked about a few posts back.

Download the example Template Visual Studio Project You'll have to export the template after editing the custom class - i.e. adding your copyright and default namespace!

Resources

Template Parameters (MSDN)

Visual Studio Templates Overview (MSDN)


Comments are closed.