Keep your codebase consistent with custom `dotnet new <TEMPLATES>`
You’ve probably used the
dotnet new webapicommand. You might not have given it much thought, but why did you not write the *.csproj file and the
program.cs from scratch? And who do you write other files from scratch?
Building plumbing takes time and it hardly delivers any business value. Scaffolding boilerplate code increases productivity because it allows you to focus on writing code for the things that matter.
If you are a dotnet web developer, big chance you’re working with Angular too. The Angular CLI took generating boilerplate code next level compared to the dotnet CLI. With the Angular CLI you can scaffold much more than just a blank project.
ng new component, for instance, creates a new component (including tests).
ng new service creates a new service. And so forth.
Would it not make much more sense to scaffold typical controllers, aggregates, tests, or deployment pipelines in dotnet too? Imagine being able to just type
dotnet new controller ,
dotnet new aggregate ,
dotnet new repository ,
dotnet new integrationtest, or
dotnet new deploymentpipeline ?
Using dotnet templates in your project(s)
In my entire career, I have never come across companies that have similar codebases. Everybody does things differently (which makes sense). That’s why I think it makes sense the
dotnet new CLI is extendible. You can easily create templates that allow you to scaffold things in a way that makes sense for your company.
Ideally, things in a codebase are built following a similar philosophy. Code is harder to maintain when, for example, some controllers access the data context directly, others use a repository pattern, some derive from ApiControllers, and others are minimal APIs.
It’s easy to deviate from a convention when you’re starting from scratch. There are plenty of ways to make things work in dotnet.
Scaffolding code is a good way to provide a standard way of doing things in a code base. When you need to create an endpoint and the CLI generates it for you, the CLI is your guide in telling you how to do things. As a result, you’ll have much more consistency in the code.
From a microservices perspective, it makes even more sense to use this approach. In a…