MVC vs. Pac

Source: Internet
Author: User
Tags drupal

MVC vs. Pac 31 December 2006-PM-Larry

One of the most common mistakes I see people make when talking about Web architecture is with regards to MVC. generally it comes down to a statement such as this: It's a web app, so we have to use MVC. that way we separate the logic and presentation, which means keeping PHP out of our display layer. all the important projects do it that way. of course, such a statement is false. it demonstrates a L ACK of understanding about MVC, about Web applications, about "important projects", and about software architecture in general. let's try to clarify, with a little help from Wikipedia. modular Design modular design is a generally recognized good thing (TM) in software engineering. as in science in general, breaking a problem down to smaller, bite-sized pieces makes it easier to solve. it also allow S different people to solve different parts of the problem and still have it all work correctly in the end. each component is then self-contained and, as long as the interface between different components remains constant, can be extended or even gutted and rewritten as needed without causing a chaotic mess. I shoshould note that modular design is not the same thing as Plugin-based design, which is W Hat users need open source projects use. drupal "modules" are actually plugins. the Linux kernel, Apache, eclipse, and other high-profile projects with "loadable modules" are Plugin-based ubuntures. plugin-based ubuntures are not incompatible with modular design and in fact complement it well, but I digress... most software specified tural patterns are based around the idea that modular Design is a good thing, rather by definition. there are lots of arbitrary tural patterns for systems depending on what it is they're supposed to do. for interactive systems, the usual breakdown of components is "display component", "data storage component", and "business logic ". MVC the most commonly-known interactive system architecture is Model-View-controller, or MVC. most good desktop applicatio NS use MVC or a variant of it, sometimes with the Controller partially merged into the view. in MVC, as the pretty picture at the other end of that links shows, the model holds data, the view is the part the user sees, and the Controller is an intermediary for business logic. seems reasonable, right? Now take a closer look. in MVC, the view component has direct access to the model. the controller itself doesn't enter the picture unless there is actual change to the data. simply reading and displaying data is done entirely by the view component itself. as a result, a system can have lateral view components active at once, all reading and displaying data in a variety of different ways, even on Diff Erent systems or in different versions or different modes (GUI. textual. web ). on the flipside, however, that means the view component has to have some rather complex logic in it. it needs to know how to pull data out of the model, which means it needs to know what the data structure is (or at least a rich API in front of the model ). it needs to be able to handle user interaction itself, wi Th its own event loop. well, that rules out most wocould-be web-MVC setups. one of the most common cries of such systems is to "get database stuff away from HTML ". instead, everything is handled through a smart controller that uses a template layer to render and display output. that's not a bad design necessarily, but it is not MVC. if the display component does not have direct, random-access, pull- Access to the data store, then it is not MVC. PAC a less publicized but still widely used architecture is presentation-Modify action-control, or PAC. the two main differences between MVC and PAC are that in PAC the presentation component is "dumb" while all the intelligence resides in the Controller and PAC is layered. again, see the pretty picture. you'll notice that the presentation and your actio N components never speak to each other. the controller takes input, not the display component. the Controller has all the business logic and routing information. the presentation component is essential just a filter that takes raw data that the controller pushes through it and renders it to HTML (or WML, or XML, or text, or an icon in a graphical monitoring system, or whatever ). it's just a temp Lating system. the classic example of a PAC architecture is an air traffic control system. one Pac agent takes input from a radar system about the location of an incoming 747, and uses the presentation component to paint a picture of that blip on a canvas (screen ). another agent independently takes input about a DC-10 that is taking off, and paints that blip to the canvas as well. still another Ta Kes in weather data and paints clouds, while another tracks the incoming enemy bomber and paints a red blip instead. (Er, wait ...) the Web and Drupal a web application doesn' t map nicely to either MVC or PAC. there's no consistent event loop for the view component of MVC. for PAC the input is coming from the same source as the display, that is, in via the presentation layer (kinda ). most Web appli Cations I 've seen (and written) tend to use a sort of hybrid approach, which then gets (wronugly) called MVC. I blame sun for that confusion, mostly, but there's enough blame to go around, too. i'm going to pick on Drupal here because it's what I'm most familiar with, but this isn't a Drupal-specific observation. drupal is very much a PAC architecture. in fact, it's a rather good PAC architecture. The menu system acts as the controller. it accepts input via a single source (http get and post), routes requests to the appropriate helper functions, pulls data out of the specified action (nodes and, in Drupal 5, forms ), and then pushes it through a filter to get a presentation of it (the theme system ). it even has multiple, parallel PAC agents in the form of blocks that pushes data out to a common con VAS (page. TPL. PHP ). that's all well and good, but we need to remember, then, that Drupal is a PAC, not MVC framework. if Drupal were an MVC framework, then theme _ * functions wowould have node_load () CILS scattered throughout them. that's one reason why, for instance, Adrian's Fapi 3 plans scare me. he talks in his presentation about "a modified MVC but with the model and view somewhat merged ". well, To start with Drupal isn' t MVC in the first place. secondly, the model and view are the last pieces you want to merge. merging a thin controller into a thick view makes some sense, but merging a rich model and rich view together makes a rich mess. that loses the separation of data and display that was the whole point in the first place. currently the closest Drupal comes to MVC is the panels mod Ule. panels allows the site admin to set up a custom Layout with custom regions and then pull blocks, views, or nodes into it in those regions. currently that's the only real pull-based logic that Drupal supports for display given the active discouragement of database access (even via accessors like node_load () in theme_functions and template files. even that, however, is still limited to specifi C Panel pages. there's no supported way to randomly pull a block into a node page, for instance. I know Earl "Merlin" Miles is deeply engrossed in panels 2.0, the next generation, but I don't believe it currently involves turning panels inside out. he is, of course, welcome to correct me if he does. there's also the viewfield module for turning a view into a field of a node, but as cool a concept As that is it's still PAC thinking, or at best a very weird hybrid muddle. conclusion There's more to say on this subject, but it's getting late so I will find a convenient stopping point for now. when thinking about your web applications, though, try not to fall back on the default "separation is good so we're using MVC which means separation" logic. only the first part of that statement, "separa Tion is good ", is true. there are lots of ways to build a modular system, all of them appropriate in different places. MVC is not the only, nor even the most widely-used of them. if you aren't calling your data store directly from your display layer, in fact, you're not using it at all. i'll be back next year with more thoughts on how to address the MVC. PAC question, and the impact they have o N team development processes. Until then, Happy New Year and happy coding!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.