asp.net mvc framework design pattern
DESCRIPTION
Asp.Net MVC Framework Design PatternTRANSCRIPT
The ASP.NET MVC Framework
Sarang [email protected]://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
ControllerView
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
Even More Detail – Request Flow
• You can futz at each step
in the processRequest
HTTPRouting
RouteRoute
Handler
HttpHandler
Controller
ViewEngine View
Response
ExtensibilityViews
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 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}"}}
})
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);
}
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 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);}
}
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);
}
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);
}
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);
}
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);
}
© 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.