MVC Software Architecture

When we go to build complex database driven websites using a collection of scripts where we have an html page with some code in the page that generates the dynamic parts has gone the way of the dodo. Instead, the trend is to use a software architecture, like MVC, to build an application that generates the pages and functionality in your site. Let's take a look at MVC, the simple and very popular software architecture.

MVC is an acronym for Model, View, Controller which are the 3 components to the MVC architecture.

m-v-c-diagram.png

This diagram shows the separation of the components and the interactions of the components.

Stepping through the flow we start with a user contacting a controller, like a website event through ajax or visiting a url. The input from the user goes to the controller. The controller updates the model. This is, often, the model getting some information from a database like a series of articles or a blog post. At the same time, the controller tells the view what to present. In the case of a website it might tell it to use a certain html output. The model tells the view what information to use and the view presents that information to the user.

Does this seem a little complex?

Let's look at what this allows us to do. First, what if our model changes? What if we switch databases? What if we need to change the logic in some math calculations? If we use MVC then we only change the model and the rest is untouched. A lot less needs to happen to make a change.

Let's look at web services for our second area. RSS, RDF, and a dozen other buzz words come to mind. If we wanted to add a web service on like RSS all we would have to do is add another view for that information and tell the controller when to present in that view. For example, a user clicks on an RSS icon the controller tells the view to be an RSS presentation of the model and that's the only difference from the other flow. No need to create or modify a model. This makes expanding into web services easier and less work.

Software architectures like MVC can be a bit to grasp at first and are a change in the way web development was done just a few years ago. MVC is nothing new. It has been used in application development as long as there has been graphical interfaces. It's just newer to web applications.

If you have questions or know of any good tutorials please feel free to post them here. MVC is a design pattern every web developer should have in their arsenal of tools.

Further Reading: