Understanding Model-View-Controller

What a freak!

But the first step to harnessing MVC is to understand why it works,

I’d love to see an other article about WHY it works.

I’m using MVC for my site but I often wonder whether so-and-so should be a simple include, a class, an object, etc.

Are all objects called “models” in MVC? What do I do with my “includes”? What’s a model without data? Does MVC allow includes or “libraries”? Can you have a model without a view? etc. I still find it confusing somehow.

Well spoken, Jeff. Asking yourself whether your application is skinnable is a good way to look at MVC.

“My favorite thing about CSS is that you can turn it off. All those news sites with tiny text and layouts that break when you size it up? Boom, disable CSS. All those sites locked to 700 pixels wide on my 1920 pixel width monitor? Again, no way I’m using that CSS.”

What is the obsession with wide-screen? It is much more efficient to read thin columns that are “tall” than reading very wide columns that are short.

There are two different examples of MVC presented in the above article:

  1. Web content
  2. Three-tier web development

So ,just from another point of web content and a AJAX view , without web brower, as HTML+CSS+JavaScript pattern is so popular, I think JavaScript should be the controller of web content.

Languages such as Python or Tcl, use Tkinter(Tk) Graphical User Interface, which hides MVC from the user. If for example you bind a variable x with a button, then whenever you modify x, button’s text will also change.
“No need to pass pointers around, to unknown levels up and down”

Jeff: “Isn’t CSS used to do exactly this on the underlying HTML?”

Yes, CSS is used to generate the view, but, alas, is not the view itself.

The problem I see with this example is that HTML/CSS/Browser can be considered as a “separation of concerns” pattern alright, but it does not actually map to Model-View-Controller.

The View in this case is something closer to what the browser temporarily stores as “rendered page cache”. That is the view: the final rendered page. The Model could be both HTML and CSS, and even Javascript in this case is part of what the browser has to render/execute. The Browser is not exactly a Controller either, it doesn’t recieve the input to “operate on the model”, it doesn’t change the HTML if I click on an image in the rendered view of that page.

But the underlying error in all this, in my most humble opinion, is trying to force everything into a certain pattern.

I never thought much of the the MVC pattern. It’s abstract - too abstract, in fact. If a bunch of smart people can’t agree on what it means then I question the usefulness of it at all.

Really brilliant ideas make you say, “Why didn’t I think of that?”. The most common reaction to MVC is, “Huh?”

If you think about it, ASP.NET by itself is already (and has been from the start) MVC complient, you have View (.aspx), Controller (aspx.cs) and Model (whatever backend).

The only problem being ASP.NET makes it easy to do View and even Model stuff in what should really be the Controller.

I haven’t really dug into the newish MVC framework yet, but it what i’ve seen does look cool, more tightly defined seperation of concepts etc. so it looks like it’ll make it easier to write leaner, cleaner code. Definately not a bad thing.

I disagree with these examples.

The model is always a fat Albanian goat named Morris, the view should be a softly baked piece of brie placed on a wooden shelf on an autumn afternoon and the controller should be the echos of a Chas and Dave anthem played at full volume (11) through crackily speakers.

Everyone else is just wrong because I know better.

I dunno, forcing the MVC pattern over HTML/CSS/browser seems a stretch to me. That is view/other bit of view/bit that displays the view, IMO. And I don’t think “different levels of abstraction” come into it either.

It seems a bit like when you have a hammer… :slight_smile:

That said, the more interest generated in patterns the better. I struggled for years at programming conferences to get people to look at things beyond the trivial “how to use component/framework X” type sessions. Take a look at MS Tech-Ed sessions and see how often such things are discussed, for example.

@Venkman:
Yes, CSS is used to generate the view, but, alas, is not the view itself.
The View in this case is something closer to what the browser temporarily stores as “rendered page cache”.

I don’t agree. It isn’t meant to be taken that literally.

The View is the “presentation filter” that determines how the model is presented to the user. That is what the CSS does.

If you argue that the View is literally the thing you see then why stop at the “rendered page cache”? By that logic you could argue that the view is the Windows drawing area or the frame buffer in the video card.

MVC works ok for traditional content-oriented web apps (with Plain Old Semantic HTML), but works better still generalised data-oriented web-apps:

Semantic Web apps could I suppose appear as a subtree under any of these - M for inference engines and the like; V for user-oriented apps; C for services/autonomous agents

see also: http://en.wikipedia.org/wiki/Linked_Data

I think for a bunch of people reading a programming blog many of you fail to see the abstraction that Jeff is trying to point out and instead are concentrating on the example he uses to try and convey that abstraction. MVC is not a concrete implementation but an idea that you should separate, as much as possible, the logic that makes up those ideas of model, view and controller. Depending on what level of abstraction you think of Jeff’s HTML+CSS+Browser example in it can either make perfect sense or none at all. Don’t forget, it is software, we make up the rules as we go along and there is no such thing as the right answer.

HTML/CSS/browser seems a stretch to me. That is view/other bit of view/bit that displays the view

HTML is definitely the Model.

HTML is purely intended to describe (model) the structure of a document. It does not determine HOW the document looks. It only describes the document’s contents and structure.

e.g.

h1Header/h1
pA paragraph./p

just describes a headline and a paragraph of text. It doesn’t say the header should be large or bold, or that the paragraph should have a space above or below it.

CSS is definitely the View.

It has knowledge of the HTML (model) it is presenting and provides details of how it should be presented. The same HTML model can be presented in completely different ways by two different CSS views.

Browser could be viewed as the Controller.

Not a great fit, since the browser typically responds to the user clicking links by requesting a new model from the server, rather passing messages onto the View. But it does fit with the “the link between a user and the system”

The point of patterns like this is to aid understanding. “A software metaphor is more like a searchlight than a road map” as Steve McConnell says.

I find it useful and illuminating to think of the interaction of HTML/CSS/Browser as an example of MVC. So the metaphor works for me.

I have a problem with some uses of MVC. Namely finding triplets such as: Order, OrderView, OrderController etc…

If I take your web framework analogy, it is like having every web page have its own HTML, CSS and browser(!). When, in fact, it is only the model that has to change while the view and controller is reused across models.

Very informative … got some details, helpful for someone that has an exam in 1 hour … concerning MVC amongst other thinks.

Wow this disucssion is a classic “WTF is MVC” discussion.
I work a few contracts and by jove have I seen some interesting implementations of MVC so i’m not suprised that people have such differing opinions.

I personally think the example Jeff gives is a good one and am glad that someone is finally giving good examples like this instead of binding it to a particular technology or particular way of doing things.
I think it is more logical to agree on the principle of Separation of Concerns and allow the MVC pattern to evolve naturally as opposed to religeously following one implementation of MVC. Same goes for MVP, it’s just MVC with a slight difference and Presenter is a bad word for a controller (which might also write a file to disk which has nothing to do with “presenting”) so instead of making different words for slightly different MVCs lets just talk SoC from now on!

A MVC masturbation article? What is this 2005?

Can we please be spared by the ensuing RoR is great article?

Cheers. Finally, I think I’ve grasped it.