It's impossible to work within a software development team these
days without having enthusiastic discussions about something called
'MVC'. In broad terms, MVC (Model View Controller) is an
'architecture pattern', which Wikipedia defines as:
"Software patterns that offer well-established solutions to
architectural problems in software engineering."
In software engineering, as with any other construction
business, I can't stress the importance of applying good
architecture and design patterns enough. MVC is one such pattern
which has been around for a long time - its origins can be traced
back to 1979 and the Smalltalk programming language, yet has
recently seen a huge surge in popularity as it has proven to be a
natural fit for web-based solutions. I'll try not to bore you with
a lengthy discussion of MVC concepts, but a short overview is
necessary before I discuss the benefits that the MVC pattern can
bring to projects.
What is MVC?
In short, MVC projects consist of at least 3 distinct areas of
responsibility:
- Models - Concerned with data modelling and
capturing the project's business requirements.
- Views - Presentation / user-interface
components. A view generally provides a visual representation of
data in the model(s). On a web site, these are usually web pages or
HTML snippets.
- Controllers - The engine room. Controllers
facilitate the flow of data through the application. A controller
will generally respond to some form of user-input, and then pass
data to or from the model before handing another view back to the
user.
So what does this actually mean? At its simplest level, MVC
encourages developers to break their projects down into manageable,
independent components with clearly defined lines of
responsibility.
Back in the old days of writing Classic ASP or PHP spaghetti
code, now often referred to as an anti-pattern
(opens new window), it wasn't an uncommon sight for
web pages to comprise of a mix of HTML mark-up, database access
code and business logic all in one place, which while quick to
write would become increasingly difficult to test and maintain
during the course of the project's lifespan.
Instead, using the MVC pattern we are encouraged to break apart
the tightly coupled dependencies that can exist in typical
implementations (e.g. the user interface should not need to know
how to access the database), which leads to code that is more
modular, easier to maintain, and much more robust.
What are the benefits of MVC?
There are many benefits to using the MVC approach, some of the
more tangible ones are:
- MVC's architecture makes it much easier for different team
members to work independently on separate aspects of the system.
For example, designers can work on the views without needing to
understand how the underlying database technology works. Likewise,
the programmer with no interest in user-interface design can focus
solely on modelling the database and business requirements of the
project.
- MVC projects are very easy to test. Developers typically create
'unit tests' which can be run automatically and allows them to
quickly verify that each part of their application behaves as
expected. Being able to break an application down into the loosely
coupled components that MVC encourages is key to being able to
create good unit tests.
- MVC projects tend to be modular by nature, which makes it much
easier to alter (or even rewrite) one part of the application
without it adversely impacting on other areas. MVC makes it very
easy to achieve Separation
of Concerns (opens new window) - a key
principal to writing code that is easy to maintain and
refactor.
Where do we use MVC?
We work with plenty of legacy applications written using non-MVC
frameworks, such as standard PHP, ASP, and ASP.NET Webforms sites.
These all fulfil their purpose and certainly haven't become bad
applications overnight! Likewise, we won't be abandoning popular
products such as Wordpress any time soon just because they aren't
MVC based. However we have been shifting greenfield project
development to MVC frameworks. Some of the MVC based solutions that
we currently use are ASP.NET MVC,
CodeIgniter - a PHP-based MVC framework,
Joomla – an MVC-based CMS and Umbraco
CMS - moving to ASP.NET MVC for its next release.
Of course, there is no such thing as a silver bullet in the
software development world and MVC is no exception. It's possible
for a good software developer to craft elegant solutions no matter
what architectural pattern or framework is used, just as no amount
of 'best practices' is going to save a poor developer from creating
buggy, unmanageable code. But by encouraging you to adopt proven
design patterns there’s no doubt that MVC empowers and encourages
developers to create code that is elegant, reusable and robust -
and that can only mean happier clients.