Sometimes we are using the same patterns of code over and over again. Those of us who are lazy (but smart) will create their own code snippets, if you are not familiar with this subject, read about how to create code snippets easily. But what if we need to create lots of classes with the same pattern? Code snippets may not be enough because they lack of some functionality which is needed to achieve our goal. Consider the following code:

abstract class FourWheelsVehicle : IVehicle
{
    abstract public double MaxVelocity { get; }

    abstract public string Manufacturer { get; }

    abstract public double Price { get; }

    abstract public int YearManufactured { get; }

    virtual public int NumberOfWheels
    {
        get { return 4; }
    }
}

An abstract class called FourWheelsVehicle implements the IVehicle interface (which is not presented here because it is not important for our matter). Our task is to create classes which represent each and every existing 4 wheels vehicle, there is a lot of work to do, a huge amount of work. So, I started by implementing Mazda6 class:

class Mazda6 : FourWheelsVehicle
 {
     override public double MaxVelocity
     {
         get { return 180.0; }
     }

     override public string Manufacturer
     {
         get { return "Mazda"; }
     }

     override public double Price
     {
         get { return 110.0; }
     }

     override public int YearManufactured
     {
         get { return 2008; }
     }
 }

Yes, I know, I should have implemented Mazda class and than all of the Mazda types, but this is just an example and I want to keep it simple. So, it is not very hard to realize that the rest of our work is similar to what we did with the Mazda6 class. Wouldn’t it be great if there was an option to add files which contain this pattern? I guess that you know that the answer is yes, otherwise I wouldn’t write this article…

Lets make a new Visual Studio Item Template for the new cars to come. First, we have to create 2 files called NewCar.cs and NewCar.vstemplate which will contain the template for new cars and some meta data about the provided template. This is how NewCar.cs looks like:

/****************************************************************************/
/* Class Name  : $safeitemname$                                             */
/* Author      : $username$                                                 */
/* Date Created: $time$                                                     */
/****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace $rootnamespace$
{
    class $safeitemname$ : FourWheelsVehicle
    {
        override public double MaxVelocity
        {
            get { return 0.0; }
        }

        override public string Manufacturer
        {
            get { return ""; }
        }

        override public double Price
        {
            get { return 0.0; }
        }

        override public int YearManufactured
        {
            get { return 0; }
        }
    }
}

As you can see, there are some template parameters used in this code, it is recommended to know all of the template parameters. The second file is the NewCar.vstemplate:

<VSTemplate Type="Item" Version="2.0.0"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>NewCar Template</Name>
    <Description>Custom Car.</Description>
    <Icon>Icon.ico</Icon>
    <ProjectType>CSharp</ProjectType>
    <DefaultName>NewCar.cs</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true">NewCar.cs</ProjectItem>
  </TemplateContent>
</VSTemplate>

Now, zip those 2 files to NewCar.zip, drop your file in the Visual Studio item templates location which is provided in Tools > Options > Projects and Solutions > General. Notice that you have to choose the exact folder according to the .Net language which was used, in our example it is “item templates location”\Visual C#. That’s it, to see the results, just try to add a new class to your project and you will find our little NewCar Template:

image

Choose it, rename the file to BMW.cs, for example and get your new BMW just waiting for you to edit it…

/****************************************************************************/
/* Class Name  : BMW                                                        */
/* Author      : shahar                                                     */
/* Date Created: 7/18/2008 7:39:03 AM                                       */
/****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cars
{
    class BMW : FourWheelsVehicle
    {
        override public double MaxVelocity
        {
            get { return 0.0; }
        }

        override public string Manufacturer
        {
            get { return ""; }
        }

        override public double Price
        {
            get { return 0.0; }
        }

        override public int YearManufactured
        {
            get { return 0; }
        }
    }
}

For further reading about this issue, check out the how to get visual studio to create new classes public by default article, we actually changed an existing item template there. Hope you will find this post useful, did you already know about the item templates? Do you think it may help in your daily tasks? Hope it will.

Tags :

2 Responses to “Visual Studio Item Templates”


  1. DrWatson

    Said on July 25, 2008 :

    There is a nice video showing all this process available for free at DimeCasts.net, episode #9, http://www.dimecasts.net/Casts/CastDetails/9

  2. Mathias

    Said on July 25, 2008 :

    Thank you for the tip! I can already see how this will be useful to create a template for unit testing classes, pre-populated with the right usings & attributes – rather than re-typing it every single time…

Post a Comment