ASP.net MVC CodeCamp Presentation

Post on 13-May-2015

3.252 views 4 download

Tags:

description

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

Transcript of ASP.net MVC CodeCamp Presentation

ASP.net MVC

Owen Evans

Developer, Xero

or

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

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

The MVC pattern

Controller

View Model

MVC vs. Classic ASP.net

ASP.net Classic ASP.net MVC

Model

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

• Business objects, services, data access etc

• The application core

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

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

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

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

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

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.

ActionFilter

• Attribute based interception of action calls

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

• Useful for logging, security, caching etc.

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

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

Questions?

• Ask Scott Hanselman…….

• Contact me:– http://www.bgeek.net, – owen@bgeek.net, – http://www.twitter.com/buildmaster

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