asp.net mvc framework design pattern

21
The ASP.NET MVC Framework Sarang Datye [email protected] http://www.dotnetbetaworks.com

Upload: maddinapudi

Post on 14-May-2015

9.045 views

Category:

Technology


4 download

DESCRIPTION

Asp.Net MVC Framework Design Pattern

TRANSCRIPT

Page 1: Asp.Net MVC Framework Design Pattern

The ASP.NET MVC Framework

Sarang [email protected]://www.dotnetbetaworks.com

Page 2: Asp.Net MVC Framework Design Pattern

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.

Page 3: Asp.Net MVC Framework Design Pattern

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

Page 4: Asp.Net MVC Framework Design Pattern

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

Page 5: Asp.Net MVC Framework Design Pattern

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

Page 6: Asp.Net MVC Framework Design Pattern

MVC

Model

ControllerView

Page 7: Asp.Net MVC Framework Design Pattern

A Little More Detail

Model

ControllerView

•Browser requests /Products/

•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

Controllers

Page 8: Asp.Net MVC Framework Design Pattern

Even More Detail – Request Flow

• You can futz at each step

in the processRequest

HTTPRouting

RouteRoute

Handler

HttpHandler

Controller

ViewEngine View

Response

Page 9: Asp.Net MVC Framework Design Pattern

ExtensibilityViews

Controllers

Models

Routes

…are all Pluggable

Page 10: Asp.Net MVC Framework Design Pattern

Demo – Complete Application

It’s your thing. Do what you wanna do.

Page 11: Asp.Net MVC Framework Design Pattern

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

Page 12: Asp.Net MVC Framework Design Pattern

URL Routing – Pretty URIsDevelopers adds Routes to a global RouteTable

Mapping creates a RouteData - a bag of key/values

RouteTable.Routes.Add(

new Route("blog/bydate/{year}/{month}/{day}",

new MvcRouteHandler()){

Defaults = new RouteValueDictionary {

{"controller", "blog"}, {"action", "show"}

},

Constraints = new RouteValueDictionary {

{"year", @"\d{1.4}"},

{"month", @"\d{1.2}"},

{"day", @"\d{1.2}"}}

})

Page 13: Asp.Net MVC Framework Design Pattern

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("showpost",viewEngine.LastRequestedView);

Assert.IsTrue(repository.GetPostByIdWasCalled);

Assert.AreEqual(2, repository.LastRequestedPostId);

}

Page 14: Asp.Net MVC Framework Design Pattern

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)

Page 15: Asp.Net MVC Framework Design Pattern

Basic Controller HandlingScenarios, 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("showpost", p);} else {

RenderView("nosuchpost", id);}

}

Page 16: Asp.Net MVC Framework Design Pattern

Controller Base Classpublic class Controller : IController {

protected virtual void Execute(ControllerContextcontrollerContext);

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(FilterExecutedContextfilterContext);

protected virtual bool OnActionExecuting(FilterExecutedContextfilterContext);

protected virtual void RedirectToAction(object values);

protected virtual void RenderView(string viewName,

string masterName, object viewData);

}

Page 17: Asp.Net MVC Framework Design Pattern

Controller – Regular APIspublic class Controller : IController {

protected virtual void Execute(ControllerContextcontrollerContext);

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(FilterExecutedContextfilterContext);

protected virtual bool OnActionExecuting(FilterExecutedContextfilterContext);

protected virtual void RedirectToAction(object values);

protected virtual void RenderView(string viewName,

string masterName, object viewData);

}

Page 18: Asp.Net MVC Framework Design Pattern

Controller – Customization APIspublic class Controller : IController {

protected virtual void Execute(ControllerContextcontrollerContext);

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(FilterExecutedContextfilterContext);

protected virtual bool OnActionExecuting(FilterExecutedContextfilterContext);

protected virtual void RedirectToAction(object values);

protected virtual void RenderView(string viewName,

string masterName, object viewData);

}

Page 19: Asp.Net MVC Framework Design Pattern

Controller – Test Hookspublic class Controller : IController {

protected virtual void Execute(ControllerContextcontrollerContext);

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(FilterExecutedContextfilterContext);

protected virtual bool OnActionExecuting(FilterExecutedContextfilterContext);

protected virtual void RedirectToAction(object values);

protected virtual void RenderView(string viewName,

string masterName, object viewData);

}

Page 20: Asp.Net MVC Framework Design Pattern

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

Page 21: Asp.Net MVC Framework Design Pattern