AMS 304 Introduction to the ASP NET Model

  • Slides: 26
Download presentation
AMS 304: Introduction to the ASP. NET Model View Controller (MVC) Framework Scott Hanselman

AMS 304: Introduction to the ASP. NET Model View Controller (MVC) Framework Scott Hanselman Microsoft [email protected] com Eilon Lipton Microsoft [email protected] com

MVC INTRO

MVC INTRO

Goodness • Maintain Clean Separation of Concerns ● ● ● Easy Testing Red/Green TDD

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

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

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

MVC Model View Controller

A Little More Detail Model View • Browser requests /Products/ • Route is determined

A Little More Detail Model View • Browser requests /Products/ • Route is determined • Controller is activated • Method on Controller is invoke • Controller does some stuff • Renders View, passing in custom View. Data • URLs are rendered, pointing to other Controllers

Even More Detail – Request Flow Request • You. Controller can futz at each

Even More Detail – Request Flow Request • You. Controller can futz at each View step Factory in the process URL Routing Http Handler View Route Handle r Respons e

Demo – Hello MVC World Don’t fall asleep, it’ll be worth it.

Demo – Hello MVC World Don’t fall asleep, it’ll be worth it.

MVC HOW IT WORKS

MVC HOW IT WORKS

Basic Controller Handling • Scenarios, Goals and Design ● ● URLs route to controller

Basic Controller Handling • Scenarios, Goals and Design ● ● URLs route to controller “actions”, not pages – mark actions in Controller executes logic, chooses view. [Controller. Action] public void Show. Post(int id) { Post p = Post. Repository. Get. Post. By. Id(id); if (p != null) { Render. View("showpost", p); } else { Render. View("nosuchpost", id); } }

Basic Views • Scenarios, Goals and Design: ● Are for rendering/output. • Pre-defined and

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 Route. Table

URL Routing – Pretty URIs • Developers adds Routes to a global Route. Table • Mapping creates a Route. Data - a bag of key/values protected void Application_Start(object sender, Event. Args e) { Route. Table. Routes. Add(new Route { Url = "Blog/bydate/[year]/[month]/[day]", Defaults = new { controller="blog", action="showposts" }, Validation = new { [email protected]"d{1, 4}", month= @"d{1, 2}", day = @"d{1, 2}"} }); Route. Table. Routes. Add(new Route { Url = "[controller]/[action]/[id]", Route. Handler = typeof(Mvc. Route. Handler) }); }

Demo – Routing The route less travelled…

Demo – Routing The route less travelled…

MVC HOW TO TEST IT

MVC HOW TO TEST IT

Interfaces and TDD • Mockable Intrinsics ● IHttp. Context, IHttp. Response, IHttp. Request •

Interfaces and TDD • Mockable Intrinsics ● IHttp. Context, IHttp. Response, IHttp. Request • Extensibility ● ● ● IController. Factory IRoute. Handler IView. Factory

Testing Controller Actions • No requirement to mock out full ASP. NET runtime. [Test.

Testing Controller Actions • No requirement to mock out full ASP. NET runtime. [Test. Method] public void Show. Posts. Display. Post. View() { Test. Post. Repository repository = new Test. Post. Repository(); Test. View. Factory view. Factory = new Test. View. Factory(); Blog. Controller controller = new Blog. Controller(…); controller. Show. Post(2); Assert. Are. Equal("showpost", view. Factory. Last. Requested. View); Assert. Is. True(repository. Get. Post. By. Id. Was. Called); Assert. Are. Equal(2, repository. Last. Requested. Post. Id); }

Controller Factory • Scenarios, Goals and Design: ● Hook creation of controller instance •

Controller Factory • Scenarios, Goals and Design: ● Hook creation of controller instance • Dependency Injection. • Object Interception. public interface IController. Factory { IController Create. Controller(IHttp. Context context, Route. Data route. Data, Type controller. Type); } protected void Application_Start(object s, Event. Args e) { Controller. Builder. Current. Set. Default. Controller. Factory( typeof(My. Controller. Factory)); }

View Factory • Scenarios, Goals and Design: ● ● Mock out views for testing

View Factory • Scenarios, Goals and Design: ● ● Mock out views for testing Replace ASPX with other technologies public interface IView. Factory { IView Create. View(IHttp. Context context, Route. Data route. Data, string view. Name, string layout. Name, object view. Data); } Inside controller class: View. Factory = new Xml. View. Factory(. . . ); Render. View("foo", my. Data);

Demo – TDD Wasn’t this demo technically supposed to be first?

Demo – TDD Wasn’t this demo technically supposed to be first?

Demo – Dynamic Data Controls Not DDE. Scared you, didn’t I?

Demo – Dynamic Data Controls Not DDE. Scared you, didn’t I?

Demo – Image. Gen It’s your thing. Do what you wanna do.

Demo – Image. Gen It’s your thing. Do what you wanna do.

Demo – Ruby View Engine & Python Controller It’s a kinder, gentler Microsoft. No

Demo – Ruby View Engine & Python Controller It’s a kinder, gentler Microsoft. No seriously. Hug?

Demo – XML-RPC SOAP is for dorks.

Demo – XML-RPC SOAP is for dorks.

Conclusion • This is not Web Forms 4. 0 ● It’s about alternatives. Car

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

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!