What is The MVC (Model-View-Controller) Pattern?

The MVC Pattern

Model-View-Controller (MVC) has been an important architectural pattern in computer science for many years. Originally named ThingModelViewEditor in 1979, it was later simplified to ModelViewController. It is a powerful and elegant means of separating concerns within an application (for example, separating data access logic from display logic) and applies itself extremely well to web applications. Its explicit separation of concerns does add a small amount of extra complexity to an application’s design, but the extraordinary benefits outweigh the extra effort. It has been used in dozens of frameworks since its introduction. You’ll find MVC in Java and C++, on Mac and on Windows, and inside literally dozens of frameworks.

The MVC separates the user interface (UI) of an application into three main aspects:

  • The Model: A set of classes that describes the data you’re working with as well as the business rules for how the data can be changed and manipulated
  • The View: Defines how the application’s UI will be displayed
  • The Controller: A set of classes that handles communication from the user, overall application flow, and application-specific logic

MVC as a User Interface Pattern

Notice that we’ve referred to MVC as a pattern for the UI. The MVC pattern presents a solution for handling user interaction, but says nothing about how you will handle other application concerns like data access, service interactions, etc. It’s helpful to keep this in mind as you approach MVC: It is a useful pattern, but likely one of many patterns you will use in developing an application.

MVC on the Web

For many, the Web didn’t really become prominent until the first graphical browsers began to flood the market, starting with Mosaic in 1993. Shortly after, dynamic web pages began showing up using languages such as Perl and enabled by technologies like the Common Gateway Interface (CGI). The technology available in the early stages of the Web was focused more around the concept of scripting HTML to do light, content-driven work, as opposed to deep, application logic, which just wasn’t needed back then.

As the Web grew and HTML standards began to allow for richer interactions, the notion of the Web as an application platform began to take off. As languages matured and web server software grew in capability, MVC soon found its way into web application architectures. But MVC didn’t hit its mainstream stride until July 2004, when a 24-year-old developer at 37Signals in Chicago, Illinois, named David Heinemeier Hansson, introduced the world to his fresh way of thinking about MVC.

David, or DHH as he’s known in the community, created Rails, a web development framework that used the Ruby language and the MVC pattern to create something special.

There are a lot of web development frameworks. Some of the most popular MVC web application frameworks are summarized here.

Ruby on Rails

Ruby on Rails (or Rails, as it’s colloquially known) is arguably one of the most popular MVC frameworks ever created for the Web. It works according to two major guiding principles:

  • Convention over Configuration: The idea is that as developers we’ve been doing web development for years now. Let’s agree on the things that work and make that part of the framework.
  • Don’t Repeat Yourself, or Keep It DRY: This applies to centralizing the logic of an application as well as the code. If you keep this idea in mind, you’ll find yourself writing a lot less code.

Rails is primarily a LAMP (Linux/Apache/MySQL/PHP) platform.

Django and Python

Django calls itself “the web framework for perfectionists with deadlines.” It is a web framework that uses the Python language and values clean design. Django focuses on automating as much as possible and also values the DRY principle.

Spring, Struts, and Java

In the Java space alone, there are three major MVC players: Apache Struts, the Spring Framework, and JSF, which is the J2EE standard MVC framework. There have been others, but these are the top three, and some would argue that JSF is a distant third at that.

Spring

This is a Java/J2EE framework, started in 2003, that values object-oriented design, interfaces over classes, and testability over all other considerations. It’s more than just an MVC framework — it’s a business objects framework, and even if its MVC aspects are not used, you’ll find Spring inside, at least the business tier, of almost every recent Java application. Spring includes a component container and transaction support integration with several object-relational mappers. It’s focused on making your Java applications as simple and flexible as possible using POJOs (Plain Old Java Objects) and eschewing aspects of Java/J2EE that it considers bloated. Spring includes an MVC Framework that provides an application context for web applications that can enable Spring to work with other Java web frameworks, such as Struts.

Struts

This was released in June 2001, and later the WebWorks and Struts communities merged and created Struts. It’s often considered the standard framework, and more and more is used in tandem with the Spring Framework.

JSF

This is a Java-based MVC development framework that provides a set of standard, reusable GUI components for web development. There are a number of vendor implementations of JSF.

Regardless of their differences, all these Java frameworks share the concepts of a Model, a View, and a Controller. They differ slightly in their life cycles and the things they each value, but the essential goal is the same — clean separation of concerns.

Zend Framework and PHP

PHP applications are usually not given much respect on the Web, even though much of the Web runs on PHP. Many developers think that PHP encourages spaghetti code, and it’s not hard to find entire PHP applications written in a single file called index.php.

The Zend Framework, or ZF, is an open-source web application framework implemented in PHP5 that tries bringing a little formality to PHP application development while still providing the kind of flexibility that PHP developers are used to. The Zend Framework doesn’t require developers to use these components, as it promotes a “use-at-will” philosophy.

ZF also implements a front-controller model, using convention over configuration whenever possible. It includes its own view templating engine and also supports plugging in alternative views.

ASP.NET MVC

In February 2007, Scott Guthrie (“ScottGu”) of Microsoft sketched out the core of ASP.NET MVC while flying on a plane to a conference on the East Coast of the United States. It was a simple application, containing a few hundred lines of code, but the promise and potential it offered for parts of the Microsoft web developer audience was huge.

ASP.NET MVC relies on many of the same core strategies that the other MVC platforms use, plus it offers the benefits of compiled and managed code and exploits new language features of VB9 and C#3 like lambdas and anonymous types. Each of the MVC frameworks discussed above shares in some fundamental tenets:

  • Convention over configuration
  • Don’t repeat yourself (aka the DRY principle).
  • Pluggability whenever possible
  • Try to be helpful, but if necessary, get out of the developer’s way.

ASP.NET MVC has gone through several versions with ASP.NET MVC 5 released in early 2014.

This article is excerpted and adapted from Wrox’s Professional ASP.NET MVC 4 (ISBN: 978-1-118-34846-8, copyright John Wiley & Sons 2013) and previous editions of the same book by Jon Galloway, Phil Haack, Brad Wilson, and K. Scott Allen. Their new book Professional ASP.NET MVC 5 also from Wrox publishes in the summer of 2014.

Jon Galloway works at Microsoft as a Community Program Manager. He wrote the MVC Music Store tutorial and helps manage ASP.NET site content. Phil Haack is a developer at GitHub, a developer website used by more than a million programmers. Brad Wilson is a developer on the ASP.NET team and has been a professional software developer for nearly 20 years. K. Scott Allen is the founder of OdeToCode LLC. He provides custom development, consulting, and mentoring services for clients around the world.


Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *