Monday, February 19, 2007

* Increasing Productivity/Discipline using Project Templates

If your team/organization is  into developing lot of similar type of /applications components like libraries, console applications etc with similar structure like config files, then Project Templates feature of Visual Studio 2005 can bring lot of productivity improvements and discipline into your applications.

Project templates are way of generating default base structure for similar kind of projects. In default installation of Visual Studio when we create a new project and select one of the project type like console application or class library, its actually the project template which we select.

Project Templates are different from Enterprise Templates. Infact Enterprise Templates are made of Project Templates, Policy files, etc.

 Let us consider the Console Application template which comes with Visual Studio installation. When we create an application using this template by default it has one code file called Program.cs and the default code which gets generated is :

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}






By default a namespace, a class with a Main method has been generated as this is the minimum every console application needs to have to get qualify as a console application. As a best practice, every console application should also have a way of displaying the usage if wrong arguments are passed. So, it makes sense for the console application template to generate even the code for usage display.



Let us create a template for console applications which will generate the method for usage display also.



Create an application using Visual Studio console application template and the method to display usage in case the number of arguments are not correct. The modified code can look like this :

using System;
using System.Collections.Generic;
using System.Text;
namespace ProjectForTemplate
{
class Program
{
static void Main(string[] args)
{
if(args.Length != 2)
DisplayUsage();
}

static void DisplayUsage()
{
Console.WriteLine("Usage :");
}
}
}






Compile the app and make sure it works.



Go to File -> Export Template. Select Project Template and click Next. Give the appropriate Template Name. Make sure the option 'Automatically import the template into Visual Studio' is checked, this will copy the generated template to Visual Studio template repository.



Start a new instance of Visual Studio, Select File -> New -> Project and you will see your template in My Template section. After this all steps remain same as with other templates. All the projects created using template will have usage method by default. So you just extended your Visual Studio environment making it more powerful.



~bye & take care~



Other Posts



3 comments:

  1. Good stuff Vikas.
    Even though it was so simple and there right at the File menu, i have never noticed it and that was an intersting one to try out.

    ReplyDelete
  2. I work for an Application Service Provider; this is going to come in very handy. I have yet to mess with this option, but is there a way to create a custom wizard with this that asks if any classes need to be extended and the like?

    I'm trying to combine this with an OR/M layer; any advice?

    Thanks.

    http://johnramey.net

    ReplyDelete
  3. Hi John,
    You can try using GAX & GAT packages from p&p team of microsoft which assist in creating such wizards and recepies.
    http://dotnetwithme.blogspot.com/2007/03/gax-and-gat-feb-2007-ctp-released.html

    ReplyDelete