Create .NET Core Project Template

In this blog let’s see how to create custom template that will hep us to speed up our development process and also helps us in maintaining each solution at ease.

Consider we are developing a microservice application , as a team we should follow the below standards across all services.

  • Folder structure
  • Clean Architecture
  • OpenAPI specifications
  • Resiliency
  • Authentication
  • Authorization
  • Health Check

Tracking and maintaining these standards across all services are painful and one should invest considerable amount of time in creating these standards across each services. In order to avoid this wasted effort across all services , .NET SDK provides you the capability to create your own custom templates.

If you wanted to share this template across all developers , one way to do it is Nugetize the custom template project and install it from a NuGet package on any NuGet feed, by referencing a NuGet .nupkg file directly, or by specifying a file system directory that contains the template. I have covered this part in my next blog , check this out if you want to know on how to nugetize the package and upload it to nuget.org.

.NET command-line interface is a cross-platform toolchain for developing, building, running, and publishing .NET applications. This comes along with your .NET SDK and you can also install this tool as a workload alongside with Visual Studio on selecting .NET core platform development tools.

/wp-content/uploads/2021/05/windows-install-visual-studio-2019-1024x514.png

Once you have installed .NET CLI , run below command in your command prompt

1
dotnet new --list

This will gives you the list of built-in templates that are available as part of your .NET SDK

/wp-content/uploads/2021/05/Desktop-screenshot-1024x592.png

Open solution explorer and create folder structure in below format

1
2
3
4
5
6
├── src
│   ├── Content
│         ├──ProjectTemplate
│              ├── .template.config 
│                     └── template.json
├── sln

Create empty solution under src folder using below command

1
dotnet new sln -n customtemplate

/wp-content/uploads/2021/05/Desktop-screenshot-1-1024x592.png

For demo purpose we will create a console application using visual studio and place it under Content folder.

1
dotnet new console -n ConsoleTemplate

/wp-content/uploads/2021/05/Desktop-screenshot-3-1024x592.png

Earlier one should tokenize each namespace of the class file in order to replace it with the name provided by a user at the time of installation. Below is the representation how they used to create a class files with namespace tokenized.

So whenever we are planning to create a custom template we should maintain two solutions and it created lot of issues. With the help of Template Engine we don’t need to tokenize the namespace across all files.

/wp-content/uploads/2021/05/carbon-9-1024x341.png

The template engine is a open source that will look for any occurrence of the sourceName mentioned in the config file and replace it in file names and file contents. The value to be replaced with can be given using the -n  or --name options while running a template. If no name is specified, the current directory is used

Now let’s create template.json file that defines the configuration information to the template engine. Choose “https://json.schemastore.org/template.json” as your schema

/wp-content/uploads/2021/05/image-1-1024x430.png

/wp-content/uploads/2021/05/carbon-10-1024x341.png

/wp-content/uploads/2021/05/carbon-11-1024x341.png

Now let’s install our custom template using the below command . Replace the folder path with your custom path , where you have placed your console project.

1
dotnet new --install D:\Demo\CustomTemplate\src\Content\ConsoleTemplate

/wp-content/uploads/2021/05/Desktop-screenshot-4-1024x847.png

Now that we have installed our custom template , lets create a new console project based on this template and let’s see the output of it. use the below command to create a new project using our template. Replace the shortname “customconsole” with your template’s shortname

1
2

dotnet new customconsole -o SuccessDemo

/wp-content/uploads/2021/05/Desktop-screenshot-5-1024x847.png

/wp-content/uploads/2021/05/Desktop-screenshot-6-1024x847.png

We can see “Hello World from ConsoleTemplate!” has been replace to Hello World from SuccessDemo!

/wp-content/uploads/2021/05/Desktop-screenshot-7-1024x847.png

In this blog we have covered :

  • Why we need Custom Template
  • .NET CLI overview
  • Folder structure for Custom template
  • Role of Template Engine
  • Content of template.json

In our next blog let’s see how to nugetize this custom template, share it across team and make use of it in a effective way . Find the github repo for this blog here and feel free to leave a comment and fork the source code.