• Save
ASP.NET MVC - Whats The Big Deal
Upcoming SlideShare
Loading in...5
×
 

ASP.NET MVC - Whats The Big Deal

on

  • 5,701 views

 

Statistics

Views

Total Views
5,701
Views on SlideShare
5,664
Embed Views
37

Actions

Likes
5
Downloads
0
Comments
0

3 Embeds 37

http://www.slideshare.net 29
http://geojohn.blogspot.com 6
http://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

ASP.NET MVC - Whats The Big Deal ASP.NET MVC - Whats The Big Deal Presentation Transcript

  • Venketash (Pat) Ramadass Systems Architect & Managing Director emediaIT pat.ramadass@emediait.com http://patramadass.emediait.com
  • Separation of Concerns  MVC  ASP.NET MVC  Demonstrations of building simple ASP.NET  MVC functionality Questions 
  • Presentation logic, business logic and data  access logic are mixed together too often  Business logic code within the User Interface  Unmanagable code, goes against good OO principles Proper Separation of Concerns (SoC) solves  this by de-coupling the overall architecture
  • Stands for Model-View-Controller  It is a design pattern for software development  Designed to promote and enforce Separation  of Concerns  Model – Data Access and Business Logic  View – User Interface  Controller – Handles User Interaction and Model Manipulation
  • User User Interface Generates Events View(s) Controller(s) Provides Data Manipulates Model
  • MVC Framework which Microsoft is adding to  ASP.NET Alternative to ASP.NET, not a replacement  Can combine both in a single solution  Currently in public beta, but there are  production systems using it already
  • Clear separation of concerns (SoC)  Testability - support for TDD  Fine-grained control over HTML and  JavaScript Intuitive URLs (Routing) – SEO Friendly 
  • Functionality Webforms ASP.NET MVC URLs Page names and URL Routing Programmatically - Rewriting REST Event Driven Yes No Stateful Yes (Viewstate) No Easily Unit Tested No Yes Abstraction from HTML Yes No Exact control over HTML No Yes Supports ORM Yes Yes Easy use of Third Party UI Yes Partial (No state/postback controls controls only) Works on Linux/Mono Yes (Partial) Not yet supported, but can be made to work The above is not intended to be a complete list, it simply highlights some key differences.
  • 6 View Routing Controller 1 2 5 Rules 3 1. HTTP Request 2. ASP.NET MVC Framework evaluates URL against registered Routing Rules and forwards the request to a matching Controller Model Data Store 4 3. Controller calls Model to build ViewData, if required 4. The Model returns the requested Data to the Controller 5. The Controller selects a View and passes the Model to the View 6. View renders and returns HTTP Response
  • Creating a new MVC Project  Folder Structure  Sample application included in ASP.NET MVC  Application Template A URL does not equal a page  Understanding URL Routing 
  • URL: /Product/Details/3   This URL is parsed into three parts like this: ▪ Controller = ProductController ▪ Action = Details ▪ Id = 3  The request is therefore routed to the Details() action on the ProductController class, with a Id parameter of 3  Notice that the suffix Controller is tacked on to the end of the Controller parameter.
  • Default Route   Default Controller: HomeController  Default Action: Index  Default Id: Empty String URL: /Employee   This URL is parsed into three parts like this: ▪ Controller = EmployeeController ▪ Action = Index ▪ Id = “”  The request is therefore routed to the Index() action on the EmployeeController class
  • Default Route   Default Controller: HomeController  Default Action: Index  Default Id: Empty String URL: http://localhost, i.e. Without supplying a  further URL
  • In a blog application, you may want to handle  incoming requests that look like: /Archive/12- 25-2009. For this you would need a Custom Route. The order in which routes are added is  important
  • A controller should only contain the bare minimum of logic  required to return the right view or redirect the user to another action. A controller is just a Class, inherits from  System.Web.Mvc.Controller HomeController.cs has 2 methods called Index() and About(),  these are the actions it exposes. The URL /Home/Index invokes the HomeController.Index()  method and the URL /Home/About invokes the HomeController.About() method. Any public method in a controller is exposed as a controller  action.
  • A method used as a controller action cannot be overloaded or static  A controller action returns action result.  Six standard types of action results:   ViewResult – Represents HTML and markup.  EmptyResult – Represents no result.  RedirectResult – Represents a redirection to a new URL.  RedirectToRouteResult – Represents a redirection to a new controller action.  JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.  ContentResult – Represents a text result. ContentResult is useful for returning plain text such as dates, integers etc 
  • Instead of returning an action result directly, you  normally call one of the following methods on the Controller base class:  View – Returns a ViewResult action result.  Redirect – Returns a RedirectResult action result.  RedirectToAction – Returns a RedirectToRouteResult action result.  RedirectToRoute – Returns a RedirectToRouteResult action result.  Json – Returns a JsonResult action result.  Content – Returns a ContentResult action result.
  • Modifying ActionResult 
  • A view should contain only logic related to generating the user  interface. A view contains the HTML markup and content that is sent to the  browser. A view is the equivalent of a page. In general, to return a view for a controller action, you must create  a subfolder in the Views folder with the same name as your controller. Within the subfolder, you must create an .aspx file with the same name as the controller action. The HomeController.Index() action returns a view located at the  following path: ViewsHomeIndex.aspx The HomeController.About() action returns a view located at the  following path: ViewsHomeAbout.aspx
  • Adding a new View Page  Using HTML Helpers to Generate View  Content Using View Data to Pass Data to a View 
  • An MVC model contains all of your  application logic that is not contained in a view or a controller. This should be all of your business logic and data access logic Aim for fat models and skinny controllers 
  • Create a new Database  Create a new Model  Using LINQ in Controller Actions  Using the Repository Pattern 
  • Why TDD?  Testing the View returned by a Controller  Testing the View Data returned by a  Controller Testing the Action Result returned by a  Controller
  • ASP.NET 3.5 Installed  Visual Studio 2008  Microsoft ASP.NET MVC Installed   Download the current Beta
  • ASP.NET 3.5 Installed  Visual Studio 2008  Microsoft ASP.NET MVC Installed   Download the current Beta
  • More Involved Examples   Add/Edit/Delete and use of Server Controls ▪ Email me if you would like information on this Custom HTML Helpers   You can create your own Helper Classes and Extension Methods which return formatted HTML strings Unity Dependency Injection / IoC Container   Framework for Dependency Injection and Controller Factories  Stand alone version and bundled with Enterprise Library 4.0
  • Using Partials as Templates for formatting/rendering   For example a template for how a Movie is rendered  HTML.RenderPartial(string partialViewName, object Model) Action Filters   Attributes that can be applied to a Controller Action or entire Controller ▪ Authorization ▪ Action Result ▪ Exception ▪ Custom  For example for Logging/Tracing
  • Upgrading existing code will take a long time, better  for Greenfields projects For now, applications which require richer user  interfaces will require more work with MVC  Third party UI library vendors have started developing and releasing updated controls Some SEO issues, may require URL rewrite engine,  but can be done Not officially supported on pre Windows Server  2003/IIS 6, but can work
  • If you are developing a new system it is worth  evaluating the pros and cons, it is not a silver bullet however ASP.NET MVC is much more in line with what is  happening elsewhere in the industry, for example Ruby on Rails We feel that the Pros outweigh the Cons in  general and are implementing ASP.NET MVC in our new Web Applications wherever possible
  • http://www.asp.net/mvc/   Official Site http://weblogs.asp.net/scottgu   Scott Guthrie http://www.hanselman.com/   Scott Hanselman http://quickstarts.asp.net/previews/mvc   Videos, Examples, Blogs http://martinfowler.com/articles/injection.html   Repository/Dependency Injection Pattern http://www.pnpguidance.net/Category/Unity.aspx   Unity http://patramadass.emediait.com   This presentation, example code