Welcome to a new post where we’ll see how to create custom templates for our C# solutions, which can save us a lot of time when starting a new project.
Table of Contents
1 - Why create a code template?
It's important to know that we can create templates for our projects, since they are incredibly useful. For example, when we create a new project -> API
we get several pre-written classes and code. This is all possible thanks to templates.
Having your own template can really help in your day-to-day work.
For instance, in this post we’ll see how to create a template that includes multiple projects, what I consider the basic structure for a Back-End application.
Using a template saves you a lot of time, especially tedious, repetitive time, like when you go through interviews.
Note: At the top, you’ll find a link to the GitHub code for template creation.
2 - Creating a template for a single C# project
Creating a template for a C# project is pretty straightforward, especially if you use Visual Studio since the menu actually provides an option for it.
In the top menu -> Project -> Export Template
.
A wizard will appear, where you select the project and then click "next, next
" and it will create the template for you.
It will automatically create a zip file in the folder %USERPROFILE%\Documents\Visual Studio <version>\Templates\ProjectTemplates
, and your template will be available in Visual Studio:
By the way, the contents of the zip
file are the files you want included in the template and a .vstemplate
file, which contains metadata (like the name or description) of the project, as well as its files and structure:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>ProjectName.BackEnd.API</Name>
<Description>descripción</Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>ProjectName.BackEnd.API</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<CreateInPlace>true</CreateInPlace>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<Project TargetFileName="ProjectName.BackEnd.API.csproj" File="ProjectName.BackEnd.API.csproj" ReplaceParameters="true">
<Folder Name="Properties" TargetFolderName="Properties">
<ProjectItem ReplaceParameters="true" TargetFileName="launchSettings.json">launchSettings.json</ProjectItem>
</Folder>
<Folder Name=".vs" TargetFolderName=".vs" />
<Folder Name="Controllers" TargetFolderName="Controllers">
<ProjectItem ReplaceParameters="true" TargetFileName="ProductController.cs">ProductController.cs</ProjectItem>
</Folder>
<Folder Name="Mappers" TargetFolderName="Mappers">
<ProjectItem ReplaceParameters="true" TargetFileName="ProductMapper.cs">ProductMapper.cs</ProjectItem>
</Folder>
<ProjectItem ReplaceParameters="true" TargetFileName="ApiDependencyInjection.cs">ApiDependencyInjection.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="appsettings.json">appsettings.json</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="appsettings.Development.json">appsettings.Development.json</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Program.cs">Program.cs</ProjectItem>
<ProjectItem ReplaceParameters="true" TargetFileName="Startup.cs">Startup.cs</ProjectItem>
</Project>
</TemplateContent>
</VSTemplate>
3 - Creating a template with multiple projects
Unfortunately, when you want to include more than one project in your template, the process is a bit more tedious.
This is because you have to repeat the previous process for each project you want to include in your template.
So, you export each one to its corresponding .zip
, and then you need to extract them (you can do this in the same folder):
Now you can delete the .zip
files.
The next step is to create the file that will contain the multi-project template. To do this, manually create a .vstemplate
file, which will contain some metadata and references to the .vstemplate
files for each of your projects (the folders you just extracted).
Here’s what this file looks like:
<VSTemplate Version="3.0.0" Type="ProjectGroup" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>BackEnd Project</Name>
<Description>BackEnd Project</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>0</SortOrder>
<CreateNewFolder>false</CreateNewFolder>
<ProvideDefaultName>false</ProvideDefaultName>
<EnableLocationBrowseButton>false</EnableLocationBrowseButton>
<CreateInPlace>false</CreateInPlace>
<Icon>templateIcon.png</Icon>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="ProjectName.BackEnd.API">ProjectName.BackEnd.API\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="ProjectName.BackEnd.Data">ProjectName.BackEnd.Data\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="ProjectName.BackEnd.ServiceDependencies">ProjectName.BackEnd.ServiceDependencies\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="ProjectName.BackEnd.Services">ProjectName.BackEnd.Services\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="ProjectName.BackEnd.UnitTests">ProjectName.BackEnd.UnitTests\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="ProjectName.Shared.Dto">ProjectName.Shared.Dto\MyTemplate.vstemplate</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
</VSTemplate>
Note: as you can see, I’ve added an icon.
But that’s not where the process ends. With the current setup, when you add this template to a solution, it will create the projects with the name ProjectName.BackEnd.xxx
, which isn’t what we want. We want the name you enter when creating the project. For that, change ProjectName
to $projectname$
.
<VSTemplate Version="3.0.0" Type="ProjectGroup" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>BackEnd Project</Name>
<Description>BackEnd Project</Description>
<ProjectType>CSharp</ProjectType>
<SortOrder>0</SortOrder>
<CreateNewFolder>false</CreateNewFolder>
<ProvideDefaultName>false</ProvideDefaultName>
<EnableLocationBrowseButton>false</EnableLocationBrowseButton>
<CreateInPlace>false</CreateInPlace>
<Icon>templateIcon.png</Icon>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="$projectname$.BackEnd.API">ProjectName.BackEnd.API\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.BackEnd.Data">ProjectName.BackEnd.Data\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.BackEnd.ServiceDependencies">ProjectName.BackEnd.ServiceDependencies\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.BackEnd.Services">ProjectName.BackEnd.Services\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.BackEnd.UnitTests">ProjectName.BackEnd.UnitTests\MyTemplate.vstemplate</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.Shared.Dto">ProjectName.Shared.Dto\MyTemplate.vstemplate</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
</VSTemplate>
Now, you need to select all the files and compress them into a zip
with any name you want. Then move the zip
to the %USERPROFILE%\Documents\Visual Studio <version>\Templates\ProjectTemplates
folder.
All that’s left is to open a solution in Visual Studio and add the project.
As you can see, all the projects are created inside your solution.
Note: since the process is tedious, in the project where I have my default backend, I included a small tool that I wrote to automatically generate the .vstemplate files. It’s not fully polished yet, but if you want to take a look, here’s the link:
Conclusion
- In this post, we saw the usefulness of creating templates for our C# code
- We covered how to create templates for a single C# project
- We covered how to create multi-project templates for C#
If there is any problem you can add a comment bellow or contact me in the website's contact form