Upload
claivoryant
View
225
Download
0
Embed Size (px)
Citation preview
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 1/26
AMS304: Introduction to the
ASP.NET Model View Controller (MVC) Framework
Scott Hanselman Eilon Lipton
Microsoft [email protected] [email protected]
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 2/26
INTRO
MVC
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 3/26
Goodness
Maintain Clean Separation of ConcernsEasy Testing
Red/Green TDDHighly maintainable applications by default
Extensible and Pluggable
Support replacing any component of thesystem
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 4/26
Goodness
Enable clean URLs and HTMLSEO and REST friendly URL structures
Great integration within ASP.NETSupport both static and dynamic languages
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 5/26
What¶ s th e Poin t?
This is not Web Forms 4.0It¶s about alternatives. Car vs. Motorcycle.
Simple or as complex as you likeExtend it, add IOC. Or not. If the shoe pinches, don¶twear 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.
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 6/26
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 7/26
A Littl e M ore De ta il
ModelModel
Controller Controller ViewView
Browser requests /Products/Route is determinedController is activated
Method on Controller is invokeController does some stuff Renders View, passing in
custom ViewDataURLs are rendered,pointing to other Controllers
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 8/26
Ev en M ore De ta il ± R eques t F low
Y ou can futz at each stepin the process
R eques t R eques t
URLURLRoutingRouting
RouteRoute RouteRouteHandler Handler
HttpHttpHandler Handler Controller Controller
ViewViewFactoryFactory ViewView
R esponseR esponse
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 9/26
Demo ± He lloMVC
Wor ldDon¶t fall asleep, it¶ll be worth it.
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 10/26
HO W IT WORKS
MVC
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 11/26
B a sic C on tro ller H a nd ling
Scenarios, Goals and DesignURLs 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);}
}
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 12/26
B a sic V iews
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 ViewLoosely typed or strongly typed data
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 13/26
U R L R ou ting ± Pre tty U RI sDevelopers adds Routes to a global RouteTableMapping 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) });
}
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 14/26
Demo ±R
ou tingThe route less travelled«
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 15/26
HO W TO T E ST IT
MVC
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 16/26
In terf a ces a nd T DD
Mockable IntrinsicsIHttpContext, IHttpResponse, IHttpRequest
ExtensibilityIController IControllerFactoryIRouteHandler IViewIViewFactory
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 17/26
T es ting C on tro ller A c tions
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);}
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 18/26
C on tro ller F a c tor y
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));}
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 19/26
V iew F a c tor y
Scenarios, Goals and Design:Mock out views for testingReplace 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);
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 20/26
Demo ±T
DDWasn¶t this demo technically
supposed to be first?
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 21/26
Demo ± D yn a mic D ata C on tro ls
Not DDE. Scared you, didn¶t I?
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 22/26
Demo ±Im a geGenIt¶s your thing.
Do what you wanna do.
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 23/26
Demo ± R ub y V iew E ngine& P yth on C on tro ller
It¶s a kinder, gentler Microsoft.No seriously. Hug?
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 24/26
Demo ± XM
L-R
PC
SOAP is for dorks.
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 25/26
C onc lusion
This is not Web Forms 4.0It¶s about alternatives. Car vs. Motorcycle.
Simple or as complex as you likeExtend it, add IOC. Or not. If the shoe pinches, don¶twear 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.
8/3/2019 ton ASP Connections AMS304 MVC
http://slidepdf.com/reader/full/ton-asp-connections-ams304-mvc 26/26
Y our Feedb a ck is Impor ta n t
Please fill out a session evaluation form andeither put them in the basket near the exit
or drop them off at the conferenceregistration desk.
Thank you!