MVC A new Web Project Type for ASP.NET. An option. More control over your More control over your A more easily Testable Framework. Not for everyone.

Slides:



Advertisements
Similar presentations
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Advertisements

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Windows 8 (1) (2) (3) Windows 8 (1) (2) (3)
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
INTRODUCTION TO ASP.NET MVC AND EXAMPLE WALKTHROUGH RAJAT ARYA EFECS - OIM DAWG – 4/21/2009 ASP.NET MVC.
Feature: Reprint Outstanding Transactions Report © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Jess Chadwick Website Manager, Infragistics
Feature: Purchase Requisitions - Requester © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
ASP.NET Web Forms vs. MVC Choosing the right one for web development Harish Ranganathan Senior Evangelist Microsoft Corporation.
MIX 09 4/15/ :14 PM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
 Stephen Walther ASP.NET MVC Ninja Microsoft Corporation PV31.
Co- location Mass Market Managed Hosting ISV Hosting.
06 | Implementing Web APIs Jon Galloway | Tech Evangelist Christopher Harrison | Head Geek.
Windows 7 Training Microsoft Confidential. Windows ® 7 Compatibility Version Checking.
AMS304: Introduction to the ASP.NET Model View Controller (MVC) Framework Scott Hanselman Eilon Lipton Microsoft Microsoft
Feature: Purchase Order Prepayments II © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
var site="s15gizmodo" var site="s15gizmodo"
Feature: OLE Notes Migration Utility
Feature: Web Client Keyboard Shortcuts © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
CONTROL ASP.NET Services Caching Routing Localization … ASP.NET WebForms Control EcosystemControl Ecosystem Automatic StateAutomatic.
Session 1.
Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.
Built by Developers for Developers…. © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
 Rico Mariani Architect Microsoft Corporation.
 Bertrand Le Roy Senior Program Manager Lead Microsoft Corporation PC32.
Feature: Assign an Item to Multiple Sites © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
Windows 8 (1) (2) (3) Windows 8 (1) (2) (3)
WinHEC /22/2017 © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Building Web Sites with ASP.NET MVC Framework Noam King CTO Sela College
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Connect with life Connect with life
WEB 304 An Overview of ASP.NET and Windows Workflow Foundation Kashif Alam Program Manager Developer Division Microsoft Corporation.
Introducing Dynamic Data DemosRoadmap Feedback and Q&A.
NEXT: Overview – Sharing skills & code.
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or.
Feature: Document Attachment –Replace OLE Notes © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
Feature: Customer Combiner and Modifier © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
demo Instance AInstance B Read “7” Write “8”

© Copyright SELA Software & Education Labs Ltd Baruch Hirsch St. Bnei Brak Israel Noam King CTO Sela College
customer.
2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.
demo © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names.
demo Demo.
Feature: Void Historical/Open Transaction Updates © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
demo QueryForeign KeyInstance /sm:body()/x:Order/x:Delivery/y:TrackingId1Z
Top model AMERICA’S NEXT view controller. ScottGu Wrote The First Prototype of ASP.NET MVC While Flying A Plane In.
projekt202 © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are.
The CLR CoreCLRCoreCLR © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product.
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks.

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or.
Scott Guthrie
Virtual techdays INDIA │ 9-11 February 2011 SESSION TITLE Kamala Rajan S │ Technical Manager, Marlabs.

IoCompleteRequest (Irp);... p = NULL; …f(p);
Возможности Excel 2010, о которых следует знать
Driving Experiences via Services Using the Microsoft .NET Framework
ASP.NET Roadmap Mike Ormond Developer & Platform Group Microsoft Ltd
ASP.NEXT The ASP.NET 3.5 Extensions
Title of Presentation 12/2/2018 3:48 PM
8/04/2019 9:13 PM © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Виктор Хаджийски Катедра “Металургия на желязото и металолеене”
Шитманов Дархан Қаражанұлы Тарих пәнінің
Title of Presentation 5/24/2019 1:26 PM
7/19/2019 2:53 AM © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Presentation transcript:

MVC

A new Web Project Type for ASP.NET. An option. More control over your More control over your A more easily Testable Framework. Not for everyone.

“ScottGu”

“The Gu”

“His Gu-ness”

“Sir”

“Master Chief Gu”

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

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

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

Microsoft Visual Business Enabler Studio 2008R3v2 September Technical Preview Refresh MSVBES2k8R3v2lptrczstr for short

MVC 27

Model ControllerView

ModelController View 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

You can futz at each step in the process Request HTTP Routing Route Route Handler Http Handler Controller View Engine View Response

MVC

Developers 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 { })

ViewsControllersModelsRoutes …are all Pluggable

View Engines render output You get WebForms by default Can implement your own MVCContrib has ones for Brail, Nvelocity NHaml is an interesting one to watch View Engines can be used to Offer new DSLs to make HTML easier Generate totally different mime/types Images, RSS, JSON, XML, OFX, VCards, whatever.

ViewEngineBase public abstract class ViewEngineBase { public abstract class ViewEngineBase { public abstract void RenderView(ViewContext viewContext); public abstract void RenderView(ViewContext viewContext); }

Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> ( ) ( ) </asp:Content>

%h2= ViewData.CategoryName %ul - foreach (var product in ViewData.Products) %li = product.ProductName.editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" }) - foreach (var product in ViewData.Products) %li = product.ProductName.editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" })

MVC

Mockable Intrinsics HttpContextBase, HttpResponseBase, HttpRequestBase ExtensibilityIControllerIControllerFactoryIRouteHandlerViewEngineBase

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); } [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); }

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

HAI! IM IN YR Northwind HOW DUZ I ListProducts YR id PRODUCTS = GETPRODUCTS id OMG FOUND YR PRODUCTS IF U SEZ IM OUTTA YR Northwind

HAI! WTF: I HAS A STRING GIMMEH STRING IM IN YR VAR UPPIN YR 0 TIL LENGTH OF STRING VISIBLE “GIMMEE A ” STRING!!VAR STEPPIN IM OUTTA YR VAR OMGWTF HALP!

Be well, write good code, and stay in touch

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

MVC

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)

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("showpost", p); } else { RenderView("nosuchpost", id); }

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

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

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

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

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

View Engines render output You get WebForms by default Can implement your own MVCContrib has ones for Brail, Nvelocity NHaml is an interesting one to watch View Engines can be used to Offer new DSLs to make HTML easier to write Generate totally different mime/types Images RSS, JSON, XML, OFX, etc. VCards, whatever.

ViewEngineBase public abstract class ViewEngineBase { public abstract class ViewEngineBase { public abstract void RenderView(ViewContext viewContext); public abstract void RenderView(ViewContext viewContext); }

Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %> ( ) ( ) </asp:Content>

%h2= ViewData.CategoryName %ul - foreach (var product in ViewData.Products) %li = product.ProductName.editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" }) - foreach (var product in ViewData.Products) %li = product.ProductName.editlink = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID }) = Html.ActionLink("Add New Product", new { Action="New" })

Scenarios, Goals and Design: Hook creation of controller instance Dependency Injection. Object Interception. public interface IControllerFactory { IController CreateController(RequestContext context, string controllerName); } protected void Application_Start(object s, EventArgs e) { ControllerBuilder.Current.SetControllerFactory( typeof(MyControllerFactory)); }

Scenarios, Goals and Design: Mock out views for testing Replace ASPX with other technologies public interface IViewEngine { void RenderView(ViewContext context); } Inside controller class: this.ViewEngine = new XmlViewEngine(...); RenderView("foo", myData);