Grupo de Arquitectura de Software Português


Welcome to GASP Sign in | Join | Help
in Search

Tiago Pascoal

Visual Studio Export Templates and conditional inclusion of content

 

Visual Studio templates are a great way to give you an head start with code that has a lot of boilerplate code.

In fact you may already be using project templates withouth knowing. :-) When do option Open New Project in Visual Studio and select a project type (Windows Application, Console Application,etc) you are in fact selecting the project template that will be used to generate your project skeleton. This project skeleton not only includes some code/data files but it also includes the references to the .Net assemblies that your particular project type needs.

If you are curious of how out of the box project template looks like you can find them at:

  • VS 2005 - %ProgramFiles%\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates
  • VS 2008 - %ProgramFiles%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates

Visual Studio not only provides you with a great number of project templates out of the box, but you can also write your own project templates

If you don’t want to spend time writing your project template you can find that a lot of people make them freely available on the net. For example Team System MVP Neno Loje created a project template that you can use to kickstart Team System Utilities.

You can create your own project by hand (which involves some labor) or you can create your project like you regularly do. Create all the boilerplate code, include all the references,all your content and then use that project as your project template by exporting it to a project template using the Export Template wizard (available in File->Export as Template)

I don’t want to get into much details of how you can create project templates. Either by hand or using the export option. There is plenty of information about that already.

You can read some fine information about it on these articles:

Not to get back to the real intent of this post. :-) Conditional inclusion of content.

Starting with VS 2008 your project templates can have content that is only included if a certain condition is met. This is a simple IF condition and all you can do is comparing the value of a parameter to a value.

for this you simple have to use on your template files $if$ (<expression>) <your content here> $endif$

For example if your project template allows framework multi targeting you can use this to include references to Framework 3.5

$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
$endif$

You can check here the list of parameters that are available out of the box and but you can also reference parameters added in the CustomParameters element or parameters added by your wizard.

Now there is a catch. In order to use conditions, your template has to be a template of version 3.0.0 (the template version is defined in VSTemplate element of the .vstemplate file)

eg: <VSTemplate Version="3.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">

What is the catch then? If you use the export template to create your project template (even in VS 2008) the project template is exported as version 2.0.0 and you have to manually edit the .vstemplate file to use version 3.0.0 if you wish to use the $if$ functionality.

 

Post Scriptum: If you think project templates aren’t up to the level of customization you want, you can also use your own wizard in project template. And from that wizard use the VS API to do more complex things on the created project (or/and the solution that is going to hold it). And if that is not enough you can combine it with Software Factories to make it even more powerful.

[cross posted on http://www.agilior.pt/blogs/tiago.pascoal/archive/2009/05/31/8094.aspx]

Published Sunday, May 31, 2009 12:51 PM by tspascoal
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems