18
ASP.net MVC Owen Evans Developer, Xero

ASP.net MVC CodeCamp Presentation

Embed Size (px)

DESCRIPTION

Slides from Code Camp Auckland 2008, presentation on MS MVC framework

Citation preview

Page 1: ASP.net MVC CodeCamp Presentation

ASP.net MVC

Owen Evans

Developer, Xero

Page 2: ASP.net MVC CodeCamp Presentation

or

Page 3: ASP.net MVC CodeCamp Presentation

Welcome to the new (old) way

• MVC – Model-View-Controller– About architectural separation– Not a new pattern, first attributed to Trygve

Reenskaug, working in smalltalk at Xerox Parc

– Convention over configuration– Less to think about (no page object model)– Easier to unit test

Page 4: ASP.net MVC CodeCamp Presentation

Lots of MVC Frameworks

• Some well known:– Rails, Merb, Monorail, Grails, Spring MVC

Framework, Dojo, Django, Silverstripe, Flex, Swing,

• Others not so– Check Wikipedia for links to lots of

frameworks

Page 5: ASP.net MVC CodeCamp Presentation

The MVC pattern

Controller

View Model

Page 6: ASP.net MVC CodeCamp Presentation

MVC vs. Classic ASP.net

ASP.net Classic ASP.net MVC

Page 7: ASP.net MVC CodeCamp Presentation

Model

• The computer model. The representation of the system within your application domain

• Business objects, services, data access etc

• The application core

Page 8: ASP.net MVC CodeCamp Presentation

Controllers

• Direct all the action of a request

• Make calls to models to gather data

• Sends business object and information to a particular view.

• Makes decisions for security, UI, redirects etc.

• Provides the glue between the mental model and the computer model

Page 9: ASP.net MVC CodeCamp Presentation

A Basic Controllerpublic class HomeController : Controller

{

public ActionResult Index()

{

var message = someService.GetAMessage()

return View(message);

}

}

• Interacts with the model and gets a message• Puts the message to the default view• Only one action/view for this controller

Page 10: ASP.net MVC CodeCamp Presentation

View• Responsible for displaying a given expectation of data.• Will interact with model but shouldn’t make decisions

over what entities to display, shouldn’t make CRUD actions etc.

• Purely about representing the mental model of the system.

• Not restricted to aspx, can also use Nhaml, Brail, XSLT to name but a few, just implement a ViewEngine if you want your own view syntax

Page 11: ASP.net MVC CodeCamp Presentation

A Basic View<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"

AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Basic_MVC_Project.Views.Home.Index" %>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

<h2><%= Html.Encode(ViewData["Message"]) %></h2>

<p>

To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.

</p>

</asp:Content>

• Relies on a master page• Just displays the data in the ViewData store

Page 12: ASP.net MVC CodeCamp Presentation

Putting the R in MVC

• Routing is part of the major power of MVC– Many URLs can link to the same controller,

unlike code behinds which have a 1-1 link with .aspx pages, controllers can have multiple views

– Controllers are usually grouped around conceptual objects within the domain

• Tasks, Posts, Products

Page 13: ASP.net MVC CodeCamp Presentation

The ActionResult• The key to actions is ActionResult which has many implementations

– ViewResult: Renders the specified view to the response.– EmptyResult: Does nothing. Returned if the action method must return a null

result.– RedirectResult: Performs an HTTP redirect to the specified URL.– RedirectToRouteResult: Given some routing values, uses the routing API to

determine the URL and then redirects to that URL.– JsonResult: Serializes the specified ViewData object to JSON format.– ContentResult: Writes the specified text content to the response.

Page 14: ASP.net MVC CodeCamp Presentation

ActionFilter

• Attribute based interception of action calls

• Can hook into calls:– OnActionExecuted– OnActionExecuting– OnResultExecuted– OnResultExecuting

• Useful for logging, security, caching etc.

Page 15: ASP.net MVC CodeCamp Presentation

Extras

• Ajax made easy– You can just return a Json result and

JavaScript can eval the response to get the result.

• REST made easy– Routing makes creating rest web services just

a case of routing to the correct action

Page 16: ASP.net MVC CodeCamp Presentation

Caveats

• This talk was based on Preview 4, Preview 5 was released on Friday and has a couple of minor changes

• Preview code still, API’s are subject to change.

• Still a lot of community work to do to create helpers, utilities etc..

Page 17: ASP.net MVC CodeCamp Presentation

Questions?

• Ask Scott Hanselman…….

• Contact me:– http://www.bgeek.net, – [email protected], – http://www.twitter.com/buildmaster

Page 18: ASP.net MVC CodeCamp Presentation

References• ASP.net MVC Official Site

http://www.asp.net/mvc/

• MVC Xerox Parc http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html

• Scott Gu, Phil Haack, Scott Hanselman, Rob Conery http://weblogs.asp.net/scottgu/ http://haacked.com/Default.aspx

http://www.hanselman.com/ http://blog.wekeroad.com/

• MVC Contrib Project http://codeplex.com/MVCContrib