MSDN - ASP.NET MVC

16,539 views

Published on

Introduction to the ASP.NET MVC framework. (Belgium MSDN session)

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
16,539
On SlideShare
0
From Embeds
0
Number of Embeds
1,267
Actions
Shares
0
Downloads
527
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • MSDN - ASP.NET MVC

    1. 1. ASP.NET MVC (model-view-controller) Maarten Balliauw – RealDolmen http://blog.maartenballiauw.be
    2. 2. Who am I? <ul><li>Maarten Balliauw </li></ul><ul><li>Antwerp, Belgium </li></ul><ul><li>www.realdolmen.com </li></ul><ul><li>Focus on web </li></ul><ul><ul><li>ASP.NET, ASP.NET MVC, PHP, Azure, VSTS, … </li></ul></ul><ul><li>http://blog.maartenballiauw.be </li></ul><ul><li>http://twitter.com/maartenballiauw </li></ul>
    3. 3. Agenda <ul><li>Overview of ASP.NET </li></ul><ul><li>ASP.NET MVC </li></ul><ul><li>Building an application </li></ul><ul><li>Unit testing </li></ul><ul><li>Should I choose Webforms or MVC? </li></ul><ul><li>Q&A </li></ul>
    4. 4. ASP.NET… <ul><li>“ one web application framework to rule them all” </li></ul><ul><li>Always seen as one whole… </li></ul>ASP.NET Caching Modules Handlers Intrinsics Pages Controls Globalization Profile Master Pages Membership Roles Etc.
    5. 5. Look at it differently! <ul><li>Core infrastructure </li></ul><ul><li>Different presentation options </li></ul>Presentation Runtime ASP.NET Dynamic Data ASP.NET WebForms ASP.NET MVC ASP.NET Core
    6. 6. ASP.NET MVC <ul><li>A new presentation option for ASP.NET </li></ul><ul><li>Simpler way to program ASP.NET </li></ul><ul><li>Easily testable and TDD friendly </li></ul><ul><li>More control over your <html/> </li></ul><ul><li>More control over your URLs </li></ul><ul><li>Not for everyone! (car vs. motorcycle) </li></ul><ul><li>Not a replacement for webforms! </li></ul><ul><li>Supports existing ASP.NET features </li></ul>
    7. 7. Things you will lose… <ul><li>Viewstate </li></ul><ul><li>Postbacks </li></ul><ul><li>Tricky interview questions about nuances of the page lifecycle  </li></ul>
    8. 8. MVC? Model-View-Controller! Separation of concerns! Controller (Input) Model (Logic) View (Presentation)
    9. 9. What does an MVC request look like? Controller Handles input (HTTP requests) View Visually represents the model ViewData Request View Controller Response
    10. 10. DEMO File > New…
    11. 11. Framework Goals <ul><li>Easy and frictionless testability </li></ul><ul><li>Full control over your <html/> </li></ul><ul><li>Full control over your URLs </li></ul><ul><li>Leverage existing ASP.NET features </li></ul><ul><li>Conventions and guidance </li></ul><ul><li>… </li></ul>
    12. 12. Clean URLs <ul><li>Would you use: </li></ul><ul><li>/Products.aspx?CategoryID={3F2504E0-4F89-11D3-9A0C-0305E82C3301} </li></ul><ul><li>Or: </li></ul><ul><li>/Products/Books </li></ul>My Favorite
    13. 13. Extensibility <ul><li>These come into play… </li></ul><ul><li>… and are all replacable! </li></ul>Controller Builder Controller Factory Controller View Engine View Controller Action Invoker Action Result Action Filters Model Binders
    14. 14. DEMO <ul><li>Building an ASP.NET MVC application </li></ul>
    15. 15. Testing <ul><li>Has anyone tried testing webforms? </li></ul><ul><li>Without IIS being fired up? </li></ul><ul><li>Each component tested individually? </li></ul><ul><li>Did you require vacation afterwards? </li></ul>
    16. 16. Infrastructure designed for testing <ul><li>These are all easily mockable! </li></ul><ul><ul><li>HttpContextBase, HttpResponseBase, HttpRequestBase </li></ul></ul><ul><li>Extensibility </li></ul><ul><ul><li>IController </li></ul></ul><ul><ul><li>IControllerFactory </li></ul></ul><ul><ul><li>IRouteHandler </li></ul></ul><ul><ul><li>IViewEngine, IView </li></ul></ul>
    17. 17. Testing controller actions <ul><li>No requirement to test within ASP.NET runtime! </li></ul><ul><ul><li>Can mock parts of runtime you want to fake </li></ul></ul><ul><ul><li>Using Moq, Rhino, TypeMock, … </li></ul></ul><ul><ul><ul><li>http://code.google.com/p/moq/ </li></ul></ul></ul>[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. 18. Testing controller actions using Moq <ul><li>More on Mocking? Visit VISUG on May 7, 2009 – www.visug.be) </li></ul>[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. 19. Testing frameworks <ul><li>Any framework is supported! </li></ul><ul><ul><li>Visual Studio Test </li></ul></ul><ul><ul><li>NUnit </li></ul></ul><ul><ul><li>XUnit </li></ul></ul><ul><ul><li>MBUnit </li></ul></ul><ul><ul><li>… </li></ul></ul>
    20. 20. DEMO <ul><li>Unit testing </li></ul>
    21. 21. Choosing between Webforms and MVC <ul><li>ASP.NET Webforms </li></ul><ul><li>Winforms-alike event model </li></ul><ul><li>Familiar controls </li></ul><ul><li>Familiar = rapid application development </li></ul><ul><li>Functionality per page </li></ul><ul><li>Uses viewstate for state management </li></ul><ul><li>Less control over rendered HTML </li></ul><ul><li>ASP.NET MVC </li></ul><ul><li>Less complex: separation of concerns </li></ul><ul><li>Easier parallel development </li></ul><ul><li>TDD support </li></ul><ul><li>No viewstate, … </li></ul><ul><li>Full control over behavior and HTML </li></ul><ul><li>Makes you think </li></ul>
    22. 22. So what do I choose? <ul><li>Go with the flow! </li></ul><ul><li>Remember: mixing of worlds is possible! </li></ul><ul><ul><li>Mixing with ASP.NET Webforms </li></ul></ul><ul><ul><li>Mixing with dynamic data </li></ul></ul><ul><ul><li>… </li></ul></ul>
    23. 23. Takeaways <ul><li>A new option for ASP.NET </li></ul><ul><ul><li>Not a replacement for Webforms! </li></ul></ul><ul><li>More control over your <html/> and URLs </li></ul><ul><li>Strong, frictionless testability </li></ul>
    24. 24. Resources <ul><li>“ ASP.NET MVC 1.0 Quickly” http://tinyurl.com/mvcquickly </li></ul><ul><li>My blog http://blog.maartenballiauw.be/category/MVC.aspx </li></ul><ul><li>Microsoft http://www.asp.net/mvc http://wiki.asp.net/page.aspx/286/aspnet-mvc-framework/ </li></ul><ul><li>http://blog.wekeroad.com/blog/i-spose-ill-just-say-it-you-should-learn-mvc/ </li></ul><ul><li>Community projects http://mvccontrib.codeplex.com http://mvcsitemap.codeplex.com </li></ul>
    25. 25. Questions and Answers <ul><li>http://blog.maartenballiauw.be </li></ul><ul><li>[email_address] </li></ul>
    26. 26. © 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.

    ×