DEV322 Visual Studio 2005 C - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

DEV322 Visual Studio 2005 C

Description:

Run the test, watch it fail. Write code to pass the test. Run the test, watch it succeed ... http://blog.wekeroad.com/ Microsoft. http://asp.net/mvc. Matthew D. ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 41
Provided by: joenal
Category:
Tags: blog | dev322 | fail | studio | visual

less

Transcript and Presenter's Notes

Title: DEV322 Visual Studio 2005 C


1
ASP.NET MVC
Matthew D. Groves
Student Life IT Web Developer
2
Brief history of MVC
  • First used in Smalltalk
  • Xerox PARC
  • 1979
  • Trygve Reenskaug
  • Struts (2000), Ruby on Rails (2005)
  • ASP.NET MVC
  • ASP.NET launched as CTP on December 10, 2007
  • Preview 2 launched March 5, 2008
  • Preview 3 launched May 1, 2008

3
Scope of this presentation
  • Introduction to ASP.NET MVC and syntax
  • Introduction to .NET syntax useful for MVC
  • Quick introduction to TDD
  • Not exhaustive
  • Not a detailed tutorial

4
Scope of this presentation
  • Visual Studio 2008
  • ASP.NET 3.5
  • C
  • MVC is not part of the .NET framework yet

5
MVC diagram
6
M is for Model
  • Components of the application responsible for
    maintaining state
  • State is persisted via a database
  • For instance, a Product object may represent a
    row in a Product table in a SQL DB

7
V is for View
  • Display / UI
  • List.aspx list of all products
  • Detail.aspx details of a product
  • New.aspx add a new product

8
C is for Controller
  • Handles user interaction / form submissions
  • Manipulates the model
  • Chooses a view

9
Why ASP.NET MVC?
  • True, clean separation
  • Components designed to be replaced / customized
  • Probably the big difference is that Rails will
    make it difficult for you if you don't think in
    MVC, while ASP.NET couldn't care less. This is
    something we will see a lot with Ruby on Rails
    the Rails architecture will intentionally make
    things difficult if you do not follow its
    conventions. Tom Rossi (themolehill.com)
  • Efficient, easy development
  • Maintainability
  • Test driven development (TDD)

10
Test Driven Development
  • Red/green testing
  • Write automated test script for some specific
    functionality
  • Run the test, watch it fail
  • Write code to pass the test
  • Run the test, watch it succeed
  • Repeat

11
MVC diagram
12
URL routing
  • URLs have a logical mapping
  • Friendly URLs
  • Distinct from MVC, but integral
  • /Products/Categories
  • Products controller
  • Categories action gt View of all categories
  • /Products/Detail/ProductID
  • Products controller
  • Detail action gt Details view of ProductID
  • http//website/Products/Detail/34

13
Example application
  • Visual Studio 2008
  • File-gtNew Project -gt ASP.NET MVC Web Application
  • Two projects
  • Application
  • Test project

14
MVC diagram
15
Products Controller
16
Products Controller Syntax
17
MVC diagram
18
Model
  • LINQ to SQL is an ORM
  • LINQ Language Integrated Query
  • ORM Object Relational Mapping

19
NorthwindDataContext
20
NorthwindDataContext
21
Syntax Break
  • Partial class
  • Class is split into multiple files
  • ListltTgt
  • Generic list of objects of type T
  • ListltCategorygt is a list of Category objects
  • Why? Functionality and helper methods

22
Syntax Break
  • Lambda expression
  • Concise, anonymous expressions
  • Code blocks written inline
  • Useful for LINQ expressions
  • foreach functionality

23
MVC diagram
24
Back to the controller
  • DataContext with some helper functions
  • Retrieve data needed for Categories view
  • Render the categories view with the
    ListltCategorygt as ViewData

25
ProductsController
26
MVC diagram
27
Recall the controller.
  • The controller is querying via the models
    helper function, GetCategories()
  • Passing off the data that needs to be rendered to
    the Categories view.

28
Views
  • Folder structure
  • /Shared
  • Master pages
  • User controls
  • Any view used for multiple controllers
  • /Products
  • Views for Products specifically
  • Standard CRUD goes here

29
Categories view
  • Categories.aspx.cs

Categories.aspx
30
Syntax Break
  • Views inherit from ViewPage class by default
  • ViewData is a bag of data
  • Strongly typed view
  • View inherits from ViewPageltTgt
  • Where T is the type of ViewData that is passed
  • Full intellisense

31
More on views
  • Can be coded inline, like standard ASP classic /
    PHP
  • Can be coded via code-behind pages with standard
    ASP.NET controls
  • Forms are just submitted to controllers for
    processing

32
Views Links and URLs
  • Hardcoded links
  • Html.ActionLink to avoid hardcoding URLs
  • Html.Form lambdas allow for strongly typed
    links/actions

33
Views Form field names
  • Use a naming convention on form fields
  • nameProduct.ProductName
  • nameProduct.ProductPrice
  • Doing so allows for easy data binding in the
    controller
  • Flexible, optional

34
Views Validation
  • Done in the controller
  • Typical example
  • New() controller directs to blank form view
  • Blank form view submits to Create() controller
  • Create() controller performs validation, creates
    error message
  • If valid, data is updated/inserted/deleted,
    redirect to list
  • If invalid, redirect back to the New() controller
    with a persisting context and error message

35
MVC diagram
36
Unit testing
  • ASP.NET MVC designed specifically to enable easy
    unit testing
  • Back to the test project when this application
    was first created

37
Test project
38
Syntax Break
  • TestClass
  • class full of test methods
  • TestMethod
  • Indicates methods that are run when running all
    tests
  • Should have descriptive names
  • Controller object instantiated
  • TestViewEngine provides a simulated browser
    environment (server variables, form, session,
    etc)
  • Controller action is performed
  • Assert(s) are used to verify action success

39
More information
  • ScottGu (Scott Guthrie)
  • http//weblogs.asp.net/scottgu
  • Youve been Haacked (Phil Haack)
  • http//haacked.com/
  • WekeRoad (Rob Conery)
  • http//blog.wekeroad.com/
  • Microsoft
  • http//asp.net/mvc

40
ASP.NET MVC
Matthew D. Groves
Student Life IT Web Developer
Write a Comment
User Comments (0)
About PowerShow.com