The document is a presentation on ASP.NET MVC. It provides an overview of ASP.NET MVC, including that it is a new presentation option for ASP.NET that allows for simpler programming, easier testing, and more control over HTML and URLs. It then demonstrates building a simple ASP.NET MVC application and unit testing controllers. It concludes by discussing factors to consider when choosing between ASP.NET WebForms and MVC.
Who am I?Maarten Balliauw Antwerp, Belgium www.realdolmen.com Focus on web ASP.NET, ASP.NET MVC, PHP, Azure, VSTS, … http://blog.maartenballiauw.be http://twitter.com/maartenballiauw
3.
Agenda Overview ofASP.NET ASP.NET MVC Building an application Unit testing Should I choose Webforms or MVC? Q&A
4.
ASP.NET… “ oneweb application framework to rule them all” Always seen as one whole… ASP.NET Caching Modules Handlers Intrinsics Pages Controls Globalization Profile Master Pages Membership Roles Etc.
5.
Look at itdifferently! Core infrastructure Different presentation options Presentation Runtime ASP.NET Dynamic Data ASP.NET WebForms ASP.NET MVC ASP.NET Core
6.
ASP.NET MVC Anew presentation option for ASP.NET Simpler way to program ASP.NET Easily testable and TDD friendly More control over your <html/> More control over your URLs Not for everyone! (car vs. motorcycle) Not a replacement for webforms! Supports existing ASP.NET features
7.
Things you willlose… Viewstate Postbacks Tricky interview questions about nuances of the page lifecycle
Framework Goals Easyand frictionless testability Full control over your <html/> Full control over your URLs Leverage existing ASP.NET features Conventions and guidance …
12.
Clean URLs Wouldyou use: /Products.aspx?CategoryID={3F2504E0-4F89-11D3-9A0C-0305E82C3301} Or: /Products/Books My Favorite
13.
Extensibility These comeinto play… … and are all replacable! Controller Builder Controller Factory Controller View Engine View Controller Action Invoker Action Result Action Filters Model Binders
Testing Has anyonetried testing webforms? Without IIS being fired up? Each component tested individually? Did you require vacation afterwards?
16.
Infrastructure designed fortesting These are all easily mockable! HttpContextBase, HttpResponseBase, HttpRequestBase Extensibility IController IControllerFactory IRouteHandler IViewEngine, IView
17.
Testing controller actionsNo requirement to test within ASP.NET runtime! Can mock parts of runtime you want to fake Using Moq, Rhino, TypeMock, … http://code.google.com/p/moq/ [TestMethod] public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewData[ " Message " ], " Hello " ); }
18.
Testing controller actionsusing Moq More on Mocking? Visit VISUG on May 7, 2009 – www.visug.be) [TestMethod] public void TestInvalidCredentials() { LoginController controller = new LoginController(); var mock = new Mock<System.Web.Security.MembershipProvider>(); mock.Expect(m => m.ValidateUser("", "")).Returns(false); controller.MembershipProviderInstance = mock.Object; var result = controller.Authenticate("", "") as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, "Index"); Assert.AreEqual(controller.ViewData["ErrorMessage"], "Invalid credentials! Please verify your username and password."); }
Choosing between Webformsand MVC ASP.NET Webforms Winforms-alike event model Familiar controls Familiar = rapid application development Functionality per page Uses viewstate for state management Less control over rendered HTML ASP.NET MVC Less complex: separation of concerns Easier parallel development TDD support No viewstate, … Full control over behavior and HTML Makes you think
22.
So what doI choose? Go with the flow! Remember: mixing of worlds is possible! Mixing with ASP.NET Webforms Mixing with dynamic data …
23.
Takeaways A newoption for ASP.NET Not a replacement for Webforms! More control over your <html/> and URLs Strong, frictionless testability
24.
Resources “ ASP.NETMVC 1.0 Quickly” http://tinyurl.com/mvcquickly My blog http://blog.maartenballiauw.be/category/MVC.aspx Microsoft http://www.asp.net/mvc http://wiki.asp.net/page.aspx/286/aspnet-mvc-framework/ http://blog.wekeroad.com/blog/i-spose-ill-just-say-it-you-should-learn-mvc/ Community projects http://mvccontrib.codeplex.com http://mvcsitemap.codeplex.com