SOLID & Design Patterns Interview Question #147
How would you prevent controllers from becoming too large?
Anti-Patterns, Refactoring, DI Lifetimes & Senior Design Scenarios Senior Advanced
Quick Interview Answer
Keep controllers focused on HTTP concerns: receiving input, invoking an application use case, and translating the result into an HTTP or view response. Move business rules to domain/application components and data access to repositories or query services where appropriate.
Detailed Explanation
Keep controllers focused on HTTP concerns: receiving input, invoking an application use case, and translating the result into an HTTP or view response. Move business rules to domain/application components and data access to repositories or query services where appropriate.
Model binding, filters, middleware, mediator handlers, and application services can remove unrelated concerns, but avoid creating pass-through layers that add no meaningful boundary.
Code Example
public async Task<IActionResult> Confirm(Guid id)
{
var result = await _orders.ConfirmAsync(id);
return result.IsSuccess
? RedirectToAction("Details", new { id })
: BadRequest(result.Error);
}