There was a time when everything was moving towards the desktop. This Internet thing was new and cool, but there was no way it would ever last. And no one knew how to code for the web, at least not anything beyond animated lava lamps and cute "Under Construction" images. So, to make coding for the web easier, they made ASP.NET to be just like coding for a desktop, using the same patterns, the same event-based model, and the same stateful approach. But the web isn't stateful, its only events are GET and POST, and is nothing like a desktop, so we tortured ourselves for years forcing a square peg through a round hole. The time has come for redemption, and its name is ASP.NET MVC. Spend an hour discovering how coding for the web is supposed to be--how it is today--and end your misery. Salvation awaits.
There was a time when everything was moving towards the desktop. This “Internet” thing was new and cool, but there was no way it would ever last. And no one knew how to code for the web, so to make web development easy and familiar, they made ASP.NET to be just like coding for a desktop. It used the same patterns, the same event-based model, and the same stateful approach. But the web isn't stateful, its only events are GET and POST, and it is nothing like a desktop, so we tortured ourselves for years forcing a square peg through a round hole. The time has come for redemption, and its name is ASP.NET MVC. This session will cover how to write and test an application in MVC, how it differs from ASP.NET, and will discuss some of the new features of ASP.NET MVC 2. Spend an hour discovering how coding for the web is supposed to be--how it is today--and end your misery. Salvation awaits.
In 1994, Netscape released Netscape Navigator 1.0, and though it is not the first graphical browser, it takes off with a speed and enthusiasm greater than any browser before it. The popularity of the web explodes...
Jay Harris is a .NET developer in Southeast Michigan, and is independent software consultant for his company, Arana Software (http://www.aranasoft.com). He has been developing on the web for 15 years, since he abandoned VB3 for JavaScript because he didn't have to wait for a compile. With a career focus on end-user experience, he is a strong advocate of practices and processes that improve quality through code, ranging from automated testing, continuous integration, and performance analysis, to designing applications from the perspective of the user instead of the database. Jay is also active in the developer community beyond speaking, including serving as President of Ann Arbor .Net Developers (http://www.aadnd.org) and as an organizer for Lansing Give Camp and Come Jam With Us. When not coding, he is usually blogging to http://www.cptloadtest.com or playing games on his Xbox 360.
First, the MVC pattern. It is made up of three components: The Model, the View, and the Controller.
The Model is the Business Entity. It is responsible for its own internal custom logic, such as validating itself, and is responsible for how to save and retrieve itself. Model is the “How” in an application.
The Controller interprets user interactions. It takes user input, such as parameters in a URL or data from a submitted form, and figures out what needs to be done. In ASP.NET MVC, given a URL of “~/Employee/Edit/42”, the Controller understands that the user wants to load and Edit the Employee with an id of 42. Controller will then tell the Employee Model to load id 42, then pass the returned Model to the appropriate View. Controller is the “What” in an application.
The View handles display and presentation. It is responsible for taking whatever Model was passed to it by the Controller, and displaying the data appropriately. Given identical lists of Employees, a Last Name Search Results view may display the Employees in a grid with the Last Name as the first column, but the Hire Date Search Results view may place the Hire Date column first. View is the “Where” in an application.
The lines on this diagram indicate the direction of data flow in the pattern. Data is passed from the Model to the View via the Controller. The controller requests an instance of the model and passes that instance on to the View. Once the View has the instance, it can access data and properties on the model for Display.
A Nintendo Wii makes a great example of MVC in the real world.
The Game Disk is a Model; it knows all of its own business logic, and how to save and retrieve game data from the save file. But, it knows nothing of the Wii or your TV. The Wii (both the head unit and the Wiimote) is the Controller; it knows how to interpret user actions, tell the Model what to do, then pass the signal on to your TV. Finally, the TV is your view, taking the signal passed to it from the Wii and displaying it, but it knows nothing of the Wii itself, only of the signal being passed.
All components are modular & replaceable. Crayons, colored pencils, permanent markers, and paint are all Models to create artwork in a coloring book. Coloring Books are the view to display that artwork, but to a small child, markers work just as well in a coloring book as they do all over the walls of the house, and to a parent covering up that marker on the wall, paint can be applied to that wall just as it can to a fence, the house, or even an easel. The artist is your controller, accepting a model and passing it to the view, but that artist can be you, me, a 5-year-old child, Leonardo DaVinci, or Bob Ross; each is capable of putting crayon to paper.
In MVC, each component has its own responsibility and each component is modular and independent. This allows each component to be very testable...testable without the influence of the other two components.
It’s 1994. No one knows how to code for web, but we thought the web was cool. So, we cheered. Developers everywhere grab a free Geocities or Tripod account, and create web pages that look like ransom notes, plastered with animated Lava Lamp graphics. But we soon dreamed of sites where we could check the stock prices or our bank accounts in real time, but static HTML could’t do this. We needed something more.
1997. Microsoft releases dynamic web coding with Active Server Pages, or ASP. (Now called Classic ASP.) We could make dynamic sites to check our bank balances, and everyone cheered. But it mashed all of our code together into one big ugly untestable page. We still couldn’t code for the web, but we knew we needed something different.
2002. The launch of ASP.NET. This separated our Model from our Controller (the ASPX Code Behind) and our View (the ASPX Code-in-Front, the HTML), increasing testability. Since we didn’t know how to code for the web, they made it just like coding for desktop, with many different events and a very stateful platform. We knew how to code for the desktop, so we cheered. But the design didn’t fit, since the web only has two events, GET and POST, and is definitely not stateful. And we still couldn’t separate our View from our Controller for testing. So, we still didn’t know how to code for the web, but we knew we needed something different.
2009. Microsoft releases the ASP.NET MVC framework. Finally, the web was the web. The design matched the stateless web environment. The design matched the web’s event model. And we had full separation of concerns. We cheered. Now we can learn how to code for the web.
...and 2010 brings ASP.NET MVC, version 2, with strong typing, model validation, and attributes for simplifying our code.
Demo. The code walkthrough is available on Jay Harri’s blog: http://tinyurl.com/AspNetMvcWalkthrough
Open question period.
Primary Resource: ASP.NET MVC 2 in Action is THE book for learning the new framework. Don’t bother with any other books. This is the only one you need.
A few other resources are available online.
- Microsoft’s site will give you more information, and some more depth into using ASP.NET MVC.
- The source code can be fully downloaded from CodePlex.
- And finally, MVC Contrib is an awesome collection of MVC utilities to make coding and testing your projects even easier.
Feel free to contact Jay Harris using email, twitter, or through my blog. He will try and answer your ASP.NET MVC questions.