Asp.Net MVC Framework Design Pattern

50 %
50 %
Information about Asp.Net MVC Framework Design Pattern

Published on June 19, 2008

Author: maddinapudi

Source: slideshare.net

Description

Asp.Net MVC Framework Design Pattern

The ASP.NET MVC Framework Sarang Datye sarang.datye@microsoft.com http://www.dotnetbetaworks.com

ASP.NET MVC is… A new Web Project Type for ASP.NET. An option. More control over your <html/> A more easily Testable Framework. Not for everyone.

What’s the Point? This is not Web Forms 4.0 It’s about alternatives. Car vs. Motorcycle. Flexible Extend it. Or not. Fundamental Part of System.Web and isn’t going anywhere. Plays Well With Others Feel free to use NHibernate for Models, Brail for Views and Whatever for Controllers. Keep it simple and DRY

Goodness – Driving Goals Maintain Clean Separation of Concerns Easy Testing Red/Green TDD Highly maintainable applications by default Extensible and Pluggable Support replacing any component of the system

Goodness – Driving Goals Enable clean URLs and HTML SEO and REST friendly URL structures Great integration within ASP.NET All the same providers still work Membership, Session, Caching, etc. ASP.NET Designer Surface in VS2008

MVC Model View Controller

A Little More Detail •Browser requests /Products/ Model •Route is determined •Controller is activated •Method on Controller is invoke •Controller does some stuff •Renders View, passing in custom ViewData •URLs are rendered, pointing to other Controller View Controllers

Even More Detail – Request Flow • You can futz at each step Request in the process HTTP Http Controller Response Routing Handler Route View Route View Handler Engine

Extensibility Views Controllers Models Routes …are all Pluggable

Demo – Complete Application It’s your thing. Do what you wanna do.

What’s the Point? This is not Web Forms 4.0 It’s about alternatives. Car vs. Motorcycle. Flexible Extend it. Or not. Fundamental Part of System.Web and isn’t going anywhere. Plays Well With Others Feel free to use NHibernate for Models, Brail for Views and Whatever for Controllers. Keep it simple and DRY

URL Routing – Pretty URIs Developers adds Routes to a global RouteTable Mapping creates a RouteData - a bag of key/values RouteTable.Routes.Add( new Route(quot;blog/bydate/{year}/{month}/{day}quot;, new MvcRouteHandler()){ Defaults = new RouteValueDictionary { {quot;controllerquot;, quot;blogquot;}, {quot;actionquot;, quot;showquot;} }, Constraints = new RouteValueDictionary { {quot;yearquot;, @quot;d{1.4}quot;}, {quot;monthquot;, @quot;d{1.2}quot;}, {quot;dayquot;, @quot;d{1.2}quot;}} })

Testing Controller Actions No requirement to test within ASP.NET runtime. Use RhinoMocks or TypeMock Create Test versions of the parts of the runtime you want to stub [TestMethod] public void ShowPostsDisplayPostView() { TestPostRepository repository = new TestPostRepository(); TestViewEngine viewEngine = new TestViewEngine(); BlogController controller = new BlogController(…); controller.ShowPost(2); Assert.AreEqual(quot;showpostquot;,viewEngine.LastRequestedView); Assert.IsTrue(repository.GetPostByIdWasCalled); Assert.AreEqual(2, repository.LastRequestedPostId); }

Controller Base Controller Class Basic Functionality most folks will use IController Interface Ultimate Control for the Control Freak IControllerFactory For plugging in your own stuff (IOC, etc)

Basic Controller Handling Scenarios, Goals and Design URLs route to controller “actions”, not pages – mark actions in Controller. Controller executes logic, chooses view. All public methods are accessible public void ShowPost(int id) { Post p = PostRepository.GetPostById(id); if (p != null) { RenderView(quot;showpostquot;, p); } else { RenderView(quot;nosuchpostquot;, id); } }

Controller Base Class public class Controller : IController { … protected virtual void Execute(ControllerContext controllerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual bool InvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfo methodInfo); protected virtual bool OnError(string actionName, MethodInfo methodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContext filterContext); protected virtual bool OnActionExecuting(FilterExecutedContext filterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

Controller – Regular APIs public class Controller : IController { … protected virtual void Execute(ControllerContext controllerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual bool InvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfo methodInfo); protected virtual bool OnError(string actionName, MethodInfo methodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContext filterContext); protected virtual bool OnActionExecuting(FilterExecutedContext filterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

Controller – Customization APIs public class Controller : IController { … protected virtual void Execute(ControllerContext controllerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual bool InvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfo methodInfo); protected virtual bool OnError(string actionName, MethodInfo methodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContext filterContext); protected virtual bool OnActionExecuting(FilterExecutedContext filterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

Controller – Test Hooks public class Controller : IController { … protected virtual void Execute(ControllerContext controllerContext); protected virtual void HandleUnknownAction(string actionName); protected virtual bool InvokeAction(string actionName); protected virtual void InvokeActionMethod(MethodInfo methodInfo); protected virtual bool OnError(string actionName, MethodInfo methodInfo, Exception exception); protected virtual void OnActionExecuted(FilterExecutedContext filterContext); protected virtual bool OnActionExecuting(FilterExecutedContext filterContext); protected virtual void RedirectToAction(object values); protected virtual void RenderView(string viewName, string masterName, object viewData); }

© 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.

Add a comment

Related pages

ASP.NET MVC | The ASP.NET Site

Download ASP.NET MVC and learn how to build ... to ASP.NET MVC; Create an ASP.NET Web App ... MVC 5 application with Entity Framework. ASP.NET MVC ...
Read more

Architecture Guide: ASP.NET MVC Framework + N-tier ...

Architecture Guide: ASP.NET MVC ... So that first to be introduced is the pattern and then the framework. Why ASP.NET ... this very same design pattern ...
Read more

ASP.NET MVC Overview - msdn.microsoft.com

The ASP.NET MVC framework provides an alternative to the ASP.NET Web ... ASP.NET MVC Overview. ASP.NET MVC ... MVC is a standard design pattern that many ...
Read more

.NET Design Pattern Framework - dofactory.com

.NET Design Pattern Framework and SPARK Platform. ... ASP.NET MVC Patterns ... MVC is based on the MVC (Model View Controller) design pattern, ...
Read more

CodePlex - ASP.NET Design Patterns - ASP.NET MVC 2 Case ...

ASP.NET MVC 2 Case Study Starter Kit as featured in ... Introducing Patterns & Design Principles. 1. The Pattern for successful ... Entity Framework, ...
Read more

Design Patterns 3,5: Das ASP.NET MVC Framework

Da im letzten Artikel der Design Patterns Reihe ASP.NET MVC aufgekommen ist will ich hier eine kurze Einführung in das neue ASP.net MVC Framework zu geben ...
Read more

The MVC Pattern and ASP.NET MVC - Back to Basics | DotNetCurry

The MVC Pattern and ASP.NET MVC - Back to Basics ... and was available for use in both WebForms and ASP.NET MVC.Framework. ... of SOLID design ...
Read more

ASP.NET MVC 4 | The ASP.NET Site

ASP.NET MVC 4 ASP.NET MVC 4 is a framework for ... standards-based web applications using well-established design patterns and the power of ASP.NET and ...
Read more

Model View Controller – Wikipedia

... (MVC, englisch für Modell ... Ein fertiges GUI-Framework beinhaltet: eine Präsentation (view) ... Portland Pattern Repository – Model View ...
Read more