HOME | SIGN UP | ACCOUNT | SUBSCRIBE | TROUBLESHOOTING | LOGIN
News · Meetings · Jobs · Downloads · Universal Thread articles · Web articles · Conference coverages · Consultants · Blogs · FAQ · User groups · Photos · Videos

Mere Mortals .NET Column - Customizing VS 2005 with New Item Templates

Kevin McNeish, Oak Leaf Enterprises, Inc.
Oak Leaf Enterprises is a company of integrity and quality. We have nearly 20 years of experience in the computer industry combined with a strong history of application development. Our experience in creating custom software for large and small companies and our thorough understanding of Object-Oriented programming gives us the insight necessary to create a stable Framework with built-in flexibility for your own development needs. In addition to the Mere Mortals Framework for .NET and VFP, Oak Leaf Enterprises continues to offer custom software development and mentoring services to large and small companies around the world.

Visual Studio 2005 provides much easier mechanisms than previous version of Visual Studio for extending the IDE. In this month's column I'm going to show you how MM .NET uses new item templates in VS 2005 to provide better RAD capabilities. As always, this column contains valuable information even if you are not an MM .NET user.

The Add New Item Dialog

If you have MM .NET installed on your machine, when you right-click a project and select Add | New Item... from the shortcut menu, MM .NET templates are displayed under My Templates at the bottom of the Add New Item dialog: As you select each template a description of the template is displayed under the templates list and a default class name is displayed in the Name text box:

The following sections show you the steps it takes to display your own custom templates in this dialog.

Item Template Zip Files

When you install MM .NET on your computer, a set of zip files is installed in the following directories:

C:\Documents and Settings\Kevin\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual C#\1033
C:\Documents and Settings\Kevin\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual Basic\1033

Here is a screen shot of the C# template zip files:

When you launch the Add New Item dialog, Visual Studio searches this directory for zip files and examines their contents. Template zip files contain one .template file and one or more source code files. For example, the MMBusinessObject.zip file contains these files:

And the MMBusinessForm.zip file contains these files:

Visual Studio Template Files

Visual Studio .template files contain information that instructs Visual Studio how to display a template in the Add New Item dialog and what actions to perform when a new item is added to a project from the template.

For example, here is the contents of the MMBusinessObject.vstemplate file:

Specifying Display Information with the <TemplateData> Element

The <TemplateData> element contains information that tells Visual Studio how to display the template in the Add New Item dialog. Here is a rundown of its sub-elements:

Element Description
Name The name displayed under the template icon in the dialog
Description The description displayed at the bottom of the dialog when the template is selected
Icon The icon displayed for the template in the dialog
ProjectType The type of project (CSharp, VisualBasic, etc.) the template is associated with
SortOrder A relative number that specifies the order in which the template is displayed in the dialog
DefaultName The default name of a new item created from the template. This is displayed in the Name text box of the dialog

Specifying Dependent Assemblies with the <References> Element

The <References> element (nested within the <TemplateContent> element) allows you to specify .NET assemblies that must be referenced in the project when a new item is added to the project from the template. This is an extremely useful feature of template files. For example, in the MMBusiness.template file shown above, the following assembly dependencies are specified:

  • System
  • System.Data
  • System.Xml
  • Mere Mortals Framework Interfaces
  • Mere Mortals Framework

When Visual Studio adds a new business object to a project based on this template, it checks if the specified assemblies are already referenced in the project. If not, it automatically adds a reference to these assemblies for you.

Specifying Template Source Code Files with the <ProjectItem> Element

The <ProjectItem> element (nested within the <TemplateContent> element) specifies the source code files that are added to the project when a new item is added. There is one <ProjectItem> element for each source code file. For example, the MMBusiness.template file specifies a single source code file:

The MMBusinessForm.template file specifies two source code files:

This is because Windows Forms in VS 2005 are comprised of two partial classes by default.

Replacing Parameters in Template Source Code Files

Notice the <ProjectItem> elements shown in the previous section have a ReplaceParameters attribute that is set to "true". This tells Visual Studio to search the source code files for parameter tokens and replace them with specified values when adding the source code files to the project.

For example, here is the MMBusinessObject.cs template source code file:

The text strings contained within "$" signs are parameter tokens. In this example, the $rootnamespace$ and $safeitemrootname$ tokens are built-in parameters used by Visual Studio to indicate the project's default namespace (C#) or root namespace (VB .NET). When the source code file is added to the project, VS replaces this token with the specified namespace. The $safeitemrootname$ token is the name the user specified for the class in the Add New Item dialog. Again, Visual Studio replaces this token with the specified class name.

