MVC provides a new web project type for ASP.NET that allows for more control over HTML and a more testable framework. It maintains a clean separation of concerns between models, views, and controllers and allows developers to easily extend or replace any component. MVC supports RESTful URLs, integrates well with existing ASP.NET features, and enables test-driven development through mockable abstractions.
13. This is not Web Forms 4.0
It’s about alternatives. Car vs. Motorcycle.
Flexible
Extend it. Or not.
Fundamental
Part of System.Web and isn’t going anywhere.
Plays Well With Others
Feel free to use NHibernate for Models, Brail
for Views and Whatever for Controllers.
Keep it simple and DRY
14. Maintain Clean Separation of Concerns
Easy Testing
Red/Green TDD
Highly maintainable applications by default
Extensible and Pluggable
Support replacing any component of the
system
15. Enable clean URLs and HTML
SEO and REST friendly URL structures
Great integration within ASP.NET
All the same providers still work
Membership, Session, Caching, etc.
ASP.NET Designer Surface in VS2008
29. •Browser requests /Products/
Model •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
View Controller Controllers
30. • You can futz at each step
Request
in the process
HTTP Http
Controller Response
Routing Handler
Route View
Route View
Handler Engine
33. Developers adds Routes to a global RouteTable
Mapping creates a RouteData - a bag of key/values
RouteTable.Routes.Add(
new Route(quot;blog/bydate/{year}/{month}/{day}quot;,
new MvcRouteHandler()){
Defaults = new RouteValueDictionary {
{quot;controllerquot;, quot;blogquot;}, {quot;actionquot;, quot;showquot;}
},
Constraints = new RouteValueDictionary {
{quot;yearquot;, @quot;d{1.4}quot;},
{quot;monthquot;, @quot;d{1.2}quot;},
{quot;dayquot;, @quot;d{1.2}quot;}}
})
36. View Engines render output
You get WebForms by default
Can implement your own
MVCContrib has ones for Brail, Nvelocity
NHaml is an interesting one to watch
View Engines can be used to
Offer new DSLs to make HTML easier
Generate totally different mime/types
Images, RSS, JSON, XML, OFX, VCards,
whatever.
42. No requirement to test within ASP.NET runtime.
Use RhinoMocks or TypeMock
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(quot;showpostquot;,viewEngine.LastRequestedView);
Assert.IsTrue(repository.GetPostByIdWasCalled);
Assert.AreEqual(2, repository.LastRequestedPostId);
}
43.
44.
45. This is not Web Forms 4.0
It’s about alternatives. Car vs. Motorcycle.
Flexible
Extend it. Or not.
Fundamental
Part of System.Web and isn’t going anywhere.
Plays Well With Others
Feel free to use NHibernate for Models, Brail
for Views and Whatever for Controllers.
Keep it simple and DRY
46.
47.
48.
49. HAI!
IM IN YR Northwind
HOW DUZ I ListProducts YR id
PRODUCTS = GETPRODUCTS id
OMG FOUND YR PRODUCTS
IF U SEZ
IM OUTTA YR Northwind
50. HAI!
WTF:
I HAS A STRING
GIMMEH STRING
IM IN YR VAR UPPIN YR 0
TIL LENGTH OF STRING
VISIBLE “GIMMEE A ” STRING!!VAR
STEPPIN
IM OUTTA YR VAR
OMGWTF
HALP!
54. Base Controller Class
Basic Functionality most folks will use
IController Interface
Ultimate Control for the Control Freak
IControllerFactory
For plugging in your own stuff (IOC, etc)
55. Scenarios, Goals and Design
URLs route to controller “actions”, not pages –
mark actions in Controller.
Controller executes logic, chooses view.
All public methods are accessible
public void ShowPost(int id) {
Post p = PostRepository.GetPostById(id);
if (p != null) {
RenderView(quot;showpostquot;, p);
} else {
RenderView(quot;nosuchpostquot;, id);
}
}
60. Scenarios, Goals and Design:
Are for rendering/output.
Pre-defined and extensible rendering helpers
Can use .ASPX, .ASCX, .MASTER, etc.
Can replace with other view
technologies:
Template engines (NVelocity, Brail, …).
Output formats (images, RSS, JSON, …).
Mock out for testing.
Controller sets data on the View
Loosely typed or strongly typed data
61. View Engines render output
You get WebForms by default
Can implement your own
MVCContrib has ones for Brail, Nvelocity
NHaml is an interesting one to watch
View Engines can be used to
Offer new DSLs to make HTML easier to write
Generate totally different mime/types
Images
RSS, JSON, XML, OFX, etc.
VCards, whatever.
66. Scenarios, Goals and Design:
Mock out views for testing
Replace ASPX with other technologies
public interface IViewEngine {
void RenderView(ViewContext context);
}
Inside controller class:
this.ViewEngine = new XmlViewEngine(...);
RenderView(quot;fooquot;, myData);