views:

1308

answers:

10

I've heard the term MVC (Model View Controller) tossed about with a ton of Buzz lately, but what really is it?

A: 

Wikipedia seems to describe it best so far:

http://en.wikipedia.org/wiki/Model-view-controller

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application and the business rules used to manipulate the data; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements

Chris Pietschmann
A: 

This What is MVC blog article on Oreilly has you covered.

ctcherry
what is the difference between application logic and business logic? because in this article it says model contains business logic and controller contains application logic. I'm confused with that.
Jaywith.7
I think the article is trying to draw a distinction between code that is directly related to the problem(s) you are solving (thats the code in the models - for example calculating an invoice total) and the code that is responsible for directing requests and passing information around (thats the code in the controllers - handling get/post and response formats, etc). I don't think calling them business and application logic was the best choice of words.
ctcherry
+4  A: 

You might want to take a look at what Martin Fowler has to say about MVC, MVP and UI architectures in general at Martin Fowlers site.

Ruben
+2  A: 

MVC is a design pattern originally pioneered in the olden days of smalltalk.

The concept was that a model would represent your application state and logic, and controllers would handle IO between "Views".

A View was a representation of the state in the model. For example, your model may be a spreadsheet document, and you may have a view that represents it as a spreadsheet and a view that represents it as a pivot table.

Modern MVC has been polluted with fake MVC web junk, so I'll let others answer that.

FlySwat
You make the statement "Modern MVC has been polluted with fake MVC web junk" and then avoid addressing it, deferring to "others" that will answer that. Why do you feel this way and why don't *you* elucidate for us?
Jason Bunting
I deferred to others because I knew that in 10 minutes there would 8 answers equating MVC to Rails :)
FlySwat
Ah, I see...you are saying Rails is *not* really MVC? I don't know much about Rails, so I can't address that myself...
Jason Bunting
Rails, and most other web MVC frameworks move business logic to the controller (and sometimes the view!) and usually use the model as a dumb datastore.In a purist viewpoint, this is not MVC...however it works, Stackoverflow is using ASP.NET MVC...same idea there.
FlySwat
A: 

MVC is a way to partition a user interface element into 3 distinct concepts. The model is the data on which the interface operates. The view is how the element is represented visually (or maybe audibly?). The controller is the logic that operates on the data.

For example, if you have some text you want to manipulate in a UI. A simple string could represent the data. The view could be a text field. The controller is the logic that translates input from the user - say character or mouse input - and makes changes to the underlying data model.

AdamC
This is fake Web MVC...In real MVC, the controller is simply the IO bridge, and the model does all data manipulation.
FlySwat
+5  A: 

I like this article by Martin Fowler. You'll see that MVC is actually more or less dead, strictly speaking, in its original domain of rich UI programming. The distinction between View and Controller doesn't apply to most modern UI toolkits.

The term seems to have found new life in web programming circles recently. I'm not sure whether that's truly MVC though, or just re-using the name for some closely related but subtly different ideas.

Luke Halliwell
+1  A: 

As the tag on your question states its a design pattern. But that probably doesn't help you. Basically what it is, is a way to organize your code into logical groupings that keep the various pieces separate and easily modifiable.

Simplification: Model = Data structure / Business Logic View = Output layer (i.e HTML code) Controller = Message transfer layer

So when people talk about MVC what they are talking about is dividing up there code into these logical groups to keep it clean and structured, and hopefully loosely coupled. By following this design pattern you should be able to build applications that could have there View completely changed into something else without ever having to touch your controller or model (i.e. switching from HTML to RSS).

There are tons and tons of tutorials out there just google for it and I'm sure you'll turn up at least one that will explain it in terms that click with you.

Rob Booth
A: 

Like many have said already, MVC is a design pattern. I'm teaching one of my coworkers now and have explained it this way:

Models - The data access layer. This can be direct data access, web services, etc

Views - The presentation layer of your application.

Controllers - This is the business logic for your application.

This pattern enhances test-driven development.

Jason N. Gaylord
A: 

It is a way of separating the underlying functionality of your application (model) from the way it interacts with the user (view). The controller coordinates how the model and view talk to each other.

Whilst it is all the rage at the moment, it is important to remember that preventing the model itself being able to determine exactly how its data is presented to the user can seen as a negative thing. The most obvious example is with HTML. The original intention of HTML was that there should be a clear separation of the model (HTML) from the view (rendered webpage) via a controller (the browser). There has been such a backlash against this original intention that browsers are criticised if they do not render a page pixel perfect to the designer's desired view.

David Arno
+1  A: 

The MVC or Model-View-Controller User Interface Paradigm was first described by Trygve Reenskaug of the Xerox PARC. In first appeared in print in Byte magazine volume 6, number 8, in August of 1981.

dacracot
Xerox PARC also invented smalltalk, and MVC was a fundamental concept of it.
FlySwat
Here is Reenskaug's article... http://folk.uio.no/trygver/2003/javazone-jaoo/MVC_pattern.pdf
dacracot