The other two tokens, $basefullname$ and $createbusinessrule$ are two custom parameters unique to MM .NET.

Specifying Custom Parameters in Template Files

The <CustomParameters> element (nested within the <TemplateContent> element) allows you to specify your own custom parameters in template source code files. For example, the MMBusiness.template file contains these custom parameter declarations:

The <CustomParameter> sub-element specifies the token for the custom parameter and the value to be inserted in place of the token when the source code file is added to the project. In this case, an empty string is used to replace the $createbusinessrule$ token, and the text "ABusinessObject" is used to replace the $basefullname$ token (this specifies the default base class of the new business object).

Templates In Action

To better understand how templates work, here is a step-by-step demonstration of adding a new item to a project:

  1. Right-click a project and select Add | New Item... from the shortcut menu.

  2. In the Add New Item dialog, select the MM .NET Business Object template:

    Notice the description "MM .NET Business Object" is displayed beneath the templates box as specified in the <Description> element of the MMBusiness.template file. Also notice the default name of the class is BusinessObject1.cs which was specified by the <DefaultName> element.

  3. If you change the name of the class to Inventory.cs and click the Add button, here is the resulting source code added to the project:

Note the following:

  • The $rootnamespace$ token has been replaced with the project's default namespace OakLeaf.MM.NorthwindSample

  • The $safeitemrootname$ token has been replaced by Inventory (the name specified in the Add Item dialog)

  • The $basefullname$ token has been replaced by ABusinessObject as specified in the template file.

  • The $createbusinessrule$ token has been replaced by an empty string.

Conclusion

The approach Microsoft has taken for new item templates is much easier to understand and implement in Visual Studio 2005. By creating your own custom templates you can add RAD to your daily development tasks as well as encourage coding standards for your development team.

More articles from this author

NoTitleDate
1.An Interview with Kevin McNeish from GLGDWDecember 2002
2.Mere Mortals .NET ColumnAugust 2003
3.Mere Mortals .NET ColumnApril 2004
4.Mere Mortals .NET Column - Data Binding in Windows Forms ApplicationsDecember 2003
5.Mere Mortals .NET Column - Dynamic and Flexible Data AccessMarch 2004
6.Mere Mortals .NET Column - Enhancing the Windows Forms DataGridOctober 2004
7.Mere Mortals .NET Column - Extensibility MechanismsFebruary 2004
8.Mere Mortals .NET Column - Getting Ready for ADO.NET 2.0 - Part IMay 2005
9.Mere Mortals .NET Column - Getting Ready for ADO.NET 2.0 - Part IIJune 2005
10.Mere Mortals .NET Column - Integrating Infragistics Web ControlsSeptember 2004
11.Mere Mortals .NET Column - Integrating Infragistics Windows ControlsNovember 2004
12.Mere Mortals .NET Column - Leveraging Static Members in .NETOctober 2003
13.Mere Mortals .NET Column - MM .NET Most Valuable Professionals (MVPs)January 2004
14.Mere Mortals .NET Column - ObjectSpaces, Persistence, and Relational MappingMay 2004
15.Mere Mortals .NET Column - Run, Don't Walk to Visual Studio 2005!December 2005
16.Mere Mortals .NET Column - Security in MM .NETNovember 2003
17.Mere Mortals .NET Column - The Art of Refactoring in VS 2005February 2006
18.Mere Mortals .NET Column - Things I've Learned!July 2004
19.Mere Mortals .NET Column - Things I've Learned! (Part 2)April 2005
20.Mere Mortals .NET Column - VS 2005 Design Time Expression EvaluationOctober 2005
21.Mere Mortals .NET Column - What Happened to ObjectSpaces?August 2004
22.Mere Mortals .NET Column - What's New in MM .NETJanuary 2005
23.Mere Mortals .NET Column - What's New in Windows Forms (Part 1)July 2005
24.Mere Mortals .NET Column - What's New in Windows Forms (Part 2)August 2005
25.Mere Mortals .NET Column - Why .NET?September 2003
26.Mere Mortals .NET Column - Working with the VS 2005 Class Designer - Part 1April 2006
27.Mere Mortals .NET Column - Working with the VS 2005 Class Designer - Part 2June 2006
28.Mere Mortals .NET Column - Working with Typed DataSetsMarch 2005
29.Mere Mortals ColumnJanuary 2006

Copyright © 1993 - 2014 Level Extreme Inc., All Rights Reserved · Telephone: 506-783-9007 Email: info@universalthread.com · Privacy & Security · Copyright · Terms & Conditions