Tuesday, December 6, 2016

What Code Belongs in an MVC Controller

The purpose of a controller is to act as a conduit between each user interaction and system response. Typically this involves three steps:

  • readRequest(). For a web server this means reading the inbound HTTP request, analyzing the headers, taking care of authorization.
  • doSomething(). Now that the server knows what it's being asked to do, the server can go ahead and do something useful.
  • writeResponse(). After the server has finished its job or kicked off a long-running process, it should write a response back to the user to let the user know how things went.

In a different sense, a controller's action method is just a wrapper for a function that executes actual business logic, a wrapper that translates an HTTP request into function args.

This setup makes sense to me, but what other approaches are there to writing good controllers? Please share your comments.