.NET 6 New Project Templates and Minimal APIs. Are you ready?

 .NET 6 marks the completion of Microsoft’s plan to unify the different .NET implementations. The unification of .NET is not simply merging code from different sources into one API. There are quite some enhancements and new features that were added to the runtime and language.

You’ve got:

  • Improved Performance
  • Hot Reload
  • System.Linq enhancements
  • Minimal Web API
  • New features in C# 10

The goal for .NET unification is to simplify the choices and learning curve for new developers, and at the same time make it easier for experienced developers. That’s why the minimal APIs for hosting and routing in web applications, and new project templates were introduced.

SIMPLIFIED START-UP CODE AND MINIMAL API

The .NET SDK project templates were updated to use modern C# language features and patterns such as:

  • Top-level statements
  • async Main
  • Global and implicit using directives
  • File-scoped namespaces
  • Target-typed new expressions
  • Nullable reference types

What does this mean? When you run the command `dotnet new console` to create a new Console project, this is what you get.

```csharp
Console.WriteLine("Hello, World!");
```

You can add more code to _Program.cs_, just like you can add more statements to the `Main` method in the old template. You can even add functions, which will be created as local functions nested inside the generated Main method. This streamlines the experience to make it easier to onboard new developers, and make it faster for experienced developers to start building without unnecessary code like before.

INTRODUCING MINIMAL APIS

The experience is also simple for new web developers and existing customers who want to build microservices or small HTTP APIs. The new _web_ template gives you a fully functional HTTP API just using a single file with a few lines of code. When you run `dotnet new web`, you get a project with a single endpoint defined in _Program.cs_.


```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();
```

CONCERNS ABOUT THE NEW TEMPLATES

Not everyone wants these new project templates, and the reasons vary from person to person. Some have asked to have both the new and old template in the SDK, while some would like to have these new features available as options when creating the project. All for the flexibility to opt-out of the new feature template and continue with the familiar, traditional template. The team at Microsoft has made it clear that they do not plan on changing the template for .NET 6 GA.

If you want to use the old style with an explicit `Main` function and `using` statements, you can add them manually and disable implicit using in the project settings.

There’s also an unofficial template that can be used to create projects with the old style. It’s from Jonathan Pobst and you can install it using the command `dotnet new -i Classic.Console.Templates`. To create a new project from the command line, you run `dotnet new console-classic`. The options available for the CLI are:

– –nrt: If true, turns on nullable reference types. [defaults to ‘true’]
– –topLevelProgram: If true, auto-generates the top-level statements, skipping explicit main methods, class and namespace. [defaults to ‘false’]
– –implicitUsings: If true, auto-imports many common using statements. [defaults to ‘false’]

You can also use the _Classic.Console.Templates_ template from Visual Studio and select your preferred options.

SUMMARY

.NET 6 is a big step towards having a unified .NET platform. There are many wonderful features like hot reloading, minimal API, Inner-Loop performance, and many more. The new features and patterns provide a streamlined experience to make it easier and faster to build applications. 

Post a Comment

0 Comments