Visual Studio Multi-Project Templates

The idea

In a previous article I’ve already talked about how to create project templates. There I talked about the nuances of creating templates in VS2008 and VS2010, how to set up data, files and so on. This is certainly all right, but always wants more.

If the studio can create multiple projects for one solution, and that means we should be able to in theory do the same. And as applications, I think we will find. You can just make a blank for the domain, infrastructure, services, and interfaces, all at once to set up the relationship and create a single click (or, I don’t remember how much it needs to create as they have to create a project).

So, let’s do a multi-project types solution.

Training

Templates for project can be done manually, but the probability of error is large, it is better to rely on automation. We will create a multi-project from two projects; a larger number of them is doing similar and is not really an issue.

Multi-project will consist of WinForms and Dll. Overall, there is absolutely no difference what kind of projects you use. Does not even matter that they were all the same programming language!

So, we have 2 projects in Solution Explorer and start exporting them as templates. for this

  1. Open the File> Export Templates

2. Select the project, click «Next»

3. Fill in the required fields and click «Finish»

After this will open the folder with the archive, where it will be a project template. For a while leave it as it is now and carry out steps 1-3 for all remaining projects that need to be added to the multi-project.

Creation

When all our projects will be exported to , open the folder with them and Unpack them. It should look something like on the picture.

Open each folder and rename the file MyTemplate.vstemplate into something more authentic. Although in general this step is not mandatory.

The next step is to create a file that tells the studio that all these projects are not on their own, but the team!

Create a new folder next to a text file, rename it so that the extension was vstemplate.

Open the newly created file in any text editor and write the following

<VSTemplate Version="3.0.0"  xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>WinForms with Dll</Name>
    <Description>An example of a multi-project template</Description>
    <ProjectType>CSharp</ProjectType>
  </TemplateData>
  <TemplateContent>
    <ProjectCollection>
       <ProjectTemplateLink ProjectName="WinForms">
          WinForms\WinForms.vstemplate
       </ProjectTemplateLink>
       <ProjectTemplateLink ProjectName="ClassLib">
          ClassLibrary\ClassLibrary.vstemplate
       </ProjectTemplateLink>
    </ProjectCollection>
  </TemplateContent>
</VSTemplate>

At the beginning of selected text in bold (project name), which will appear in the studio to create the project for which you will learn that it is yours. Next is a description of the project is optional.

The following highlighted items indicate where there are included in the multi-project sub-projects. I think it is clear that if you had three sub-projects,the part

<ProjectTemplateLink ProjectName="WinForms">
  WinForms\WinForms.vstemplate
</ProjectTemplateLink>

It should be repeated three times

After the file is edited and saved it is necessary the file and folder and adds a new zip file and places it in the folder C: \% Documents% \ Visual Studio 2010 \ Templates \ Project templates (default).

Open Studio and create a new project, select the root of Visual C#  and see our project in the  list of available templates.

Additional improvements

You can create projects distributed into folders. To do this, wrap a section ProjectTemplateLink into SolutionFolder. For example:

<TemplateContent>
  <ProjectCollection>
    <SolutionFolder Name= “Folder One”>
      <ProjectTemplateLink ProjectName="WinForms">
         WinForms\WinForms.vstemplate
      </ProjectTemplateLink>
   </Solution Folder>
   <SolutionFolder />
      <ProjectTemplateLink ProjectName="ClassLib">
        ClassLibrary\ClassLibrary.vstemplate
      </ProjectTemplateLink>
  </ProjectCollection>
</TemplateContent>

In the project folders are created somewhat differently. Open vstemplate for the project and it is possible to add the following:

<TemplateContent>
  <Project TargetFileName="ClassLibrary.csproj" File="ClassLibrary.csproj" ReplaceParameters="true">
    <ProjectItem ReplaceParameters="true" TargetFileName="Class1.cs">Class1.cs</ProjectItem>
      <Folder Name="Properties" >
    <ProjectItem ReplaceParameters="true"             TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
    </Folder>
  </Project>
</TemplateContent>

The most important thing at this point, how to create links to other projects. Unfortunately, when you export a project, the links are not saved to other projects of the current solution. That, by and large, clears, as exported by regular means only one project in a stand-alone form. It’s easy to correct.

Open the project, where did the work piece, there are creating all the necessary links between the projects. Now open the project files in any text editor and search for sites by name ProjectReference. All of this should be similar to

<ItemGroup>
  <ProjectReference Include="..\ClassLib\ClassLib.csproj">
    <Project>{E4A41DEF-3F17-441F-9135-D5A902D82C8B}</Project>
    <Name>ClassLib</Name>
  </ProjectReference>
</ItemGroup>

Copy  the entire ItemGroup and paste in the .csproj file in the folder you want to export our project. All over pack the archive and put it in a folder with project templates. Now when you create a new project you will have all references to internal projects. That’s all!

Interesting projects for you!

Hard’n’heavy!

Tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>