Introduction To ASP.NET MVC

52
Introducing ASP.NET MVC Alan Dean, Senior Technologist

description

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

Transcript of Introduction To ASP.NET MVC

Page 1: Introduction To ASP.NET MVC

Introducing ASP.NET MVC

Alan Dean, Senior Technologist

Page 2: Introduction To ASP.NET MVC

Model-View-Controller (MVC)is a well-known design pattern

The original 1978 implementation is described

in depth in the influential 1992 paper“Applications Programming in Smalltalk-80:How to use Model-View-Controller (MVC)”

by Steve Burbeck

Page 3: Introduction To ASP.NET MVC

…“the concept of the design pattern in software provides a key to helping

developers leverage the expertise of other skilled architects.”

Grady Booch, 1995

Page 4: Introduction To ASP.NET MVC
Page 5: Introduction To ASP.NET MVC

MVC consists of three kinds of objects

The Model is the application object

The View is the screen presentation

The Controller defines the way the user interface reacts to user input

Page 6: Introduction To ASP.NET MVC

Before MVC, user interface designs tended

to lump these objects together

MVC decouples them toincrease flexibility and reuse

Page 7: Introduction To ASP.NET MVC

Controller

Model View

Page 8: Introduction To ASP.NET MVC

In his paperSteve Burbeck describes two variations of

MVC

a passive model and an active model

Page 9: Introduction To ASP.NET MVC

The passive model is employed when one controller manipulates the model

exclusively

The controller modifies the model and then informs the view that the model has changed and should be refreshed

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

Page 10: Introduction To ASP.NET MVC

Controller View Model

handleEventservice

update

getData

Page 11: Introduction To ASP.NET MVC

The HTTP protocol is an example of this.

The browser displays the view and responds to user input, but it does not detect changes in the data on

the server.

Only when the browser explicitly requests a refresh is the server

interrogated for changes.

Page 12: Introduction To ASP.NET MVC

Separation of Concerns (SoC)

Object types become ‘pluggable’

Intra-team dependency is reduced

Testability is enhanced

Application flow can be hard to grok

Page 13: Introduction To ASP.NET MVC

MVC Web Frameworks

Java has Swing, Struts, Grails and othersPerl has Catalyst, Gantry, Jifty and others

PHP has Zend, Zoop, Agavi and othersPython has Django, Gluon, Pylon and

othersRuby on Rails is famously ‘opinionated’

… and .NET?

Page 14: Introduction To ASP.NET MVC

.NET MVC Web Frameworks

Spring.NEThttp://www.springframework.net/

Maverick.NEThttp://mavnet.sourceforge.net/

MonoRailhttp://www.castleproject.org/monorail/

… and now ASP.NET MVC from Microsoft

http://asp.net/downloads/3.5-extensions/

Page 15: Introduction To ASP.NET MVC

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

The MVC framework is defined in the System.Web.Mvc namespace and is

a fundamental, supported part of the System.Web namespace”

Page 16: Introduction To ASP.NET MVC

Demo

Create a new ASP.NET MVC Solution

Page 17: Introduction To ASP.NET MVC

HTTP Request Flow

HTTP Response

GET /Home/Index HTTP/1.1

HTTP Request Route Handler Controller Model View

Page 18: Introduction To ASP.NET MVC

HTTP Request Flow

public class Global : HttpApplication{ protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new Route { Url = "[controller]/[action]/[id]", Defaults = new { action = "Index", id = (string)null }, RouteHandler = typeof(MvcRouteHandler) }); }}

HTTP ResponseRoute HandlerHTTP Request Controller Model View

Page 19: Introduction To ASP.NET MVC

HTTP Request Flow

public class HomeController : Controller{ [ControllerAction] public void Index() { CompanyInfo companyInfo = new CompanyInfo(); companyInfo.CompanyName = "Your company name here"; RenderView("Index", companyInfo); }}

HTTP ResponseControllerHTTP Request Route Handler Model View

Page 20: Introduction To ASP.NET MVC

HTTP Request Flow

