Not Quite Your Grandma's MVC Pattern

Feb 27, 2007

Model-view-controller (MVC) is an architectural pattern used in software engineering. This dry sentence begins the Wikipedia article on MVC. MVC is the foundation pattern of most modern application frameworks, especially on the web.

I have always had a bit of a beef about the description. In my mind, the pattern is really M-V-A-C, as in Model, View, Action, Controller. Maybe this is splitting hairs but to me it matters; the controller should not update the model directly. There is a need to decouple the driver of the application from its data.

If you examine many MVC web frameworks, you actually see this in practice. In HTTP you have two connections between the "application" and the "user", a request, and a response. Generally there are also two kinds of requests, GET and POST. A GET request asks the application to respond with a view of some data from the model. A POST request asks the application to do something, which usually involves updating the model and responding with a new or updated view. Note the bolded words.

In a tradional view of this pattern, the controller is usually assumed to update the model directly, then respond with a new or updated view of its choosing. In practice the act of updating the model is usually handed to an action, which may also first perform any validation either directly or indirectly (depending on the framework). The success or failure of the action then prompts the controller to return a view.

So in my view the pattern is really this:

The view is basically the reader of the model, and the action is the writer. The controller does not directly access the model.

None of this is really new, as in practice most MVC frameworks work this way, but the typical description always seemed to be missing the crucial detail A.