COMPUTER ENGINEER

Jump to examples
Blog
Next.js / ASP.NET Core / SQL6 min study
18/ 41

The application layer

Controllers and minimal APIs

Two styles that do the same job, and the thin-controller rule.

UnderstandStudy an examplePractise

Understand the concept

In ASP.NET Core endpoints can be written in two styles. Controllers group related endpoints in a class, define routes and behaviour with attributes and, with [ApiController], provide automatic model validation, ProblemDetails and other conveniences. Minimal APIs define endpoints with calls such as MapGet and MapPost directly in Program.cs or in grouped extension methods, with less ceremony and a faster start.

Whichever you choose, the endpoint should be ‘thin’. Its job is understanding HTTP: take the input, hand the work to the application layer and map the result to the right HTTP response (200, 201, 204, 404…). Business rules, database queries and sending e-mail live in services, not endpoints.

This separation makes testing easy: a business rule can be unit tested without HTTP. And when the same rule must be used from another entry point (a background job, a message queue) it does not get copied.

WHEN TO USE IT

Thin endpoints keep business rules testable and reusable.

COMMON PITFALL

Stuffing queries, validation, e-mail and logging into a controller method until it reaches 200 lines.

01 / PRACTICE

One topic. Three levels of depth.

Illustrative example
app.MapGet("/api/health", () => Results.Ok(new { status = "ok" }));

A one-line endpoint with a minimal API.

This page connects to no real server or database. Code samples are illustrative for .NET 8, Next.js and SQL Server; timings are example values in the simulation, not measurements. Try samples only in your own development environment; never share real connection strings, tokens or passwords.

02 / YOUR TURN

Try it yourself first.

A controller method calculates the order total, applies a discount, writes to the database and sends e-mail. What do you change to test the discount rule?

Show the solution and explanation
Worked solution
İndirim hesabını saf bir sınıfa/metoda taşı (ör. PricingService.ApplyDiscount)
ve birim testle dene. Controller yalnız servisi çağırır ve sonucu HTTP’ye çevirir.
E-posta ve veritabanı arayüzlerin arkasına alınır.

A business rule free of HTTP and external dependencies can be tested quickly and reliably.