Thursday, December 18, 2008

MVC Arcana

In studying the canonical (definitive) Django book from Apress by Holovaty and Kaplan-Moss, via O'Reilly's Safari (not to be confused with Apple's browser by that name), I'm running across a seeming subculture war around the meaning of 'MVC', which these Django journalists might want to call 'MTV' instead (clever pun, if you know your television):
If you're familiar with other MVC Web-development frameworks, such as Ruby on Rails, you may consider Django views to be the "controllers" and Django templates to be the "views." This is an unfortunate confusion brought about by differing interpretations of MVC. In Django's interpretation of MVC, the "view" describes the data that gets presented to the user; it's not necessarily just how the data looks, but which data is presented. In contrast, Ruby on Rails and similar frameworks suggest that the controller's job includes deciding which data gets presented to the user, whereas the view is strictly how the data looks, not which data is presented.

[ from The MTV Development Pattern in The Definitive Guide to Django: Web Development Done Right ]
I sense ambivalence around 'C for Controller' as that just scares people away (engineers as control freeks), and Djangonauts, like Pythonistas more generally, just wanna be warm and fuzzy (not controlling). The T is for Templating, and the attitude here is everyone uses boilerplate, that's all a web framework is anyway.

My bias is to rescue the word 'control' from its negative spin, which doesn't mean we can't make fun of controlling individuals, control freaks and so on. I myself take pride in developing the (dismissive) progressive tense of this latter, as in "oh, he's just control freaking about it".

However, in aviation we have cockpit controls, right in there with the instruments, and we actually want them to perform as advertised. Likewise we admire self control (self discipline). Control, in an engineering sense, isn't necessarily evil, or if it is, it's a necessary evil, like original sin.

So yeah, it cuts both ways, definitely.

So in the context of Django's controlling (driving), the urls- directing and switchboarding via regular expressions is "C work". And by the way I like the emphasis in Django world on the crafting of urls, making them elegant, treating them as user-facing (which they are), like not putting php or jsp or asp on the end of everything, as if users care (sneaky subliminal advertising though, until they notice).

The Python interface to the SQL engine is also all about control, in the sense of giving users vicarious access to (control over) their own records, e.g. medical records (they like that, don't mind "controlling" in that sense).

Visualizations, on the other hand, come in with the XHTML, CSS, JQuery, FLOT etc. In development work, I'm usually working closely with a client on this part, as we're talking what's public-facing, what the urls actually bring to the screen, in terms of streaming media, colorful (sometimes audible) interactive experiences. Most ecommerce sites are hoping for repeat business, are looking for long term, loyal customers, not "first time users" at every turn.

To confuse matters a little, in the context of a visual, such as a web page, controlling widgets may appear, courtesy of JavaScript or whatever, which is what end users tend think of as "screen controls" (legitimately, as they're interactive through touch pad and keyboard). Under the hood, they're populating an HttpRequest object and sending it via POST or GET, for which an HttpResponse will be given, Apache mediating perhaps, with Django behind the curtain.

The Model is basically the persistence layer, in terms of organized information, close to the SQL end of things, all quite straightforwardly implemented as Python classes in Django, with semantics that layer atop the SQL itself, using the manager type objects (used to update, insert and delete from the class-defined tables).

In terms of control rooms (a theme of this blog) -- newsrooms not unrelated -- we cover MVC in SuperMarket Math (related to Home Economics). Retailers (e.g. coffee shops), wholesale outlets, use MVC frameworks to drive point of sale devices, while meanwhile updating tables in the background. Web frameworks control the majority of shopping carts these days (a predominant metaphor for buying online). Customers typically enjoy that exercise of control, of voting with their hard earned dollars, for goods and services they freely choose.