2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

35

Transcript of 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

Page 1: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.
Page 2: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

2

ASP.NET MVC

Scott HanselmanSenior Program ManagerMicrosoft

Page 3: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

3

Hello Cheesy

Scott HanselmanSenior Program ManagerMicrosoft

demo

Page 4: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

4

What’s the Point?This is not Web Forms 4.0

It’s about alternatives. Car vs. Motorcycle.Flexible

Extend it. Or not.

FundamentalPart of System.Web and isn’t going anywhere.

Plays Well With OthersFeel free to use NHibernate for Models, Brail for Views and Whatever for Controllers.

Keep it simple and DRY

Page 5: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

5

Goodness – Driving Goals

Maintain Clean Separation of ConcernsEasy Testing Red/Green TDD Highly maintainable applications by default

Extensible and PluggableSupport replacing any component of the system

Page 6: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

6

Goodness – Driving Goals

Enable clean URLs and HTMLSEO and REST friendly URL structures

Great integration within ASP.NETAll the same providers still workMembership, Session, Caching, etc.ASP.NET Designer Surface in VS2008

Page 7: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

It’s still ASP.NET

Providers still workMembership, Caching, Session, etc.

Views leverage .aspx and .ascxBut they don’t have to if you don’t want them to

Within System.Web namespaceFeature Sharing

Page 8: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

8

3 CirclesModel

ControllerView

Page 9: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

9

A Little More DetailModel

ControllerView

•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

Page 10: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

What is MVC?

Request

Controller

Step 1Incoming request directed to Controller

Page 11: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

What is MVC?

ControllerModel

Step 2Controller processes request and forms a data Model

Page 12: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

What is MVC?

Controller

View

Step 3Model is passed to View

Page 13: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

What is MVC?

Controller

View

Step 4View transforms Model into appropriate output format

Page 14: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

What is MVC?

Response

Controller

View

Step 5Response is rendered

Page 15: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

15

Request Lifecycle

Scott HanselmanSenior Program ManagerMicrosoft

demo

Page 16: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

16

Routing

Scott HanselmanSenior Program ManagerMicrosoft

demo

Page 17: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

17

Extensibility

ViewsControllersModelsRoutes

…are all Pluggable

Page 18: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

18

ViewEnginesView Engines render outputYou get WebForms by defaultCan implement your own

MVCContrib has ones for Brail, NvelocityNHaml is an interesting one to watch

View Engines can be used toOffer new DSLs to make HTML easierGenerate totally different mime/types

Images, RSS, JSON, XML, OFX, VCards, whatever.

Page 19: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

19

Views<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %><asp:Content ContentPlaceHolderID="MainContentPlaceHolder" runat="server"> <h2><%= ViewData.CategoryName %></h2> <ul> <% foreach (var product in ViewData.Products) { %> <li> <%= product.ProductName %> <div class="editlink"> (<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>) </div> </li> <% } %> </ul> <%= Html.ActionLink("Add New Product", new { Action="New" }) %></asp:Content>

Page 20: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

20

NHaml – Extreme Views

%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" })

Page 21: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

21

Testing Controller Actions

No requirement to test within ASP.NET runtime.

Use Moq or RhinoMocks or TypeMockCreate Test versions of the parts of the runtime you want to stub

Page 22: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

22

TDD

Scott HanselmanSenior Program ManagerMicrosoft

demo

Page 23: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

23

Complete (ish) Application

Scott HanselmanSenior Program ManagerMicrosoft

demo

Page 24: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

24

What’s the Point?This is not Web Forms 4.0

It’s about alternatives. Car vs. Motorcycle.Flexible

Extend it. Or not.

FundamentalPart of System.Web and isn’t going anywhere.

Plays Well With OthersFeel free to use NHibernate for Models, Brail for Views and Whatever for Controllers.

Keep it simple and DRY

Page 25: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

Choosing Between The Two

Page 26: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

You Might be an MVC if…

You want full control over markupYou want a framework that enforces separation of concernsTDD/Unit Testing is a priority for youControl abstractions get in your way more than they helpYou like writing Javascript

With Apologies to Jeff Foxworthy

Page 27: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

You Might be a WebForm if…

You like programming against controls that encapsulate UI and logicYou like the WYSWIG designer and would rather avoid angle bracketsYou like keeping logic on the server rather than hand writing Javascript

With Apologies to Jeff Foxworthy

Page 28: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

Is ASP.NET MVCReady for Release?

Page 29: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

29

Q & A

Page 30: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

30

Resources

www.microsoft.com/teched Tech·Talks Tech·Ed BloggersLive Simulcasts Virtual Labs

http://microsoft.com/technet

Evaluation licenses, pre-released products, and MORE!

http://microsoft.com/msdn

Developer’s Kit, Licenses, and MORE!

Page 31: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

31

Resources for Developers

http://www.asp.net

http://www.silverlight.net

http://www.msdn.com

http://www.microsoft.com/teched

Page 32: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

32

ASP.NET Community – www.asp.netGreat Learning Content

219 Videos, 83 Tutorials, Starter Kits,Webcasts, Podcasts, suggested Books

Questions? Need an Answer (Forums)Over 132,000 new questions a year72% answered within 7-days

Stay up-to-date with BlogsDownload the latest alphas, betas, and releasesParticipate, Contribute, Summit, Learn

ASP.NET Wiki, Control Gallery

Page 33: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

33

Please complete anevaluation

Page 34: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

34

Be well,write good code,and stay in touch

[email protected]://hanselman.com

Page 35: 2 ASP.NET MVC Scott Hanselman Senior Program Manager Microsoft.

35

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