ASP.NET Core MVC Interview Question #177

How would you design a controller for a complex business workflow?

Senior Practical MVC Scenarios & Production Architecture Senior Advanced

Quick Interview Answer

Keep the controller focused on translating HTTP input into an application request and translating the result into an HTTP response. The application service should own the workflow, transactions, and coordination of domain operations.

Detailed Explanation

Keep the controller focused on translating HTTP input into an application request and translating the result into an HTTP response. The application service should own the workflow, transactions, and coordination of domain operations.

This prevents business rules from becoming tied to MVC and makes the workflow reusable from APIs, jobs, or other entry points.

Code Example

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> PlaceOrder(
    CheckoutViewModel model,
    CancellationToken token)
{
    if (!ModelState.IsValid)
        return View(model);

    var result = await _checkout.PlaceOrderAsync(model, token);

    if (!result.Succeeded)
    {
        ModelState.AddModelError("", result.Error);
        return View(model);
    }

    return RedirectToAction("Confirmation",
        new { id = result.OrderId });
}