Chapter 1
Getting Started
By Jon Galloway
What's In This Chapter?
- Understanding ASP.NET MVC
- An ASP.NET MVC 3 overview
- How to create MVC 3 applications
- How MVC applications are structured
This chapter gives you a quick introduction to ASP.NET MVC, explains how ASP.NET MVC 3 fits into the ASP.NET MVC release history, summarizes what's new in ASP.NET MVC 3, and shows you how to set up your development environment to build ASP.NET MVC 3 applications.
This is a Professional Series book about a version 3 web framework, so we're going to keep the introductions short. We're not going to spend any time convincing you that you should learn ASP.NET MVC. We're assuming that you've bought this book for that reason, and that the best proof of software frameworks and patterns is in showing how they're used in real-world scenarios.
A Quick Introduction to ASP.NET MVC
ASP.NET MVC is a framework for building web applications that applies the general Model View Controller pattern to the ASP.NET framework. Let's break that down by first looking at how ASP.NET MVC and the ASP.NET framework are related.
How ASP.NET MVC Fits in with ASP.NET
When ASP.NET 1.0 was first released in 2002, it was easy to think of ASP.NET and Web Forms as one and the same thing. ASP.NET has always supported two layers of abstraction, though:
- System.Web.UI : The Web Forms layer, comprising server controls, ViewState, and so on
- System.Web : The plumbing, which supplies the basic web stack, including modules, handlers, the HTTP stack, and so on
The mainstream method of developing with ASP.NET included the whole Web Forms stacktaking advantage of drag-and-drop controls, semi-magical statefulness, and wonderful server controls while dealing with the complications behind the scenes (an often confusing page life cycle, less than optimal HTML, and so on).
However, there was always the possibility of getting below all thatresponding directly to HTTP requests, building out web frameworks just the way you wanted them to work, crafting beautiful HTMLusing Handlers, Modules, and other handwritten code. You could do it, but it was painful; there just wasn't a built-in pattern that supported any of those things. It wasn't for lack of patterns in the broader computer science world, though. By the time ASP.NET MVC was announced in 2007, the MVC pattern was becoming one of the most popular ways of building web frameworks.
The MVC Pattern
Model-View-Controller (MVC) has been an important architectural pattern in computer science for many years. Originally named Thing - Model - View - Editor in 1979, it was later simplified to Model - View - Controller . 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 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 user interface (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're referred to MVC as a pattern for the User Interface. 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 as Applied to Web Frameworks
The MVC pattern is used frequently in web programming. With ASP.NET MVC, it's translated roughly as:
- Models: These are the classes that represent the domain you are interested in. These domain objects often encapsulate data stored in a database as well as code used to manipulate the data and enforce domain-specific business logic. With ASP.NET MVC, this is most likely a Data Access Layer of some kind using a tool like Entity Framework or NHibernate combined with custom code containing domain-specific logic.
- View: This is a template to dynamically generate HTML . We cover more on that in Chapter 3 when we dig into views.
- Controller: This is a special class that manages the relationship between the View and Model. It responds to user input, talks to the Model, and it decides which view to render (if any). In ASP.NET MVC, this class is conventionally denoted by the suffix Controller .
It's important to keep in mind that MVC is a high-level architectural pattern, and its application varies depending on use. ASP.NET MVC is contextualized both to the problem domain (a stateless web environment) and the host system (ASP.NET).
Occasionally I talk to developers who have used the MVC pattern in very different environments, and they get confused, frustrated, or both (confustrated?) because they assume that ASP.NET MVC works the exact same way it worked in their mainframe account processing system fifteen years ago. It doesn't, and that's a good thingASP.NET MVC is focused on providing a great web development framework using the MVC pattern and running on the .NET platform, and that contextualization is part of what makes it great.
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 newer .NET language features such as lambdas and dynamic and anonymous types. At its heart, though, ASP.NET applies the fundamental tenets found in most MVC-based web frameworks:
- Convention over configuration
- Don't repeat yourself (aka the DRY principle)
- Pluggability wherever possible
- Try to be helpful, but if necessary, get out of the developer's way
The Road to MVC 3
Two short years have seen three major releases of ASP.NET MVC and several more interim releases. In order to understand ASP.NET MVC 3, it's important to understand how we got here. This section describes the contents and background of each of the three major ASP.NET MVC releases.
ASP.NET MVC 1 Overview
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.
As the legend goes, at the Austin ALT.NET conference in October 2007 in Redmond, Washington, ScottGu showed a group of developers this cool thing I wrote on a plane and asked if they saw the need and what they thought of it. It was a hit. In fact, many people were involved with the original prototype, codenamed Scalene . Eilon Lipton e-mailed the first prototype to the team in September 2007, and he and ScottGu bounced prototypes, code, and ideas back and forth.
Even before the official release, it was clear that ASP.NET MVC wasn't your standard Microsoft product. The development cycle was highly interactive: there were nine preview releases before the official release, unit tests were made available, and the code shipped under an open source license. All of these highlighted a philosophy that placed a high value in community interaction throughout the development process. The end result was that the official MVC 1.0 releaseincluding code and unit testshad already been used and reviewed by the developers who would be using it. ASP.NET MVC 1.0 was released on 13 March 2009.