Jess Chadwick Website Manager Infragistics jesschadwickgmailcom The Genealogy of Awesomeness History of Microsoft Web Stuff Active Server Pages Active Server Pages ASP V1 circa 1996 Active Scripting Pages topdown script ID: 228657
Download Presentation The PPT/PDF document "the ASP.NET MVC Framework" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
the ASP.NET MVC Framework
Jess Chadwick
Website Manager, Infragistics
jesschadwick@gmail.comSlide2
The Genealogy of Awesomeness
History of Microsoft Web StuffSlide3
Active Server PagesSlide4
Active Server Pages (ASP)
V1 circa 1996
“Active Scripting Pages”: top-down script
Call into VB6/COM code to do “real work”
You might remember these oldies but goodies:
Request object
Response object
Session objectServer objectApplication objectObjectContext objectASPError objectSlide5
Classic ASP ExampleSlide6
ASP.NET
Released in 2002 as part of .NET 1.0
Not
“ASP
v.Next
” – completely new
Introduced a few important concepts:
Rich programming frameworkCode-BehindPage LifecycleThe omnipresent ViewState!Slide7
ASP.NET Page LifecycleSlide8
“ASP.NET Extensions”
Started with “ASP.NET Futures”
ASP.NET AJAX
Silverlight
Controls
ADO.NET Data Services
ASP.NET Dynamic Data
And…ASP.NET MVC!Slide9
What is ASP.NET MVC?
Microsoft’s ASP.NET implementation of the MVC software pattern
More control over your HTML and URLs
More easily testable framework
A new Web Project type for ASP.NET
An option / alternativeSlide10
Let’s whet the appetite!
DEMO: MVC Hello WorldSlide11
What’s the Point?
This is not “Web Forms
v.Next
”
All about alternatives
Flexibility
Extend it… or not
Create your own Controller- and ViewEngines, or use others such as Brail or NHamlFundamentalPart of System.Web namespaceFully supportedKISS & DRYSlide12
Driving Goals
Separation of Concerns
Easy testing & TDD
Highly-maintainable applications
Extensible and Pluggable
Plug in what you need
Build your own custom buildSlide13
Driving Goals (cont’d)
Clean URLs and HTML
SEO and REST friendly
Great interaction with ASP.NET
Handlers, Modules, Providers, etc. still work
.ASPX, .ASCX, .MASTER pages
Visual Studio ASP.NET Designer surfaceSlide14
Careful – there’s a grease spot over there…
Take a Look Under the HoodSlide15
Request FlowSlide16
The PatternSlide17
The Model
“The center of the universe” -
Todd Snyder
This represents your core business domain…
AKA – your “bread and butter”
Preferably independent of any specific technologySlide18
Views
Are for rendering/output.
Are usually pretty “stupid”
Web Forms as default
ViewEngine
.ASPX, .ASCX, .MASTER, etc.
Html Helpers for rendering markupCan replace with other view technologies:
Template engines (
NVelocity
, Brail, …).
Output formats (images, RSS, JSON, …).
Mock out for testing.
Can use loosely typed or strongly typed dataSlide19
Controllers
URLs route to actions on controllers, not pages
Controller executes logic, loads data (if any), and chooses view.
Can also redirect to other views & URLs
public
ActionResult
ShowPost
(
int
id) {
Post p =
PostRepository.GetPostById
(id);
if (p == null) {
return
RenderView
("
nosuchpost
", id);
} else {
return
RenderView
(“
showpost
", p);
}
}Slide20
Request FlowSlide21
The MVC Pattern in action
Browser makes a request
Route is determined
Controller is activated
Method on Controller is invoked
Controller does some stuff
Renders View, passing in
custom
ViewData
URLs are rendered,
pointing to other
ControllersSlide22
DEMO:
Northwind
SampleSlide23
Routing
Filters
Extensibility
View Engines
Controller Factories
Routing Handler
ASP.NET MVC FeaturesSlide24
URL Routing
Developers add Routes to a global
RouteTable
Mapping creates a
RouteData
- a bag of key/values
RouteTable.Routes.Add
(
new Route("blog/
bydate
/{year}/{month}/{day}",
new
MvcRouteHandler
()){
Defaults = new
RouteValueDictionary
{
{"controller", "blog"}, {"action", "show"}
},
Constraints = new
RouteValueDictionary
{
{"year", @"\d{1.4}"},
{"month", @"\d{1.2}"},
{"day", @"\d{1.2}"}}
})Slide25
“Can’t you just stop and ask for directions!?”
Demo: URL RoutingSlide26
URL Routing (cont’d)
Separate assembly, not closely tied/related to ASP.NET MVCSlide27
Filters
Add pre- and post-execute behaviors to your controller actions
Useful for logging, compression, etc.
public abstract class
ActionFilterAttribute
{
public void OnActionExecuting(ActionExecutingContext context) ;
public void
OnActionExecuted
(
ActionExecutingContext
context) ;
// New in Pre-Preview 3:
public void
OnResultExecuting
(
ResultExecutingContext
context);
public void
OnResultExecuted
(
ResultExecutedContext
context);
}Slide28
Extensibility
Views
Controllers
Models
Routes
…all PluggableSlide29
ViewEngineBase
View Engines render output
You get
WebForms
by default
Can implement your own
MVCContrib
has ones for Brail, NvelocityNHaml is an interesting one to watchView Engines can be used toOffer new DSLs to make HTML easierGenerate totally different mime/typesImages, RSS, JSON, XML, OFX, VCards, whatever.Slide30
ViewEngineBase Class
public abstract class
ViewEngineBase
{
public abstract void
RenderView
(ViewContext viewContext
);
}Slide31
Example View: Web Forms
<%@ 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
>Slide32
Example View: NHaml
%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" })Slide33
Now we can really
take control!
Demo: Filters and View EnginesSlide34
MVCContrib
Myriad UI helper extensions and classes
View Engines
NHaml
NVelocity
Brail
Xslt
Controller factories (for IoC)Castle (Windsor)Spring.NETNinject
StructureMap
Object Builder
Unity
Open Source project with extensions to base framework
On
CodePlex
at
http://www.codeplex.com/MVCContrib
Slide35
“What!? I can mock out HttpContext
!?”
TestabilitySlide36
Designed for Testability
Mockable
Intrinsics
HttpContextBase
,
HttpResponseBase
, HttpRequestBaseExtensibility IControllerIControllerFactoryIRouteHandlerViewEngineBaseSlide37
Testing Controller Actions
No requirement to test within ASP.NET runtime!
Use
RhinoMocks
,
TypeMock
,
Moq, etc.Create Test versions of the parts of the runtime you want to stub[
TestMethod
]
public void
ShowPostsDisplayPostView
() {
TestPostRepository
repository = new
TestPostRepository
();
TestViewEngine
viewEngine
= new
TestViewEngine
();
BlogController
controller = new
BlogController
(…);
controller.ShowPost
(2);
Assert.AreEqual
("
showpost",viewEngine.LastRequestedView
);
Assert.IsTrue
(
repository.GetPostByIdWasCalled
);
Assert.AreEqual
(2,
repository.LastRequestedPostId
);
}Slide38
“Wasn’t this supposed to come first?”
DEMO: Test-Driven DevelopmentSlide39
(I know what you’re thinking…)
Popular QuestionsSlide40
Why reinvent the wheel?
Aren’t there already existing frameworks out there, such as Monorail?Slide41
ASP.NET MVC & REST
Does MVC do REST?
Depends on your definition.
What about ADO.NET Data Extensions (Astoria) and/or WCF?Slide42
Controls & Components
Can still use server controls?
Can we still use Web Forms server controls?
Decent support for user controls
Still more/better support to comeSlide43
What about AJAX?
ASP.NET
AJAX?
Requires <form
runat
=“server”>
Roll
your ownJS frameworks like jQuery make this easierSlide44
Scalability, Performance, Security, Etc.
A layer of abstraction working over the solid, tested ASP.NET foundationSlide45
When’s It Gonna
Be Ready?Slide46
What’s the Point?
This is not “Web Forms
v.Next
”
All about alternatives
Flexibility
Extend it… or not
Create your own Controller- and ViewEngines, or use others such as Brail or NHamlFundamentalPart of System.Web namespaceFully supportedKISS & DRYSlide47
Q & A
(…and Thank You!)Slide48
Resources
The Bits
ASP.NET MVC Preview 2:
http://asp.net/MVC
ASP.NET MVC Pre-Preview3:
http://www.codeplex.com/aspnet
MVCContrib: http://www.codeplex.com/MVCContrib
Quickstart
http://quickstarts.asp.net/3-5-extensions/mvc/default.aspx
Videos
ASP.NET:
http://www.asp.net/learn/3.5-extensions-videos/
MIX:
http://sessions.visitmix.com
Community/Blogs
ASP.NET Forums:
http://forums.asp.net/1146.aspx
Scott Guthrie (
ScottGu
):
http://weblogs.asp.net/scottgu/
Scott
Hanselman
:
http://www.hanselman.com/blog/
Phil
Haack
:
http://haacked.com/
Sample Apps
MVC Samples:
http://www.codeplex.com/mvcsamples
CodeCampServer
:
http://codecampserver.org
Jess Chadwick
Web Lead
Infragistics, Inc
.
jesschadwick@gmail.com
http://blog.jesschadwick.com