Creating Opinionated .NET Templates for your Organisation.

Lee Dale
4 min readJul 2, 2021

--

When designing and building your application with a microservice approach you will find that you will be continually creating the same projects over and over with the same configuration and folder structure. When you add in Docker files, DevOps pipelines and other technologies you may have chosen it makes sense to try and standardise on your approach and this is where dotnet templates can be useful.

Being Opinionated

The benefit of templates comes when you encapsulate the use of technologies that your organisation wants to standardise on and avoid being too generic. For instance, in my organisation, we have decided to use Azure DevOps Pipelines for CI/CD and Docker for containerizing our applications. It makes sense to build templates that simplify the use and setup of these things for each new service. That way you know that when a developer creates a new service using the dotnet new command their project is structured in a standardised way from the start and boilerplate code and configuration is already done for them. Reducing friction when creating new services.

What is a dotnet new template?

When using the dotnet new command to create projects you can select from a list of installed templates. There are the standard templates that ship with the dotnet SDK, but you can also create your own templates

Running dotnet new with no arguments will display a list of installed templates.

How can I install my own templates?

Once you have created a set of templates you can package these up into a nuget package and store that nuget package in your private nuget feed so that anyone in your organisation can install the templates for their own use.

Creating a template project

The first thing you must do is create the correct folder structure for your templates and create a project file.

I am going to create templates for three different .NET project types

· Web API projects

· Class Library projects

· Executable projects (.exe)

As you can see here is my folder structure:

The Deployment folder houses the Azure DevOps pipeline that will package and deploy the nuget package to my private nuget feed hosted in Azure DevOps.

The Templates.csproj files looks like this:

This will include the content located in each of the template folders. In each of the template folders you will create the project structure that you want to be created when you create a new project from that template. For example, the Web API project structure will look like the following:

As you can see the files and folders inside the ApiTemplate folder are just the same as a standard .NET Web API project structure with the exception of the .template.config folder.

The .template.config folder is a special folder with a template.json file inside it. The template system will look for this file in this folder to get the metadata for the template itself. The template.json file contains the following:

Most of this file is self explanatory but a couple of things to note.

1. The value of the identity field is used when installing the template using dotnet new -i

2. The value of the name and shortName fields is displayed when running dotnet new to list the installed templates. The shortName is what you will use when running dotnet new for example. dotnet net my-webapi -o “My.Web.Api” will create a project based on your WebApi template.

Packaging and deploying the template

Once you have created your project templates you can package all the templates up into a nuget file using the dotnet pack command which looks at the Templates.csproj find the files to include in the package.

Once you have a .nupkg file you can deploy this to your private nuget feed.

Installing the template

Once you have your nuget package sitting in your feed. Anyone can then install the package to their system by running the

dotnet new -i <NUGET_PACKAGE_ID> 

command.

The package ID will be the identity property located in the template.json file. In this case My.Templates.Api

GitHub repo

You can download the code for the three templates at the github repo. These are opinionated templates that setup projects for use with Azure DevOps by including pre-configured pipeline .yaml files, Docker by including a Dockerfile and .NET 5.0. All of which can be changed for your own use.

Documentation

The official Microsoft documentation for dotnet templates can be found at https://docs.microsoft.com/en-us/dotnet/core/tools/custom-templates

--

--

Lee Dale

I am a Lead Software Developer with a keen interest in application security . Currently studying for an MSc in Cyber Security.