2

Reducing Boilerplate Code in .NET Applications with Command Line Switches

 2 years ago
source link: https://nodogmablog.bryanhogan.net/2022/09/reducing-boilerplate-code-in-net-applications-with-command-line-switches/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Reducing Boilerplate Code in .NET Applications with Command Line Switches

Over the past few years, I’ve been using Visual Studio less and less, preferring VS Code, and more recently Rider.

I also use the command line more than in the past for project creation, running the application, testing, etc. Regarding project creation, I use the command line switches to fine-tune the project, and how it works.

Sometimes I want top-level statements, sometimes I want the old-style Program.csfile with aMain` method. I often don’t want API documentation, HTTPs redirection, etc.

You could use dotnet new webapi, and then trim out what you don’t want.

But there is an easier way. Try this to see all the options -

dotnet new webapi --help

For simple things I use -

dotnet new webapi --no-https --no-openapi -n MyWebApi

This reduces the Program.cs from 14 to 6 lines of code.

normal_vs_reduced_huc006f6366da0c8db99ec33a98517a826_37483_f891ff356534b83282b8085eefb3ff1c.webp

Other useful switches are -

   --use-minimal-apis // Use minimal APIs instead of controllers
   --use-program-main // Use a traditional Main method instead of top-level statements

Keep in mind, all templates have a variety of switches available.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK