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

Post on 17-Jan-2016

226 views 0 download

Tags:

transcript

2

ASP.NET MVC

Scott HanselmanSenior Program ManagerMicrosoft

3

Hello Cheesy

Scott HanselmanSenior Program ManagerMicrosoft

demo

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

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

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

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

8

3 CirclesModel

ControllerView

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

What is MVC?

Request

Controller

Step 1Incoming request directed to Controller

What is MVC?

ControllerModel

Step 2Controller processes request and forms a data Model

What is MVC?

Controller

View

Step 3Model is passed to View

What is MVC?

Controller

View

Step 4View transforms Model into appropriate output format

What is MVC?

Response

Controller

View

Step 5Response is rendered

15

Request Lifecycle

Scott HanselmanSenior Program ManagerMicrosoft

demo

16

Routing

Scott HanselmanSenior Program ManagerMicrosoft

demo

17

Extensibility

ViewsControllersModelsRoutes

…are all Pluggable

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.

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>

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

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

22

TDD

Scott HanselmanSenior Program ManagerMicrosoft

demo

23

Complete (ish) Application

Scott HanselmanSenior Program ManagerMicrosoft

demo

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

Choosing Between The Two

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

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

Is ASP.NET MVCReady for Release?

29

Q & A

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!

31

Resources for Developers

http://www.asp.net

http://www.silverlight.net

http://www.msdn.com

http://www.microsoft.com/teched

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

33

Please complete anevaluation

34

Be well,write good code,and stay in touch

scottha@microsoft.comhttp://hanselman.com

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.