An overview
MVC – Model View Controller, is a sofware design pattern. The aim of MVC is to provide clean separation between data (the model) and what visitors/users see (the view). The controller sits between the model and the view marshalling data between them.
MVC is a pattern that can be applied to numerous software development situations, from developing desktop/mobile applications through to websites. Here, I’ll be discussing MVC in relation to website development.
User <–> View <–> Controller <–>Model <–> Data storage (database, file, etc.)
The view presents data to the user and accepts user input. The controller retrieves data from the model, formats it for use by the view and passes it to the view. The controller also receives user generated input, formats it for use by the model and passes it to the model. The model, in my opinon, should validate incoming user generated input and handle the storage and retrieval of that data. What the model uses for storage shouldn’t matter to either the view or the controller.
Advantages
There are plenty of advantages to using the MVC pattern. Here are a few:
- Reskinning a site to provide a new look and feel should just be a case of changing the view.
- Changing storage mechanisms should just be a case of updating/changing how the model works
- With (pre) defined interactions for view <–> Controller and controller <–> model, building large and complex sites with multiple developers should be straight forward. Developers working on different parts of the system shouldn’t disturb the other sections.
- Site maintenance should be easier
That all sounds great, and actually it is. This is the reason why MVC is so widely used.
Disadvantages
Of course there are some. These are disadvantages from my point of view.
- MVC isn’t suitable for everything
- Can be a bit tricky to get your head around if you are using MVC for the first time. Especially if you have decided to use a framework based on MVC.
- Initially it can feel a bit restrictive, which can put you off.
Should you be using it?
Ok, getting to the point. well, if you are keen on using one of the many frameworks out there, then yes, you should. In this situation you really need to accept how it the framework has implemented it and go with it, otherwise you will end up really struggling with your choice of framework. That can lead to delays in development, doubts about your choice of platform and general despondency. That’s not good.
When creating a site from scratch, then it is a case of weighing up the advantages and disadvantages. For a simple site with a few pages and maybe a contact form, I would suggest not – development time would probably increase, as would complexity.
However, creating something that others will have to work on at some point and that need to be extended, then I would say yes.
If you usually pepper HTML with database queries, or prefer to make decisions at the point of rendering the final output, MVC is likely to give you a bit of a headache, but for larger projects, persevere. It will pay off in the end.
Being strict
Now, you’ve made the decision. MVC is the future. Good stuff. Now, do you strictly adhere to the version of MVC that you’ve found via your friend Google? This is where opinion can vary. Personally, I say go with what works. I’ve been using MVC variations for a while and I’ve settled on the opinion that keeping the view simple is best.
Avoid having to make decisions in the view. Use the controller for that. This may lead to an increased number of templates in your system, but that really isn’t a bad thing. It can keep your code less cluttered and easier to maintain and extend. Alternatively you could end up with some HTML in your controller. Purists would probably turn blue and scream – “HTML should be kept in the view”. Try and keep it to a minimum, but it definitely isn’t the end of the world.
Try and keep your data manipulation in the model. Got several fields that comprise an address? Rather than retrieve all the fields and create a unified address in the controller, just add a method to your model to do that. The controller shouldn’t really need to know that the address is made of of 2,3 or 4 fields.
A good understanding of Object orientated programming will definitely help you get the best out of MVC, so if you are hitting both of these for the first time it’s probably best to get OOP under your belt first. I usually find 2 or 3 levels of inheritance being used in models.
Finally
Sorry if you came across this hoping to have a 3 step intro to using MVC, it wasn’t really supposed to do that. I’m just trying to help you make a decision.
Good luck.
Hey, I can’t view your site properly within Opera, I actually hope you look into fixing this.
Comment by aparadekto — October 27, 2010 @ 3:05 am
Thanks, if you could be a little more specific, I’d be more than happy to sort it out. Looks fine in latest Opera to me.
Comment by Jason — November 13, 2010 @ 11:05 am