public class CompanyInfo{ public string CompanyName { get; set; }}

HTTP ResponseModelHTTP Request Route Handler Controller View

Page 21: Introduction To ASP.NET MVC

HTTP Request Flow

public partial class Index : ViewPage<CompanyInfo>{ }

ResponseView

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApplication.Views.Home.Index" %><html><head> <title><%= this.ViewData.companyName %></title></head><body> <%= Html.ActionLink(“Homepage", "Index", "Home") %> <div>Welcome!</div></body></html>

HTTP Request Route Handler Controller Model

Page 22: Introduction To ASP.NET MVC

HTTP Request Flow

HTTP/1.1 200 OKContent-Type: text/html

<html><head> <title>Your company name here</title></head><body> <a href="/Home/Index">Index</a> <div>Welcome!</div></body></html>

HTTP Request Route Handler Controller Model View HTTP Response

Page 23: Introduction To ASP.NET MVC

Wiki

A wiki is software that allows users to create, edit, and link

web pages easily

Page 24: Introduction To ASP.NET MVC

Ward Cunningham, developer of the first wiki, WikiWikiWeb, originally

described it as

"the simplest online database that could possibly work"

Page 25: Introduction To ASP.NET MVC

Wiki Database

CREATE TABLE [dbo].[PageTable]([Id] int IDENTITY(1,1) NOT NULL,[Guid] uniqueidentifier NOT NULL,[LastModified] datetime NOT NULL,[Removed] datetime NULL,[Title] varchar(255) NOT NULL,[Body] ntext NOT NULL

);

Page 26: Introduction To ASP.NET MVC

Wiki HTTP API

GET /GET /page.html

POST /pageGET /page/[title]

GET /page/[title].txtGET /page/[title].htmlGET /page/[title].atom

PUT /page/[title]DELETE /page/[title]

Page 27: Introduction To ASP.NET MVC

GET

The GET method means retrieve whatever information (in the form of

an entity) is identified by the Request-URI.

Safe & Idempotent

Page 28: Introduction To ASP.NET MVC

GET /Accept: text/html, */*

303 See OtherLocation: http://localhost/page.htmlCache-Control: publicExpires: Thu, 31 Jan 2008 16:00:00 GMT

Page 29: Introduction To ASP.NET MVC

GET /page.htmlAccept: text/html, */*

200 OKContent-Type: text/htmlCache-Control: publicExpires: Thu, 31 Jan 2008 16:00:00 GMT

<html><head><title>…</title></head><body><form

action="/page" method="post"enctype="application/x-www-form-urlencoded"><input type="text" name="title" maxlength="255" /><textarea name="body" rows="25" cols="80"></textarea><input type="submit" value="Create Page" />

</form></body></html>

Page 30: Introduction To ASP.NET MVC

POST

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

Page 31: Introduction To ASP.NET MVC

POST /pageContent-Type: application/x-www-form-urlencoded

title=Welcome&body=Welcome+to+my+new+wiki.

201 CreatedContent-Type: text/htmlContent-Location: http://localhost/page/WelcomeCache-Control: no-cache

<html><head>

<title>…</title><meta http-equiv="refresh" content="0;url=http://localhost:64701/page/Welcome">

</head><body>…</body></html>

Page 32: Introduction To ASP.NET MVC

GET /page/WelcomeAccept: text/html, */*

303 See OtherLocation: http://localhost/page/Welcome.htmlCache-Control: publicLast-Modified: Tue, 29 Jan 2008 16:00:00 GMTVary: Accept

Page 33: Introduction To ASP.NET MVC

GET /page/Welcome.htmlAccept: text/html, */*

200 OKContent-Type: text/htmlContent-Location: http://localhost/page/WelcomeCache-Control: publicLast-Modified: Tue, 29 Jan 2008 16:00:00 GMT

<html><head>

<title>…</title><link

href="http://localhost/page/Welcome.atom"rel="alternate" title="…"type="application/atom+xml" />

</head><body>…</body></html>

Page 34: Introduction To ASP.NET MVC

