Introduction
Microsoft Visual Studio comes with many standard project templates, which leave developers to configure various aspects of a new project.
Learn how to create custom project templates that provide starter code like preloaded NuGet packages, code to set up logging, defined settings for configuration files, and more.
Documentation
Rather than repeat all of Microsoft's documentation, see the following.
- Create custom project and item templates
- Create multi-project templates
Preparation
Before jumping in, have a plan and be consistent across projects.
If the plan is to work across various project types, configuration files should have the same structure for connection strings, regardless of how connections might be accessed. Use the same format when implementing logging that writes to files. Being consistent across projects means going from project to project makes it easy to flow code and easy to maintain.
Consider when code is duplicated across projects, create NuGet packages, which makes maintenance easy.
Create two Visual Studio solutions
Create a Visual Studio solution for project templates and another for testing templates. By having two solutions, the test environment is not influenced by the templates. Also, there may be cases where a test project needs to be removed, but Visual Studio has a lock that can only be released by closing Visual Studio.
Create folder shortcut
Having this shortcut makes it easy to remove unwanted templates.
C:\Users\USERNAME\Documents\Visual Studio 2022\Templates\ProjectTemplates
Before creating a template
Before starting the template export process, create a meaningful template name and description for the following dialog box. The images are optional, as the template name and description are the most important aspects of identifying the templates.
NuGet packages
If there are packages that may be needed but might not be needed for all projects, consider adding those packages and commenting them out as shown below in the project file.
Creating a single project template
The following instructions are from Microsoft.
- Create a project.
- Edit the project until it's ready to be exported as a template. For example, you might want to edit code files to indicate where parameter replacement should take place.
- On the Project menu, choose Export Template.
- On the Choose Template Type page, select Project Template. Select the project you want to export to a template, and then choose Next.
- On the Select Template Options page, enter a name and optional description, icon image, and preview image for your template. These items will appear in the dialog box where you create a new project. Select Finish.
- The project is exported into a .zip file and placed in the specified output location, and, if selected, imported into Visual Studio.
Once completed, move to Visual Studio for testing. Select Add new project, find the template, and add it to the solution. If NuGet packages are needed, wait for them to download.
Create multi-project templates
⚠️ The project name used will create a folder under the root folder so give it a meaningful name. There should be an option to place the projects at the root of the solution but in configuring the template this failed to work.
The following instructions are from Microsoft.
Multi-project templates act as containers for two or more Visual Studio projects. When you create a project that's based on a multi-project template, every project in the template is added to the solution.
The above documentation describes several variations; an example included with the source code has two class projects created in two solution folders.
The two project templates are in the root folder of the Visual Studio folder, named TwoProjectTemplateSample. To use them, copy the ConfigurationAndLogging.zip file to the template folder.
Add new project, type log to find the template.
After adding the two project template
Maintenance
As time passes, new .NET Core Frameworks are released, and new versions of NuGet packages are available. To handle these, either modify the template and redeploy or make the changes after adding the project to a solution.
Source code
All project templates have Serilog setup to write to a new folder each day, and appsettings.json is consistent across projects.
Summary
Custom templates can save a developer time and keep things consistent. Make sure to take time planning out and testing templates.
Item templates
To following in another article.
Top comments (0)