ASP.NET Core Web API Interview Question #104
Should a controller contain business logic?
Dependency Injection, Services, EF Core, Async & Data Access Mid-Level Intermediate
Quick Interview Answer
Controllers should mainly coordinate HTTP concerns: accept input, invoke application services, apply endpoint-level authorization, and translate outcomes into HTTP responses. Complex business workflows should generally live in application or domain services.
Detailed Explanation
Controllers should mainly coordinate HTTP concerns: accept input, invoke application services, apply endpoint-level authorization, and translate outcomes into HTTP responses. Complex business workflows should generally live in application or domain services.
Thin controllers improve testability and reduce duplication because the same business operation can be reused from other entry points without depending on ControllerBase.
Code Example
[HttpPost]
public async Task<ActionResult<OrderResponse>> Create(
CreateOrderRequest request,
CancellationToken token)
{
var result = await _orderService.CreateAsync(request, token);
return CreatedAtAction(nameof(Get), new { id = result.Id }, result);
}