AMS304: Introduction to the
ASP.NET Model View Controller
     (MVC) Framework
Scott Hanselman         Eilon Lipton
   Microsoft              Microsoft
scottha@microsoft.com   elipton@microsoft.com
MVC

INTRO
Goodness

• 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

• Enable clean URLs and HTML
  ●   SEO and REST friendly URL structures

• Great integration within ASP.NET
  ●   Support both static and dynamic languages
What’s the Point?

• This is not Web Forms 4.0
  ●   It’s about alternatives. Car vs. Motorcycle.
• Simple or as complex as you like
  ●   Extend it, add IOC. Or not. If the shoe pinches, don’t
      wear it.
• 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. Be Happy.
MVC


             Model




      View           Controller
A Little More Detail

                      •Browser requests /Products/
                      •Route is determined
         Model        •Controller is activated
                      •Method on Controller is invoke
                      •Controller does some stuff
                      •Renders View, passing in
                            custom ViewData
                             •URLs are rendered,
                              pointing to other
  View           Controller   Controllers
Even More Detail – Request Flow
Request    • You can futz at each step
             in the process


 URL       Http
Routing               Controller   Response
          Handler




           Route       View
 Route                               View
          Handler     Factory
Demo – Hello MVC World

  Don’t fall asleep, it’ll be worth it.
MVC

HOW IT WORKS
Basic Controller Handling

• Scenarios, Goals and Design
  ●   URLs route to controller “actions”, not pages –
      mark actions in Controller.
  ●   Controller executes logic, chooses view.
  [ControllerAction]
 public void ShowPost(int id) {
      Post p = PostRepository.GetPostById(id);
      if (p != null) {
          RenderView("showpost", p);
      } else {
          RenderView("nosuchpost", id);
      }
}
Basic Views

• Scenarios, Goals and Design:
  ●   Are for rendering/output.
       • Pre-defined and extensible rendering helpers
  ●   Can use .ASPX, .ASCX, .MASTER, etc.
  ●   Can replace with other view technologies:
       • Template engines (NVelocity, Brail, …).
       • Output formats (images, RSS, JSON, …).
       • Mock out for testing.
  ●   Controller sets data on the View
       • Loosely typed or strongly typed data
URL Routing – Pretty URIs
    • Developers adds Routes to a global RouteTable
    • Mapping creates a RouteData - a bag of key/values
protected void Application_Start(object sender, EventArgs e)
{

RouteTable.Routes.Add(new Route {
  Url = "Blog/bydate/[year]/[month]/[day]",
  Defaults = new { controller="blog", action="showposts" },
  Validation = new { year=@"d{1,4}", month= @"d{1,2}",
                       day = @"d{1,2}"} });

RouteTable.Routes.Add(new Route {
  Url = "[controller]/[action]/[id]",
  RouteHandler = typeof(MvcRouteHandler) });

}
Demo – Routing

    The route less travelled…
MVC

HOW TO TEST IT
Interfaces and TDD

• Mockable Intrinsics
  ●   IHttpContext, IHttpResponse, IHttpRequest
• Extensibility
  ●   IController
  ●   IControllerFactory
  ●   IRouteHandler
  ●   IView
  ●   IViewFactory
Testing Controller Actions
• No requirement to mock out full ASP.NET runtime.
[TestMethod]
public void ShowPostsDisplayPostView() {
    TestPostRepository repository = new TestPostRepository();
    TestViewFactory viewFactory = new TestViewFactory();

    BlogController controller = new BlogController(…);
    controller.ShowPost(2);

    Assert.AreEqual("showpost", viewFactory.LastRequestedView);
    Assert.IsTrue(repository.GetPostByIdWasCalled);
    Assert.AreEqual(2, repository.LastRequestedPostId);
}
Controller Factory
• Scenarios, Goals and Design:
   ●   Hook creation of controller instance
        • Dependency Injection.
        • Object Interception.

public interface IControllerFactory {
    IController CreateController(IHttpContext context,
         RouteData routeData,
         Type controllerType);
}

protected void Application_Start(object s, EventArgs e) {
  ControllerBuilder.Current.SetDefaultControllerFactory(
    typeof(MyControllerFactory));
}
View Factory
• Scenarios, Goals and Design:
   ●   Mock out views for testing
   ●   Replace ASPX with other technologies
public interface IViewFactory {
    IView CreateView(IHttpContext context,
        RouteData routeData,
        string viewName, string layoutName,
        object viewData);
}

Inside controller class:
    ViewFactory = new XmlViewFactory(...);

   RenderView("foo", myData);
Demo – TDD

   Wasn’t this demo technically
     supposed to be first?
Demo – Dynamic Data
       Controls
     Not DDE. Scared you, didn’t I?
Demo – ImageGen

        It’s your thing.
    Do what you wanna do.
Demo – Ruby View Engine
& Python Controller
   It’s a kinder, gentler Microsoft.
          No seriously. Hug?
Demo – XML-RPC

     SOAP is for dorks.
Conclusion

• This is not Web Forms 4.0
  ●   It’s about alternatives. Car vs. Motorcycle.
• Simple or as complex as you like
  ●   Extend it, add IOC. Or not. If the shoe pinches, don’t
      wear it.
• 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 VB for Controllers. Be Happy.
Your Feedback is Important

