This page looks best with JavaScript enabled

Dotnet 6 is Refreshingly Simple

 ·  ☕ 3 min read

There are numerous things to love about the new .NET 6, but for me one key thing stands out - .NET now seems more approachable than ever!

Take a straight-forward example. A new .NET Web API project would look like this -

  1. A startup.cs file with generated code
  2. A Program.cs file with more lines of code
  3. A lot of other files

The two file dependencies induced that warm fuzzy feeling in ASP.NET developers for sometime now.

In addition, you have code that uses a wierd using syntax everywhere with brackets. Sure, it creates better compartmentalized & namespaced code, but did not help calm my nerves when I was evaluating ten different back end technologies to make a quick start (a while ago).

C# 10 and the new ASP.NET 6 will do wonders in the “make code look deceptively simple” area.

There is no startup.cs. The Program.cs file now looks cool with the new features -

  • top-level statements
  • global using directives
  • file scope namespace declarations (bear with me here)

A minimal API structure now looks like this (Program.cs file) -

1
2
3
4
5
6
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.Run();

A full-blown MVC web API application from the new template in .NET 6 looks like this..

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

“Whoa..!” is right.

I am not proud of the fact that the additional statements & brackets were putting me off a long time. While I love C# for what it can do, I seldom choose C# for personal projects for all the silly reasons - I did not need the complexity, did not like to do lot of typing or “auto filling”, and did not have a lot of patience for code scrolling tens of pages.

NodeJS simply offered a better alternative, was way simpler to start (especially in Fastify or Express), and came at a cost that did not quite matter to me. And, I am a happy Javascript user - no one will take that away from me.

I am a fan of the new ASP.NET structure - for probably all the wrong reasons for a “real” developer. I will probably delve more into how a further “simplified” folder structure can make the ASP.NET world more exciting to work on. The “traditional” structure has been full of folders and a lot of files. See the eShopOnWeb example on Github. The minimal API structure at a glance by Martin Fowler is a good start for a new approach to build Web APIs on .NET 6, but what is even more interesting to me is the module-based architecture outlined in this post by Tim Deschryver.

WebApplication
│   appsettings.json
│   Program.cs
│   GlobalImports.cs
│   WebApplication.csproj
│
├───Modules
│   ├───Cart
│   │      CartModule.cs
│   └───Orders
│       │   OrdersModule.cs
│       ├───Endpoints
│       │       GetOrders.cs
│       │       PostOrder.cs
│       ├───Core
│       │       Order.cs
│       │───Ports
│       │       IOrdersRepository.cs
│       │       IPaymentService.cs
│       └───Adapters
│               OrdersRepository.cs
│               PaymentService.cs

Awesome time this.

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things