Wednesday, March 21, 2007

* Visual Studio : How to create Project Item Templates

Before continuing with this post, pls go through the following two posts in sequence as they form the basis for this post.

  1. Accelerating development using Visual Studio Templates
  2. Increasing Productivity/Discipline using Project Templates

Consider a scenario where a company is into developing lot of console based utilities and want to display the copyright notice as soon as any of the utility is executed. A definite requirement will be to have a consistent copyright text in all utilities.

Now to get the consistency and ease of clubbing copyright notice to each utility, the requirement is to provide the teams with a artifact which they can easily integrate into their utility. The steps below will show how this can be achieved using the Project Item templates.

  • Create a console application using the default 'Console Application' Project template available with Visual Studio.
  • Add a new class file to the project using Add -> New item . Name the file as CopyRight.cs
  • Add the following code to CopyRight.cs file
using System;
using System.Collections.Generic;
using System.Text;

namespace MyCompany
{
class CopyRight
{
public static void Print()
{
Console.WriteLine("My Company's Copyright Notice");
}
}
}








  • Modify the code of Program.cs to contain the following code where in the start of main method the copyright class method is called to display the copyright message.
using System;
using System.Collections.Generic;
using System.Text;

namespace ProjectItemTemplate
{
class Program
{
static void Main(string[] args)
{
MyCompany.CopyRight.Print();
}
}
}








  • Build & execute the project to make sure its working fine.

  • Go to File -> Export Template. Select Item Template.

  • Select the CopyRight.cs item in the next screen.

  • Select the references based on requirement on next screen.

  • On next screen, change template name to Copyright. Click Finish. Its done.

  • To check whether we have successfully created an item template, create a new console application.

  • In solution explorer, go to Add -> New Item -> My Templates. You will a new template called Copyright. Add it to the project and you will see that Copyright.cs has been added to the project.


Above scenario may not be the best to demonstrate the true power of Project Item Templates but they definitely help in increasing productivity and bringing discipline to the project.



~tata & take care~



Other Posts



No comments:

Post a Comment