BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News ASP.NET Core 6 to Challenge Python and Node

ASP.NET Core 6 to Challenge Python and Node

This item in japanese

Bookmarks

When comparing .NET to Python or Node, one of the complaints is the amount of ceremony around setting up a new project. Even the simplest web service is expected to have a Program class, a Startup class, and one or more Controller classes. By contrast, here is a complete Python web service demonstrated by Reddit user ammar2:

from fastapi import FastAPI
import python_weather

app = FastAPI()

@app.get("/api/weather/washington")
async def root():
    weather = await python_weather.Client().find("Washington DC")
    return weather

In order to compete in low code scenarios, Microsoft has dramatically reduced the amount of code necessary. Here is a minimal example:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;

var app = WebApplication.Create(args);
app.MapGet("/", (Func<string>)(() => "Hello World!"));
app.Run();

As you can see, the developer doesn’t even need to define a class. Instead, developers can combine C# 9’s top-level statements and a simplified ASP.NET Core API.

Further enhancements in C# 10 will remove the need for the (Func<string>) casting operation. Here is an example of calling a TODO API using the proposed syntax:

app.MapGet("/api/todos", [Authorize](TodoDbContext db) => db.Todos.ToListAsync());

The above line can be interpreted as:

  1. Register an HttpGet method on the path /api/todos
  2. Require the user is authorized
  3. Inject a DB Context of type TodoDbContext
  4. Asynchronously return a list of Todo objects from the database

The ASP.NET Core 5 equivalent would be

[ApiController]
[Route("/api/todos")]
public class TodoController : ControllerBase
{
	readonly TodoDbContext _db;
	public TodoController(TodoDbContext db)
	{
		_db = db;
	}

	[HttpGet]
	[Authorize]
	public Task<Todo> GetAll()
	{
		return _db.Todos.ToListAsync();
	}
}

There are some limitations to this model. For example, there is no place to put the Swagger documentation. Normally this would appear as XML docs on the controllers and their methods. And for larger applications you lose the natural organization of having the REST methods separated into controller classes.

There are also some benefits when it comes to performance. According to Daniel Roth of Microsoft,

These new routing APIs have far less overhead than controller-based APIs. Using the new routing APIs, ASP.NET Core is able to achieve ~800k RPS in the TechEmpower JSON benchmark vs ~500k RPS for MVC.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Silly

    by Sam King,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    The "race to the bottom" in terms of project size Microsoft has been working on misses the forest for the trees. Normal developers don't care how much code it takes to run something; they care how easy it is to get started and then use in steady-state. This is where Microsoft's huge advantage has been for decades (via Visual Studio) that its current crop of leaders just doesn't seem to understand.

    Microsoft is obsessed about these "learner" scenarios despite the ample evidence that C#/.NET was and is an industry leader and has never had an adoption problem.

    Isn't it silly that C# users have been asking for lambdas to have natural types since lambdas were added but the straw that broke the camel's back was this silly initiative?

    Don't get me wrong; the end result of all this work is mostly nice, but a lot of it is effort that could have been better spent elsewhere. They should be focused on how to add visual design experiences around modern frameworks into VS if they really want to remain the market leader here.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT