Professional Documents
Culture Documents
MVC
Intro
Goodness
Goodness
Its about alternatives. Car vs. Motorcycle. Extend it, add IOC. Or not. If the shoe pinches, dont wear it.
Fundamental
Part of System.Web and isnt going anywhere. Feel free to use NHibernate for Models, Brail for Views and Whatever for Controllers. Be Happy.
MVC
MVC
How it works
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
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. Loosely typed or strongly typed data
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 Click to edit Master subtitle style
MVC
How To TEST IT
Mockable Intrinsics
Extensibility
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
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
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
Wasnt edit Master subtitle style Click to this demo technically supposed to be first?
Demo ImageGen
Its your thing. Click to edit Master subtitle style Do what you wanna do.
Demo XML-RPC
Click to edit Master subtitle style
Conclusion
Its about alternatives. Car vs. Motorcycle. Extend it, add IOC. Or not. If the shoe pinches, dont wear it.
Fundamental
Part of System.Web and isnt going anywhere. Feel free to use NHibernate for Models, Brail for Views and VB for Controllers. Be Happy.