MSDN Magazine - March 2008 - (Page 43) several best practices that I applied when worka separate object, the Presenter. The Presenter ing on my desktop apps. Two of them were: object then performs any logic necessary to • Separation of concerns: don’t mix UI logic respond to the activity on the view, typically with underlying behavior. by using other objects (the Model) to access • Automated unit testing: write automated databases, perform business logic, and so on. tests that verify whether your code does Once those steps are complete, the Presenter what you think it does. updates the view. This approach gives you testThe underlying principles here apply reability because the presenter is isolated from gardless of technology. Separation of concerns the ASP.NET pipeline; it communicates with is a fundamental principle that exists to help the view through an interface and can be tested you deal with complexity. Mixing Different in isolation from the page. responsibilities within the same object—like MVP works, but the implementation can calculating remaining work hours, formatting be a bit awkward; you need a separate view data, and drawing a graph—is just asking for interface, and you have to write lots of event maintenance problems. And automated testforwarding functions in your codebehind ing is crucial to getting production-quality files. But if you want a testable UI in your Web Figure 1 MVC Pattern Request Flow code while still maintaining your sanity, parForms applications, it’s about the best you’re ticularly when you’re updating an existing project. going to get. Any improvements would require a change in the ASP.NET Web Forms made it very easy to get started, but, in underlying platform. other ways, trying to apply my design principles to Web apps was a struggle. Web Forms are relentlessly UI focused; the fundamen- Model View Controller Pattern tal atom is the page. You start by designing your UI and dragging Luckily, the ASP.NET team has been listening to developers like controls. It’s very seductive to just start slapping your application me and has started development of a new Web application framelogic into the page’s event handlers (much like Visual Basic® en- work that sits side-by-side with the Web Forms you know and love abled for Windows® apps). but has a distinctly different set of design goals: On top of that, unit testing of pages is often difficult. You can’t • Embrace HTTP and HTML—don’t hide it. run a Page object through its lifecycle without spinning up all of • Testability is built-in from the ground up. ASP.NET. While it is possible to test Web apps by sending HTTP • Extensible at almost every point. requests to a server or automating a browser, that kind of testing • Total control over your output. is fragile (change one control ID and the test breaks), hard to set This new framework is based around the Model View Controlup (you have to set up the server on every developer’s machine ex- ler (MVC) pattern, thus the name, ASP.NET MVC. The MVC patactly the same way), and slow to run. tern was originally invented As I started building more sophisticated Web apps, the abstrac- back in the ’70s as part of The MVC pattern divides tions that Web Forms provides, like controls, view state, and the Smalltalk. As I’ll show in your UI into three distinct page lifecycle, started to chafe rather than help. I was spending this article, it actually fits more and more time configuring data binding (and writing tons into the nature of the Web objects: the controller, the of event handlers to get it configured correctly). I had to figure out quite well. MVC divides model, and the view. how to reduce the view state size to get my pages to load faster. Web your UI into three disForms require that a physical file exist at every URL, something tinct objects: the controlthat dynamic sites (like a wiki, for example) make difficult. And ler, which receives and handles input; the model, which contains successfully writing a custom WebControl is a remarkably com- your domain logic; and the view, which generates your output. In plex process that requires a comprehensive understanding of both the context of the Web, the input is an HTTP request, and the repage lifecycle and the Visual Studio® designer. quest flow looks like Figure 1. Since coming to work at Microsoft, I’ve had the opportunity to This is actually quite different from the process in Web Forms. share my learning about various .NET pain points and to hopeful- In the Web Forms model, the input goes into the page (the View), ly alleviate some of that pain. Most recently, such an opportunity and the view is responsible for both handling the input and genarose through my participation as a developer on the patterns & erating the output. When it comes to MVC, on the other hand, the practices Web Client Software Factory project (codeplex.com/websf). responsibilities are separated. In particular, one of the things that patterns & practices builds into So, right now there’s probably one of two things running through its deliverables is automated unit testing. In the Web Client Soft- your head. Either, “Hey, this is great. How do I use it?” or “Why ware Factory, we proposed the use of the Model View Presenter would I write three objects when I only had to write one before?” (MVP) pattern for building testable Web Forms. Both are excellent questions and best explained by looking at an In a nutshell, instead of putting your logic in the page, MVP has example. So I’m going to write a small Web app using the MVC you build your pages so that the page (View) simply makes calls into Framework to demonstrate its advantages. march2008 43 http://codeplex.com/websf
Table of Contents Feed for the Digital Edition of MSDN Magazine - March 2008 MSDN Magazine - March 2008 Contents Toolbox CLR Inside Out Data Points Advanced Basics Office Space Introducing ASP.NET MVC Loosen Up CI Server Performance Office Development Test Run Security Briefs Extreme ASP.NET Foundations .NET Matters {End Bracket} MSDN Magazine - March 2008 MSDN Magazine - March 2008 - (Page Intro) MSDN Magazine - March 2008 - Contents (Page Cover1) MSDN Magazine - March 2008 - Contents (Page Cover2) MSDN Magazine - March 2008 - Contents (Page 1) MSDN Magazine - March 2008 - Contents (Page 2) MSDN Magazine - March 2008 - Contents (Page 3) MSDN Magazine - March 2008 - Contents (Page 4) MSDN Magazine - March 2008 - Contents (Page 5) MSDN Magazine - March 2008 - Contents (Page 6) MSDN Magazine - March 2008 - Contents (Page 7) MSDN Magazine - March 2008 - Contents (Page 8) MSDN Magazine - March 2008 - Contents (Page 9) MSDN Magazine - March 2008 - Contents (Page 10) MSDN Magazine - March 2008 - Toolbox (Page 11) MSDN Magazine - March 2008 - Toolbox (Page 12) MSDN Magazine - March 2008 - Toolbox (Page 13) MSDN Magazine - March 2008 - Toolbox (Page 14) MSDN Magazine - March 2008 - CLR Inside Out (Page 15) MSDN Magazine - March 2008 - CLR Inside Out (Page 16) MSDN Magazine - March 2008 - CLR Inside Out (Page 17) MSDN Magazine - March 2008 - CLR Inside Out (Page 18) MSDN Magazine - March 2008 - CLR Inside Out (Page 19) MSDN Magazine - March 2008 - CLR Inside Out (Page 20) MSDN Magazine - March 2008 - Data Points (Page 21) MSDN Magazine - March 2008 - Data Points (Page 22) MSDN Magazine - March 2008 - Data Points (Page 23) MSDN Magazine - March 2008 - Data Points (Page 24) MSDN Magazine - March 2008 - Data Points (Page 25) MSDN Magazine - March 2008 - Data Points (Page 26) MSDN Magazine - March 2008 - Advanced Basics (Page 27) MSDN Magazine - March 2008 - Advanced Basics (Page 28) MSDN Magazine - March 2008 - Advanced Basics (Page 29) MSDN Magazine - March 2008 - Advanced Basics (Page 30) MSDN Magazine - March 2008 - Advanced Basics (Page 31) MSDN Magazine - March 2008 - Advanced Basics (Page 32) MSDN Magazine - March 2008 - Office Space (Page 33) MSDN Magazine - March 2008 - Office Space (Page 34) MSDN Magazine - March 2008 - Office Space (Page 35) MSDN Magazine - March 2008 - Office Space (Page 36) MSDN Magazine - March 2008 - Office Space (Page 37) MSDN Magazine - March 2008 - Office Space (Page 38) MSDN Magazine - March 2008 - Office Space (Page 39) MSDN Magazine - March 2008 - Office Space (Page 40) MSDN Magazine - March 2008 - Office Space (Page 41) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 42) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 43) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 44) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 45) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 46) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 47) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 48) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 49) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 50) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 51) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 52) MSDN Magazine - March 2008 - Introducing ASP.NET MVC (Page 53) MSDN Magazine - March 2008 - Loosen Up (Page 54) MSDN Magazine - March 2008 - Loosen Up (Page 55) MSDN Magazine - March 2008 - Loosen Up (Page 56) MSDN Magazine - March 2008 - Loosen Up (Page 57) MSDN Magazine - March 2008 - Loosen Up (Page 58) MSDN Magazine - March 2008 - Loosen Up (Page 59) MSDN Magazine - March 2008 - Loosen Up (Page 60) MSDN Magazine - March 2008 - Loosen Up (Page 61) MSDN Magazine - March 2008 - Loosen Up (Page 62) MSDN Magazine - March 2008 - Loosen Up (Page 63) MSDN Magazine - March 2008 - Loosen Up (Page 64) MSDN Magazine - March 2008 - Loosen Up (Page 65) MSDN Magazine - March 2008 - Loosen Up (Page 66) MSDN Magazine - March 2008 - Loosen Up (Page 67) MSDN Magazine - March 2008 - Loosen Up (Page 68) MSDN Magazine - March 2008 - Loosen Up (Page 69) MSDN Magazine - March 2008 - CI Server (Page 70) MSDN Magazine - March 2008 - CI Server (Page 71) MSDN Magazine - March 2008 - CI Server (Page 72) MSDN Magazine - March 2008 - CI Server (Page 73) MSDN Magazine - March 2008 - CI Server (Page 74) MSDN Magazine - March 2008 - CI Server (Page 75) MSDN Magazine - March 2008 - CI Server (Page 76) MSDN Magazine - March 2008 - CI Server (Page 77) MSDN Magazine - March 2008 - CI Server (Page 78) MSDN Magazine - March 2008 - CI Server (Page 79) MSDN Magazine - March 2008 - CI Server (Page 80) MSDN Magazine - March 2008 - Performance (Page 81) MSDN Magazine - March 2008 - Performance (Page 82) MSDN Magazine - March 2008 - Performance (Page 83) MSDN Magazine - March 2008 - Performance (Page 84) MSDN Magazine - March 2008 - Performance (Page 85) MSDN Magazine - March 2008 - Performance (Page 86) MSDN Magazine - March 2008 - Performance (Page 87) MSDN Magazine - March 2008 - Performance (Page 88) MSDN Magazine - March 2008 - Office Development (Page 89) MSDN Magazine - March 2008 - Office Development (Page 90) MSDN Magazine - March 2008 - Office Development (Page 91) MSDN Magazine - March 2008 - Office Development (Page 92) MSDN Magazine - March 2008 - Office Development (Page 93) MSDN Magazine - March 2008 - Office Development (Page 94) MSDN Magazine - March 2008 - Office Development (Page 95) MSDN Magazine - March 2008 - Office Development (Page 96) MSDN Magazine - March 2008 - Test Run (Page 97) MSDN Magazine - March 2008 - Test Run (Page 98) MSDN Magazine - March 2008 - Test Run (Page 99) MSDN Magazine - March 2008 - Test Run (Page 100) MSDN Magazine - March 2008 - Test Run (Page 101) MSDN Magazine - March 2008 - Test Run (Page 102) MSDN Magazine - March 2008 - Test Run (Page 103) MSDN Magazine - March 2008 - Test Run (Page 104) MSDN Magazine - March 2008 - Test Run (Page 105) MSDN Magazine - March 2008 - Test Run (Page 106) MSDN Magazine - March 2008 - Security Briefs (Page 107) MSDN Magazine - March 2008 - Security Briefs (Page 108) MSDN Magazine - March 2008 - Security Briefs (Page 109) MSDN Magazine - March 2008 - Security Briefs (Page 110) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 111) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 112) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 113) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 114) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 115) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 116) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 117) MSDN Magazine - March 2008 - Extreme ASP.NET (Page 118) MSDN Magazine - March 2008 - Foundations (Page 119) MSDN Magazine - March 2008 - Foundations (Page 120) MSDN Magazine - March 2008 - Foundations (Page 121) MSDN Magazine - March 2008 - Foundations (Page 122) MSDN Magazine - March 2008 - Foundations (Page 123) MSDN Magazine - March 2008 - Foundations (Page 124) MSDN Magazine - March 2008 - Foundations (Page 125) MSDN Magazine - March 2008 - Foundations (Page 126) MSDN Magazine - March 2008 - Foundations (Page 127) MSDN Magazine - March 2008 - Foundations (Page 128) MSDN Magazine - March 2008 - .NET Matters (Page 129) MSDN Magazine - March 2008 - .NET Matters (Page 130) MSDN Magazine - March 2008 - .NET Matters (Page 131) MSDN Magazine - March 2008 - {End Bracket} (Page 132) MSDN Magazine - March 2008 - {End Bracket} (Page Cover3) MSDN Magazine - March 2008 - {End Bracket} (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.