MVC CodeCamp Presentation


Published on

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

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide MVC CodeCamp Presentation

  1. 1. MVC Owen Evans Developer, Xero
  2. 2. or
  3. 3. Welcome to the new (old) way <ul><li>MVC – Model-View-Controller </li></ul><ul><ul><li>About architectural separation </li></ul></ul><ul><ul><li>Not a new pattern, first attributed to Trygve Reenskaug, working in smalltalk at Xerox Parc </li></ul></ul><ul><ul><li>Convention over configuration </li></ul></ul><ul><ul><li>Less to think about (no page object model) </li></ul></ul><ul><ul><li>Easier to unit test </li></ul></ul>
  4. 4. Lots of MVC Frameworks <ul><li>Some well known: </li></ul><ul><ul><li>Rails, Merb, Monorail, Grails, Spring MVC Framework, Dojo, Django, Silverstripe, Flex, Swing, </li></ul></ul><ul><li>Others not so </li></ul><ul><ul><li>Check Wikipedia for links to lots of frameworks </li></ul></ul>
  5. 5. The MVC pattern Controller View Model
  6. 6. MVC vs. Classic <ul><li> Classic </li></ul><ul><li> MVC </li></ul>
  7. 7. Model <ul><li>The computer model. The representation of the system within your application domain </li></ul><ul><li>Business objects, services, data access etc </li></ul><ul><li>The application core </li></ul>
  8. 8. Controllers <ul><li>Direct all the action of a request </li></ul><ul><li>Make calls to models to gather data </li></ul><ul><li>Sends business object and information to a particular view. </li></ul><ul><li>Makes decisions for security, UI, redirects etc. </li></ul><ul><li>Provides the glue between the mental model and the computer model </li></ul>
  9. 9. A Basic Controller <ul><li>public class HomeController : Controller </li></ul><ul><li>{ </li></ul><ul><li>public ActionResult Index() </li></ul><ul><li>{ </li></ul><ul><li> var message = someService.GetAMessage() </li></ul><ul><li>return View(message); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Interacts with the model and gets a message </li></ul><ul><li>Puts the message to the default view </li></ul><ul><li>Only one action/view for this controller </li></ul>
  10. 10. View <ul><li>Responsible for displaying a given expectation of data. </li></ul><ul><li>Will interact with model but shouldn’t make decisions over what entities to display, shouldn’t make CRUD actions etc. </li></ul><ul><li>Purely about representing the mental model of the system. </li></ul><ul><li>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 </li></ul>
  11. 11. A Basic View <ul><li><%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Index.aspx.cs&quot; Inherits=&quot;Basic_MVC_Project.Views.Home.Index&quot; %> </li></ul><ul><li><asp:Content ID=&quot;indexContent&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;> </li></ul><ul><li><h2><%= Html.Encode(ViewData[&quot;Message&quot;]) %></h2> </li></ul><ul><li><p> </li></ul><ul><li>To learn more about ASP.NET MVC visit <a href=&quot;; title=&quot;ASP.NET MVC Website&quot;></a>. </li></ul><ul><li></p> </li></ul><ul><li></asp:Content> </li></ul><ul><li>Relies on a master page </li></ul><ul><li>Just displays the data in the ViewData store </li></ul>
  12. 12. Putting the R in MVC <ul><li>Routing is part of the major power of MVC </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>Controllers are usually grouped around conceptual objects within the domain </li></ul></ul><ul><ul><ul><li>Tasks, Posts, Products </li></ul></ul></ul>
  13. 13. The ActionResult <ul><li>The key to actions is ActionResult which has many implementations </li></ul><ul><ul><li>ViewResult: Renders the specified view to the response. </li></ul></ul><ul><ul><li>EmptyResult: Does nothing. Returned if the action method must return a null result. </li></ul></ul><ul><ul><li>RedirectResult: Performs an HTTP redirect to the specified URL. </li></ul></ul><ul><ul><li>RedirectToRouteResult: Given some routing values, uses the routing API to determine the URL and then redirects to that URL. </li></ul></ul><ul><ul><li>JsonResult: Serializes the specified ViewData object to JSON format. </li></ul></ul><ul><ul><li>ContentResult: Writes the specified text content to the response. </li></ul></ul>
  14. 14. ActionFilter <ul><li>Attribute based interception of action calls </li></ul><ul><li>Can hook into calls: </li></ul><ul><ul><li>OnActionExecuted </li></ul></ul><ul><ul><li>OnActionExecuting </li></ul></ul><ul><ul><li>OnResultExecuted </li></ul></ul><ul><ul><li>OnResultExecuting </li></ul></ul><ul><li>Useful for logging, security, caching etc. </li></ul>
  15. 15. Extras <ul><li>Ajax made easy </li></ul><ul><ul><li>You can just return a Json result and JavaScript can eval the response to get the result. </li></ul></ul><ul><li>REST made easy </li></ul><ul><ul><li>Routing makes creating rest web services just a case of routing to the correct action </li></ul></ul>
  16. 16. Caveats <ul><li>This talk was based on Preview 4, Preview 5 was released on Friday and has a couple of minor changes </li></ul><ul><li>Preview code still, API’s are subject to change. </li></ul><ul><li>Still a lot of community work to do to create helpers, utilities etc.. </li></ul>
  17. 17. Questions? <ul><li>Ask Scott Hanselman……. </li></ul><ul><li>Contact me: </li></ul><ul><ul><li>, </li></ul></ul><ul><ul><li>, </li></ul></ul><ul><ul><li> </li></ul></ul>
  18. 18. References <ul><li> MVC Official Site </li></ul><ul><li>MVC Xerox Parc </li></ul><ul><li>Scott Gu, Phil Haack, Scott Hanselman, Rob Conery </li></ul><ul><li>MVC Contrib Project </li></ul>