MSDN - ASP.NET MVC
-
Upload
maarten-balliauw -
Category
Technology
-
view
15.081 -
download
4
Embed Size (px)
description
Transcript of MSDN - ASP.NET MVC

ASP.NET MVC(model-view-controller)
Maarten Balliauw – RealDolmenhttp://blog.maartenballiauw.be

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

Agenda
• Overview of ASP.NET• ASP.NET MVC• Building an application• Unit testing• Should I choose Webforms or MVC?• Q&A

ASP.NET…
• “one web application framework to rule them all”
• Always seen as one whole…
CachingCaching ModulesModules
HandlersHandlersIntrinsicsIntrinsics
PagesPages ControlsControls
GlobalizationGlobalization
ProfileProfile
Master PagesMaster Pages
MembershipMembershipRolesRoles
Etc.Etc.
ASP.NET

Look at it differently!
• Core infrastructure• Different presentation options
ASP.NETDynamic Data
ASP.NETWebForms
ASP.NETMVC
Presentation
RuntimeASP.NET
Core

ASP.NET MVC
• A new 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

Things you will lose…
• Viewstate• Postbacks• Tricky interview questions about nuances of the
page lifecycle

MVC? Model-View-Controller!
ControllerController(Input)(Input)
ModelModel(Logic)(Logic)
ViewView(Presentation)(Presentation)
Separation of concerns!

What does an MVC request look like?
Request
View
Controller
Response
ControllerHandles input(HTTP requests)
ViewVisually representsthe model
ViewData

DEMOFile > New…

Framework Goals
• Easy and frictionless testability• Full control over your <html/>• Full control over your URLs• Leverage existing ASP.NET features• Conventions and guidance• …

Clean URLs
Would you use:
/Products.aspx?
CategoryID={3F2504E0-4F89-11D3-
9A0C-0305E82C3301}
Or:
/Products/Books
My Favorite

Extensibility
• These come into play…
• … and are all replacable!
ControllerBuilder
ControllerFactory
Controller
ViewEngine
View
ControllerActionInvoker
ActionResult
ActionFilters
Model Binders

DEMOBuilding an ASP.NET MVC application

Testing
• Has anyone tried testing webforms?• Without IIS being fired up?• Each component tested individually?• Did you require vacation afterwards?

Infrastructure designed for testing
• These are all easily mockable!– HttpContextBase, HttpResponseBase, HttpRequestBase
• Extensibility – IController– IControllerFactory– IRouteHandler– IViewEngine, IView

Testing controller actions
• No 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");}
[TestMethod]public void ShowPostsDisplayPostView() { BlogController controller = new BlogController(…); var result = controller.ShowPost(2) as ViewResult;
Assert.IsNotNull(result); Assert.AreEqual(result.ViewData["Message"], "Hello");}

Testing controller actions using 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.");}
[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.");}

Testing frameworks
• Any framework is supported!– Visual Studio Test– NUnit– XUnit– MBUnit– …

DEMOUnit testing

Choosing between Webforms and 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

So what do I choose?
• Go with the flow!
• Remember:mixing of worlds is possible!– Mixing with ASP.NET Webforms– Mixing with dynamic data– …

Takeaways
• A new option for ASP.NET– Not a replacement for Webforms!
• More control over your <html/> and URLs
• Strong, frictionless testability

Resources• “ASP.NET MVC 1.0 Quickly”
http://tinyurl.com/mvcquickly
• My bloghttp://blog.maartenballiauw.be/category/MVC.aspx
• Microsofthttp://www.asp.net/mvchttp://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 projectshttp://mvccontrib.codeplex.comhttp://mvcsitemap.codeplex.com

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.