Please fill out a session evaluation form and
  either put them in the basket near the exit
      or drop them off at the conference
                registration desk.

                Thank you!

Hanselman lipton asp_connections_ams304_mvc

  • 1.
    AMS304: Introduction tothe ASP.NET Model View Controller (MVC) Framework Scott Hanselman Eilon Lipton Microsoft Microsoft scottha@microsoft.com elipton@microsoft.com
  • 2.
  • 3.
    Goodness • Maintain CleanSeparation of Concerns ● Easy Testing ● Red/Green TDD ● Highly maintainable applications by default • Extensible and Pluggable ● Support replacing any component of the system
  • 4.
    Goodness • Enable cleanURLs and HTML ● SEO and REST friendly URL structures • Great integration within ASP.NET ● Support both static and dynamic languages
  • 5.
    What’s the Point? •This is not Web Forms 4.0 ● It’s about alternatives. Car vs. Motorcycle. • Simple or as complex as you like ● Extend it, add IOC. Or not. If the shoe pinches, don’t wear it. • 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. Be Happy.
  • 6.
    MVC Model View Controller
  • 7.
    A Little MoreDetail •Browser requests /Products/ •Route is determined Model •Controller is activated •Method on Controller is invoke •Controller does some stuff •Renders View, passing in custom ViewData •URLs are rendered, pointing to other View Controller Controllers
  • 8.
    Even More Detail– Request Flow Request • You can futz at each step in the process URL Http Routing Controller Response Handler Route View Route View Handler Factory
  • 9.
    Demo – HelloMVC World Don’t fall asleep, it’ll be worth it.
  • 10.
  • 11.
    Basic Controller Handling •Scenarios, Goals and Design ● URLs route to controller “actions”, not pages – mark actions in Controller. ● Controller executes logic, chooses view. [ControllerAction] public void ShowPost(int id) { Post p = PostRepository.GetPostById(id); if (p != null) { RenderView("showpost", p); } else { RenderView("nosuchpost", id); } }
  • 12.
    Basic Views • Scenarios,Goals and Design: ● Are for rendering/output. • Pre-defined and extensible rendering helpers ● Can use .ASPX, .ASCX, .MASTER, etc. ● Can replace with other view technologies: • Template engines (NVelocity, Brail, …). • Output formats (images, RSS, JSON, …). • Mock out for testing. ● Controller sets data on the View • Loosely typed or strongly typed data
  • 13.
    URL Routing –Pretty URIs • Developers adds Routes to a global RouteTable • Mapping creates a RouteData - a bag of key/values protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new Route { Url = "Blog/bydate/[year]/[month]/[day]", Defaults = new { controller="blog", action="showposts" }, Validation = new { year=@"d{1,4}", month= @"d{1,2}", day = @"d{1,2}"} }); RouteTable.Routes.Add(new Route { Url = "[controller]/[action]/[id]", RouteHandler = typeof(MvcRouteHandler) }); }
  • 14.
    Demo – Routing The route less travelled…
  • 15.
  • 16.
    Interfaces and TDD •Mockable Intrinsics ● IHttpContext, IHttpResponse, IHttpRequest • Extensibility ● IController ● IControllerFactory ● IRouteHandler ● IView ● IViewFactory
  • 17.
    Testing Controller Actions •No requirement to mock out full ASP.NET runtime. [TestMethod] public void ShowPostsDisplayPostView() { TestPostRepository repository = new TestPostRepository(); TestViewFactory viewFactory = new TestViewFactory(); BlogController controller = new BlogController(…); controller.ShowPost(2); Assert.AreEqual("showpost", viewFactory.LastRequestedView); Assert.IsTrue(repository.GetPostByIdWasCalled); Assert.AreEqual(2, repository.LastRequestedPostId); }
  • 18.
    Controller Factory • Scenarios,Goals and Design: ● Hook creation of controller instance • Dependency Injection. • Object Interception. public interface IControllerFactory { IController CreateController(IHttpContext context, RouteData routeData, Type controllerType); } protected void Application_Start(object s, EventArgs e) { ControllerBuilder.Current.SetDefaultControllerFactory( typeof(MyControllerFactory)); }
  • 19.
    View Factory • Scenarios,Goals and Design: ● Mock out views for testing ● Replace ASPX with other technologies public interface IViewFactory { IView CreateView(IHttpContext context, RouteData routeData, string viewName, string layoutName, object viewData); } Inside controller class: ViewFactory = new XmlViewFactory(...); RenderView("foo", myData);
  • 20.
    Demo – TDD Wasn’t this demo technically supposed to be first?
  • 21.
    Demo – DynamicData Controls Not DDE. Scared you, didn’t I?
  • 22.
    Demo – ImageGen It’s your thing. Do what you wanna do.
  • 23.
    Demo – RubyView Engine & Python Controller It’s a kinder, gentler Microsoft. No seriously. Hug?
  • 24.
    Demo – XML-RPC SOAP is for dorks.
  • 25.
    Conclusion • This isnot Web Forms 4.0 ● It’s about alternatives. Car vs. Motorcycle. • Simple or as complex as you like ● Extend it, add IOC. Or not. If the shoe pinches, don’t wear it. • 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 VB for Controllers. Be Happy.
  • 26.
    Your Feedback isImportant Please fill out a session evaluation form and either put them in the basket near the exit or drop them off at the conference registration desk. Thank you!

Editor's Notes