MVC (model view controller) is a design/architectural pattern used in software engineering. A well implemented mvc will isolate data storage, user interface, and business logic. In MVC the model is the data, the view is what is visible to an end user of the application and the controller is where the business logic resides. MVC was first described in a paper by Trygve Reenskaug and has been around since 1979.
The model
The model is the domain-specific representation of data presented to the application. MVC doesn’t specify underlying storage or the mechanics of access. However when trying to wrap your head around MVC the first time that can sounds like the model is a view of the data - which it is - just not the view in MVC. For a web developer the model means a relational database like PostgreSQL, MySQL or directories and files wrapped in a data abstraction layer written in a programming language like python, ruby, or php.
The view
In a web application the view is what is seen by the person using the application. This includes technologies such as: html, css,javascript and flash. The view has read access to the model and their can be multiple views of a single model. For example a multiple views could be written for a single model and mediated by a single controller using any of the following:
The controller
A web application controller processes and responds to http requests from the web browser initiated in a view and mediates this interaction. All application business logic ideally resides in the controller although it may sometimes reside in the model. Controller logic is usually written in a server side programming language like python
So why use MVC for the web?
As web application complexity grows so does the need to divide and conquer the development process. An MVC architecture makes this much easier to do. The model team can quickly create a unit tested stub interface and then continue on to write the sql queries and deal with the other underlying storage issues while the controller and view teams work in parallel using the stub they provided. As they code/unit test the model it remains consistent with the stub used by the other teams so that at integration costs are minimized.
The controller team can provide stub interfaces for the view team in the same way as the model team and ensure they remain consistent with the stub they provide to the view team through rigorous unit testing.
The view team is working on the user interface but they need data so they can mockup how best to present it and operate on it. If they build an interface based on a misunderstanding of what data they have access to and how they can access it then needless rework will result.
In an MVC web application the model is the only way the underlying data storage will be accessed. This approach makes it very easy to :
- unit test your model
- migrate and/or scale underlying data storage
- reuse the model with a different interface like a data import system which immediately
which can immmediately leverage all the validation, filtering and transforms you’ve already
written in your model.
- quickly deploy a developer api when your app becomes the next facebook or digg.
I’ve heard arguments that for the small team or lone developers the MVC architecture adds needless complexity. I work alone sometimes and for me the MVC architecture can be of even greater value because it provides a powerful conceptual tool for dividing and conquering a larger problem. For example when working on the model component I focus in on the data, sql , and python code. When writing the controller its all pretty much python. Then when building out the view
component its client side stuff: html,css,javascript, ajax. I don’t mind hopping around between all of these but when conceptualizing a complex or critical portion of an application its nice to be able to divide and conquer the problem by thinking in just one or two languages while doing so.
I hope this post helps those struggling with the concept of mvc or wondering about its applicability to web application development.