GET /page/Welcome.atomAccept: application/atom+xml

200 OKContent-Type: application/atom+xmlContent-Location: http://localhost/page/WelcomeCache-Control: publicLast-Modified: Tue, 29 Jan 2008 16:00:00 GMT

<?xml version="1.0" encoding="utf-8”?><feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom">…<link href="http://localhost:64701/page/Welcome" rel="source" /><link href="http://localhost:64701/page/Welcome.atom" rel="self"

type="application/atom+xml" hreflang="en" title="…" /><link href="http://localhost:64701/page/Welcome.html"

rel="alternate" type="text/html" hreflang="en" title="…" /><link href="http://localhost:64701/page/Welcome.txt"

rel="alternate" type="text/plain" hreflang="en" title="…" /><entry>…</entry></feed>

Page 35: Introduction To ASP.NET MVC

GET /page/WelcomeAccept: text/plain

303 See OtherLocation: http://localhost/page/Welcome.txtCache-Control: publicLast-Modified: Tue, 29 Jan 2008 16:00:00 GMTVary: Accept

GET /page/Welcome.txtAccept: text/plain

200 OKContent-Type: text/plainContent-Location: http://localhost/page/WelcomeCache-Control: publicLast-Modified: Tue, 29 Jan 2008 16:00:00 GMT

Welcome to my new wiki.

Page 36: Introduction To ASP.NET MVC

PUT

The PUT method requests that the enclosed entity be stored under the

supplied Request-URI.

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.

Idempotent

Page 37: Introduction To ASP.NET MVC

PUT /page/WelcomeContent-Type: text/plain

Welcome to my new [[wiki]].

204 No ContentCache-Control: no-cache

Page 38: Introduction To ASP.NET MVC

DELETE

The DELETE method requests that the origin server delete the resource

identified by the Request-URI.

Idempotent

Page 39: Introduction To ASP.NET MVC

DELETE /page/Welcome

204 No ContentCache-Control: no-cache

Page 40: Introduction To ASP.NET MVC
Page 41: Introduction To ASP.NET MVC

REpresentational State Transfer

Page 42: Introduction To ASP.NET MVC

REST: The Web Used Correctly

A system or application architecture

… that uses HTTP, URI and other Webstandards “correctly”

… is “on” the Web, not tunnelled through it

Page 43: Introduction To ASP.NET MVC

REST is an Architectural Style

Defines a set of key “constraints”

… that, if met, make an architecture “RESTful”

… with the Web as one example

Page 44: Introduction To ASP.NET MVC

Equate “REST” with “RESTful HTTP usage”

Stefan Tilkov

Page 45: Introduction To ASP.NET MVC

Deriving REST

Client-ServerStateless

CacheUniform interfaceLayered system

Code on Demand

Page 46: Introduction To ASP.NET MVC

“The central feature that distinguishes the REST architectural style from other network-based styles is its emphasis on a uniform interface

between components.”

Roy Fielding

Page 47: Introduction To ASP.NET MVC

Uniform Interface

Uniform resource identification

A set of well-defined operations for manipulation

A shared set of media-types

Hypertext as the engine of application state

Page 48: Introduction To ASP.NET MVC

Benefits of REST

Hypertext is standardizedfewer UIs

Identification is standardizedless communication

Exchange protocols are standardizedfewer integrations

Interactions are standardizedfewer semantics

Data formats are standardizedfewer translations

Page 49: Introduction To ASP.NET MVC

“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.”

Tim Bray (XML Co-inventor)

Page 50: Introduction To ASP.NET MVC

“If you’re ready for REST I suggest you jump on board right away and get

ahead of the curveYou’ll have to train your developers in

REST principles.You definitely need to provide guidance

to your people.What you want to do is work to the point

where REST becomes the default for all your distributed applications.”

Anne Thomas Manes (Burton Group)

Page 51: Introduction To ASP.NET MVC
Page 52: Introduction To ASP.NET MVC

Linkshttp://del.icio.us/alan.dean/mvc

http://thoughtpad.net/alan-dean

[email protected]

© MMVIII