KEMBAR78
MSDN - ASP.NET MVC | PPT
ASP.NET MVC (model-view-controller) Maarten Balliauw – RealDolmen http://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… ASP.NET Caching Modules Handlers Intrinsics Pages Controls Globalization Profile Master Pages Membership Roles Etc.
Look at it differently! Core infrastructure Different presentation options Presentation Runtime ASP.NET Dynamic Data ASP.NET WebForms ASP.NET MVC ASP.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! Separation of concerns! Controller (Input) Model (Logic) View (Presentation)
What does an MVC request look like? Controller Handles input (HTTP requests) View Visually represents the model ViewData Request View Controller Response
DEMO File > 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! Controller Builder Controller Factory Controller View Engine View Controller Action Invoker Action Result Action Filters Model  Binders
DEMO Building 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[ &quot; Message &quot; ],  &quot; Hello &quot; ); }
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(&quot;&quot;, &quot;&quot;)).Returns(false); controller.MembershipProviderInstance = mock.Object; var result = controller.Authenticate(&quot;&quot;, &quot;&quot;) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, &quot;Index&quot;); Assert.AreEqual(controller.ViewData[&quot;ErrorMessage&quot;], &quot;Invalid credentials! Please verify your username and password.&quot;); }
Testing frameworks Any framework is supported! Visual Studio Test NUnit XUnit MBUnit …
DEMO Unit 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 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
Questions and Answers http://blog.maartenballiauw.be [email_address]
© 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.

MSDN - ASP.NET MVC

  • 1.
    ASP.NET MVC (model-view-controller)Maarten Balliauw – RealDolmen http://blog.maartenballiauw.be
  • 2.
    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 
  • 8.
    MVC? Model-View-Controller! Separationof concerns! Controller (Input) Model (Logic) View (Presentation)
  • 9.
    What does anMVC request look like? Controller Handles input (HTTP requests) View Visually represents the model ViewData Request View Controller Response
  • 10.
  • 11.
    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
  • 14.
    DEMO Building anASP.NET MVC application
  • 15.
    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[ &quot; Message &quot; ], &quot; Hello &quot; ); }
  • 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(&quot;&quot;, &quot;&quot;)).Returns(false); controller.MembershipProviderInstance = mock.Object; var result = controller.Authenticate(&quot;&quot;, &quot;&quot;) as ViewResult; Assert.IsNotNull(result); Assert.AreEqual(result.ViewName, &quot;Index&quot;); Assert.AreEqual(controller.ViewData[&quot;ErrorMessage&quot;], &quot;Invalid credentials! Please verify your username and password.&quot;); }
  • 19.
    Testing frameworks Anyframework is supported! Visual Studio Test NUnit XUnit MBUnit …
  • 20.
  • 21.
    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
  • 25.
    Questions and Answershttp://blog.maartenballiauw.be [email_address]
  • 26.
    © 2008 MicrosoftCorporation. 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.