O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
Próximos SlideShares
How to install ubuntu server 12.04 lts precise pangolin
Transfira para ler offline e ver em ecrã inteiro.


Introduction To ASP.NET MVC

Baixar para ler offline

This is a presentation I gave to the Charteris TechDay in January 2008 and will be giving at DDD Ireland.

Livros relacionados

Gratuito durante 30 dias do Scribd

Ver tudo

Introduction To ASP.NET MVC

  1. 1. Introducing ASP.NET MVC Alan Dean, Senior Technologist
  2. 2. <ul><li>Model-View-Controller (MVC) </li></ul><ul><li>is a well-known design pattern </li></ul><ul><li>The original 1978 implementation is described </li></ul><ul><li>in depth in the influential 1992 paper </li></ul><ul><ul><li>“ Applications Programming in Smalltalk-80: </li></ul></ul><ul><ul><li>How to use Model-View-Controller (MVC)” </li></ul></ul><ul><ul><li>by Steve Burbeck </li></ul></ul>
  3. 3. <ul><li>…“ the concept of the design pattern in software provides a key to helping developers leverage the expertise of other skilled architects.” </li></ul><ul><ul><li>Grady Booch, 1995 </li></ul></ul>
  4. 5. MVC consists of three kinds of objects <ul><li>The Model is the application object </li></ul><ul><li>The View is the screen presentation </li></ul><ul><li>The Controller defines the way the user interface reacts to user input </li></ul>
  5. 6. <ul><li>Before MVC, user interface designs tended </li></ul><ul><li>to lump these objects together </li></ul><ul><li>MVC decouples them to </li></ul><ul><li>increase flexibility and reuse </li></ul>
  6. 7. Controller Model View
  7. 8. <ul><ul><li>In his paper </li></ul></ul><ul><ul><li>Steve Burbeck describes two variations of MVC </li></ul></ul><ul><li>a passive model and an active model </li></ul>
  8. 9. <ul><li>The passive model is employed when one controller manipulates the model exclusively </li></ul><ul><li>The controller modifies the model and then informs the view that the model has changed and should be refreshed </li></ul><ul><li>The model in this scenario is completely independent of the view and the controller, which means that there is no means for the model to report changes in its state </li></ul>
  9. 10. Controller View Model handleEvent service update getData
  10. 11. <ul><li>The HTTP protocol is an example of this. </li></ul><ul><li>The browser displays the view and responds to user input, but it does not detect changes in the data on the server. </li></ul><ul><li>Only when the browser explicitly requests a refresh is the server interrogated for changes. </li></ul>
  11. 12. Separation of Concerns (SoC) <ul><li>Object types become ‘pluggable’ </li></ul><ul><li>Intra-team dependency is reduced </li></ul><ul><li>Testability is enhanced </li></ul><ul><li>Application flow can be hard to grok </li></ul>
  12. 13. MVC Web Frameworks <ul><li>Java has Swing , Struts , Grails and others </li></ul><ul><li>Perl has Catalyst , Gantry , Jifty and others </li></ul><ul><li>PHP has Zend , Zoop , Agavi and others </li></ul><ul><li>Python has Django , Gluon , Pylon and others </li></ul><ul><li>Ruby on Rails is famously ‘opinionated’ </li></ul><ul><li>… and .NET? </li></ul>
  13. 14. .NET MVC Web Frameworks <ul><li>Spring.NET </li></ul><ul><ul><li>http://www.springframework.net/ </li></ul></ul><ul><li>Maverick.NET </li></ul><ul><ul><li>http://mavnet.sourceforge.net/ </li></ul></ul><ul><li>MonoRail </li></ul><ul><ul><li>http://www.castleproject.org/monorail/ </li></ul></ul><ul><li>… and now ASP.NET MVC from Microsoft </li></ul><ul><ul><li>http://asp.net/downloads/3.5-extensions/ </li></ul></ul>
  14. 15. <ul><li>“ The ASP.NET MVC framework is a lightweight, highly testable presentation framework that is integrated with existing ASP.NET features, such as master pages and membership-based authentication. </li></ul><ul><li>The MVC framework is defined in the System.Web.Mvc namespace and is a fundamental, supported part of the System.Web namespace” </li></ul>
  15. 16. Demo Create a new ASP.NET MVC Solution
  16. 17. HTTP Request Flow HTTP Response GET /Home/Index HTTP/1.1 HTTP Request Route Handler Controller Model View
  17. 18. HTTP Request Flow public class Global : HttpApplication { protected void Application_Start( object sender, EventArgs e) { RouteTable .Routes.Add( new Route { Url = &quot;[controller]/[action]/[id]&quot; , Defaults = new { action = &quot;Index&quot; , id = ( string ) null }, RouteHandler = typeof ( MvcRouteHandler ) }); } } HTTP Response Route Handler HTTP Request Controller Model View
  18. 19. HTTP Request Flow public class HomeController : Controller { [ ControllerAction ] public void Index() { CompanyInfo companyInfo = new CompanyInfo (); companyInfo.CompanyName = &quot;Your company name here&quot; ; RenderView( &quot;Index&quot; , companyInfo); } } HTTP Response Controller HTTP Request Route Handler Model View
  19. 20. HTTP Request Flow public class CompanyInfo { public string CompanyName { get ; set ; } } HTTP Response Model HTTP Request Route Handler Controller View
  20. 21. HTTP Request Flow public partial class Index : ViewPage < CompanyInfo > { } Response View <% @ Page Language =&quot;C#&quot; AutoEventWireup =&quot;true&quot; CodeBehind =&quot;Index.aspx.cs&quot; Inherits =&quot;MvcApplication.Views.Home.Index&quot; %> < html > < head > < title > <%= this.ViewData.companyName %> </ title > </ head > < body > <%= Html.ActionLink( “Homepage&quot; , &quot;Index&quot; , &quot;Home&quot; ) %> < div > Welcome! </ div > </ body > </ html > HTTP Request Route Handler Controller Model
  21. 22. HTTP Request Flow HTTP/1.1 200 OK Content-Type: text/html <html> <head> <title>Your company name here</title> </head> <body> <a href=&quot;/Home/Index&quot;>Index</a> <div>Welcome!</div> </body> </html> HTTP Request Route Handler Controller Model View HTTP Response
  22. 23. Wiki A wiki is software that allows users to create, edit, and link web pages easily
  23. 24. <ul><li>Ward Cunningham, developer of the first wiki, WikiWikiWeb , originally described it as </li></ul><ul><li>&quot;the simplest online database that could possibly work&quot; </li></ul>
  24. 25. Wiki Database <ul><li>CREATE TABLE [dbo].[PageTable] </li></ul><ul><li>( </li></ul><ul><li>[Id] int IDENTITY (1,1) NOT NULL , </li></ul><ul><li>[Guid] uniqueidentifier NOT NULL , </li></ul><ul><li>[LastModified] datetime NOT NULL , </li></ul><ul><li>[Removed] datetime NULL , </li></ul><ul><li>[Title] varchar (255) NOT NULL , </li></ul><ul><li>[Body] ntext NOT NULL </li></ul><ul><li>); </li></ul>
  25. 26. Wiki HTTP API <ul><li>GET / </li></ul><ul><ul><li>GET /page.html </li></ul></ul><ul><li>POST /page </li></ul><ul><li>GET /page/[title] </li></ul><ul><ul><li>GET /page/[title].txt </li></ul></ul><ul><ul><li>GET /page/[title].html </li></ul></ul><ul><ul><li>GET /page/[title].atom </li></ul></ul><ul><li>PUT /page/[title] </li></ul><ul><li>DELETE /page/[title] </li></ul>
  26. 27. GET <ul><li>The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. </li></ul><ul><li>Safe & Idempotent </li></ul>
  27. 28. <ul><li>GET / </li></ul><ul><li>Accept: text/html, */* </li></ul><ul><li>303 See Other </li></ul><ul><li>Location: http://localhost/page.html </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Expires: Thu, 31 Jan 2008 16:00:00 GMT </li></ul>
  28. 29. <ul><li>GET /page.html </li></ul><ul><li>Accept: text/html, */* </li></ul><ul><li>200 OK </li></ul><ul><li>Content-Type: text/html </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Expires: Thu, 31 Jan 2008 16:00:00 GMT </li></ul><ul><li><html> </li></ul><ul><li><head><title>…</title></head> </li></ul><ul><li><body> </li></ul><ul><li><form </li></ul><ul><li>action=&quot; /page &quot; method=&quot; post &quot; </li></ul><ul><li>enctype=&quot; application/x-www-form-urlencoded &quot;> </li></ul><ul><li><input type=&quot;text&quot; name=&quot;title&quot; maxlength=&quot;255&quot; /> </li></ul><ul><li><textarea name=&quot;body&quot; rows=&quot;25&quot; cols=&quot;80&quot;></textarea> </li></ul><ul><li><input type=&quot;submit&quot; value=&quot;Create Page&quot; /> </li></ul><ul><li></form> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  29. 30. POST <ul><li>The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI </li></ul>
  30. 31. <ul><li>POST /page </li></ul><ul><li>Content-Type: application/x-www-form-urlencoded </li></ul><ul><li>title=Welcome&body=Welcome+to+my+new+wiki. </li></ul><ul><li>201 Created </li></ul><ul><li>Content-Type: text/html </li></ul><ul><li>Content-Location: http://localhost/page/Welcome </li></ul><ul><li>Cache-Control: no-cache </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>…</title> </li></ul><ul><li><meta http-equiv=&quot; refresh &quot; </li></ul><ul><li> content=&quot;0;url= http://localhost:64701/page/Welcome &quot;> </li></ul><ul><li></head> </li></ul><ul><li><body>…</body> </li></ul><ul><li></html> </li></ul>
  31. 32. <ul><li>GET /page/Welcome </li></ul><ul><li>Accept: text/html, */* </li></ul><ul><li>303 See Other </li></ul><ul><li>Location: http://localhost/page/Welcome.html </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Last-Modified: Tue, 29 Jan 2008 16:00:00 GMT </li></ul><ul><li>Vary: Accept </li></ul>
  32. 33. <ul><li>GET /page/Welcome.html </li></ul><ul><li>Accept: text/html, */* </li></ul><ul><li>200 OK </li></ul><ul><li>Content-Type: text/html </li></ul><ul><li>Content-Location: http://localhost/page/Welcome </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Last-Modified: Tue, 29 Jan 2008 16:00:00 GMT </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>…</title> </li></ul><ul><li><link </li></ul><ul><li>href=&quot; http://localhost/page/Welcome.atom &quot; </li></ul><ul><li>rel=&quot; alternate &quot; title=&quot;…&quot; </li></ul><ul><li>type=&quot; application/atom+xml &quot; /> </li></ul><ul><li></head> </li></ul><ul><li><body>…</body> </li></ul><ul><li></html> </li></ul>
  33. 34. <ul><li>GET /page/Welcome.atom </li></ul><ul><li>Accept: application/atom+xml </li></ul><ul><li>200 OK </li></ul><ul><li>Content-Type: application/atom+xml </li></ul><ul><li>Content-Location: http://localhost/page/Welcome </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Last-Modified: Tue, 29 Jan 2008 16:00:00 GMT </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8”?> </li></ul><ul><li><feed xml:lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/2005/Atom&quot;> </li></ul><ul><li>… </li></ul><ul><li><link href=&quot; http://localhost:64701/page/Welcome &quot; rel=&quot; source &quot; /> </li></ul><ul><li><link href=&quot; http://localhost:64701/page/Welcome.atom &quot; rel=&quot; self &quot; type=&quot;application/atom+xml&quot; hreflang=&quot;en&quot; title=&quot;…&quot; /> </li></ul><ul><li><link href=&quot; http://localhost:64701/page/Welcome.html &quot; rel=&quot; alternate &quot; type=&quot;text/html&quot; hreflang=&quot;en&quot; title=&quot;…&quot; /> </li></ul><ul><li><link href=&quot; http://localhost:64701/page/Welcome.txt &quot; rel=&quot; alternate &quot; type=&quot;text/plain&quot; hreflang=&quot;en&quot; title=&quot;…&quot; /> </li></ul><ul><li><entry>…</entry> </li></ul><ul><li></feed> </li></ul>
  34. 35. <ul><li>GET /page/Welcome </li></ul><ul><li>Accept: text/plain </li></ul><ul><li>303 See Other </li></ul><ul><li>Location: http://localhost/page/Welcome.txt </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Last-Modified: Tue, 29 Jan 2008 16:00:00 GMT </li></ul><ul><li>Vary: Accept </li></ul><ul><li>GET /page/Welcome.txt </li></ul><ul><li>Accept: text/plain </li></ul><ul><li>200 OK </li></ul><ul><li>Content-Type: text/plain </li></ul><ul><li>Content-Location: http://localhost/page/Welcome </li></ul><ul><li>Cache-Control: public </li></ul><ul><li>Last-Modified: Tue, 29 Jan 2008 16:00:00 GMT </li></ul><ul><li>Welcome to my new wiki. </li></ul>
  35. 36. PUT <ul><li>The PUT method requests that the enclosed entity be stored under the supplied Request-URI. </li></ul><ul><li>If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. </li></ul><ul><li>Idempotent </li></ul>
  36. 37. <ul><li>PUT /page/Welcome </li></ul><ul><li>Content-Type: text/plain </li></ul><ul><li>Welcome to my new [[wiki]]. </li></ul><ul><li>204 No Content </li></ul><ul><li>Cache-Control: no-cache </li></ul>
  37. 38. DELETE <ul><li>The DELETE method requests that the origin server delete the resource identified by the Request-URI. </li></ul><ul><li>Idempotent </li></ul>
  38. 39. <ul><li>DELETE /page/Welcome </li></ul><ul><li>204 No Content </li></ul><ul><li>Cache-Control: no-cache </li></ul>
  39. 41. <ul><li>RE presentational S tate T ransfer </li></ul>
  40. 42. REST: The Web Used Correctly <ul><li>A system or application architecture </li></ul><ul><li>… that uses HTTP, URI and other Web </li></ul><ul><li>standards “correctly” </li></ul><ul><li>… is “on” the Web, not tunnelled through it </li></ul>
  41. 43. REST is an Architectural Style <ul><li>Defines a set of key “constraints” </li></ul><ul><li>… that, if met, make an architecture “RESTful” </li></ul><ul><li>… with the Web as one example </li></ul>
  42. 44. <ul><li>Equate “REST” with “RESTful HTTP usage” </li></ul><ul><li>Stefan Tilkov </li></ul>
  43. 45. Deriving REST <ul><li>Client-Server </li></ul><ul><li>Stateless </li></ul><ul><li>Cache </li></ul><ul><li>Uniform interface </li></ul><ul><li>Layered system </li></ul><ul><li>Code on Demand </li></ul>
  44. 46. <ul><li>“ The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on a uniform interface between components.” </li></ul><ul><li>Roy Fielding </li></ul>
  45. 47. Uniform Interface <ul><li>Uniform resource identification </li></ul><ul><li>A set of well-defined operations for manipulation </li></ul><ul><li>A shared set of media-types </li></ul><ul><li>Hypertext as the engine of application state </li></ul>
  46. 48. Benefits of REST <ul><li>Hypertext is standardized </li></ul><ul><li>fewer UIs </li></ul><ul><li>Identification is standardized </li></ul><ul><li>less communication </li></ul><ul><li>Exchange protocols are standardized </li></ul><ul><li>fewer integrations </li></ul><ul><li>Interactions are standardized </li></ul><ul><li>fewer semantics </li></ul><ul><li>Data formats are standardized </li></ul><ul><li>fewer translations </li></ul>
  47. 49. <ul><li>“ No matter how hard I try, I still think the WS-* stack is bloated, opaque, and insanely complex. I think it is going to be hard to understand, hard to implement, hard to interoperate, and hard to secure.” </li></ul><ul><ul><li>Tim Bray (XML Co-inventor) </li></ul></ul>
  48. 50. <ul><li>“ If you’re ready for REST I suggest you jump on board right away and get ahead of the curve </li></ul><ul><li>You’ll have to train your developers in REST principles. </li></ul><ul><li>You definitely need to provide guidance to your people. </li></ul><ul><li>What you want to do is work to the point where REST becomes the default for all your distributed applications.” </li></ul><ul><ul><li>Anne Thomas Manes (Burton Group) </li></ul></ul>
  49. 52. <ul><li>Links </li></ul><ul><li>http://del.icio.us/alan.dean/mvc </li></ul><ul><li>http://thoughtpad.net/alan-dean </li></ul><ul><li>Email </li></ul><ul><li>[email_address] </li></ul><ul><li>© MMVIII </li></ul>
  • BernardoVieira4

    Apr. 12, 2017
  • shemseshukre

    Feb. 27, 2015
  • ampacheco

    May. 18, 2008

This is a presentation I gave to the Charteris TechDay in January 2008 and will be giving at DDD Ireland.


Vistos totais


No Slideshare


De incorporações


Número de incorporações