Project template in .Net

Saber Motamedi
4 min readJan 2, 2024

--

What is the Project template?

The project template is the most common ability we work with, When ever you want to create a new project, you use it.

Visual Studio project teamplate

In the green box, you can see the most used templates.

There is another way to create a project with PowerShell or Command Prompt.

Open Command Prompt (CMD) and run the following command.

dotnet new list

You should see a result similar to this image.

these are installed templates on your machine that are useable with this command:

dotnet new [Short Name] -o [Application Name]

for example, by running this command:

dotnet new console -o MyApp

I could create a console application, like this picture.

where should we use it?

Whenever you want to create a new project, there is one less noticeable point.

You can create your customized template and save your time, by config logs, EF core, mapper, and layers definition.

When should use it?

Freelancers:
It’s very common to create new applications when you work as a freelancer, I know that you have another way, to clone a new project from the GitHub repository and the question is what is the point of using a template?

When you create your project with a template, you can use the project name at first, but when you use a cloned project, you have to rename NameSpaces and Projects.

Repetitive applications:

In some companies, it’s common happiness to create new projects, such as eCommerce websites. in this case, you can create yours and save your time.

SaaS & Microservices:

When you work on these applications, It’s beneficial to use a template for your services.

How to create it?

for creating a template, first of all, create your project with all dependencies and requirements.

and add a [.template.config ]folder to your root project, inside the folder, put [template.json ].

fill template.json by this content:

{
“$schema”: “http://json.schemastore.org/template",
“author”: “SaberMotamedi”,
“classifications”: [
“Web”,
“SPA”,
“ASP.NET”,
“Clean Architecture”
],
“name”: “Light Clean Architecture Solution”,
“description”: “A Light Clean Architecture Solution Template for creating a WebAPI Application with ASP.NET Core.”,
“identity”: “Light.Clean.Architecture.Solution.CSharp”,
“groupIdentity”: “Light.Clean.Architecture.Solution”,
“shortName”: “lca-sln”,
“sourceName”: “LightCleanArchitecture”,
“tags”: {“language”: “C#”,“type”: “project”},
“preferNameDirectory”: true,
“sources”: [
{
“source”: “./”,
“target”: “./”,
“exclude”:
[“.template.config/**/*”,
“templates/**/*”,
“**/*.filelist”,
“**/*.user”,
“**/*.lock.json”,
“*.nuspec”
],
“rename”: {
“README-template.md”: “README.md”
}
}
]
}

It seems a little complex but It’s not, what do we have here? let's dive in.

  • author: Indicates the author of the template.
  • classifications: Describes the classifications or categories associated with the template, such as "Web," "SPA," "ASP.NET," and "Clean Architecture."
  • name: Specifies the name of the template.
  • description: Provides a brief description of the template.
  • identity: Represents a unique identifier for the template.
  • groupIdentity: Defines a group identity for the template.
  • shortName: Specifies a short name or abbreviation for the template.
  • sourceName: Defines the source name for the template.
  • tags: Includes additional tags for the template, such as the programming language (C#) and project type.
  • preferNameDirectory: Indicates whether to prefer using the project name as a directory.
  • sources: Describes the source files and their configurations. It specifies the source and target directories, files to exclude, and any renaming rules.

now run this command:

dotnet new — install ./YourProjectDirectory

after that run this command:

dotnet new list

you have to see your template name in the result

by running this command you can create your new application based on your template:

dotnet new [Short Name] -o [Application Name]

I created a simple clean architecture template and shared it in Nuget, for more information about that check this article:

Light Clean Architecture — part 1

If you have any questions, please do not hesitate and ask me in the comments or throw e-mail.

--

--

Saber Motamedi
Saber Motamedi

Written by Saber Motamedi

Be the change that you wish to see in the world

No responses yet