Tuesday, July 23, 2013

Controller Micro-Architecture

A Controller supplies one or more web-service API's.  Those API's are most often closely related to a single Model.  For example, one has a Widget model and a WidgetsController.  That close relationship can cause the lines of responsibility to become blurred.

Controllers also coordinate with the presentation of the response.  Again, the lines of responsibility can be a bit a fuzzy - does the logic go in the controller or in the .erb file?

Here is my sense for controllers:
  • Functional Authorization
    • Does the user have to be signed-in to access the controller's method?
    • Is the user allowed to access the method?  For example, is it superuser-only?
  • Check for basic argument correctness
    • Required parameters
    • Extraneous parameters (e.g. white-list)
  • Data Authorization
    • Is the user allowed to see/modify/delete that particular data?
  • Invocation of domain logic
    • Error handling
  • Assigning domain logic results to instance variables
    • For use by result rendering (HTML, XML, JSON)
  • Selection and invocation of the result rendering code
    • Often done implicitly by Rails!
    • Correct presentation of errors
Next:  Best (for some relative value of best) Practices for each area of responsibility.

No comments:

Post a Comment