Dotnet Core Templates

Dotnet provides an easy way to create custom templates very useful for boilerplates.

I've often been in the need of creating again and again microservices that follow the same folder structure, dependencies, naming conventions and some basic implementation. At first I used to copy-paste a solution and adapt it to my needs... until I discovered templates.

Remember when you followed a basic tutorial on dotnet core and saw something like
dotnet new console -o Foo
that created a hello world basic console project? This is the same, only this time we're defining what our template will exactly create.

There are different approaches to achieve this. My recommendation is to create a solution with the boilerplate that you want to use often. Make sure it works.

Then you simply rename each project, file, folder or solution file which name may be different. I like using the word "__Template__" for this.

So for example instead having a:

MyBoilerplate.sln
src
|--MyBoilerplate.Host
    |--MyBoilerplate.Host.csproj

we can rename it to:

__Template__.sln
src
|--__Template__.Host
    |--__Template__.Host.csproj

Then we will create a folder at the root of the boilerplate called
.template.config
and within it a
template.json
with the following content:

The template specifies which word is the placeholder to be replaced when used.

I have created an example with a multiproject solution with dotnet core 3 to demonstrate how it works and further instructions on how to install, uninstall and use the template here at my repo: https://gitlab.com/DiegoDrivenDesign/DiDrDe.Templates

Comments