ASP.NET MVC -
5
http://Sharma-
NareshIT.blogspot.com
Design Patterns
Design patterns are solutions to software
design problems you find again and again in
real-world application development. Patterns
are about reusable designs and interactions of
objects..
The 23 Gang of Four (GoF) patterns are
generally considered the foundation for all other
patterns. They are categorized in three groups:
 Creational,
 Structural, and
 Behavioral
Creational Patterns
Abstract
Factory
Creates an instance of several families of classes
Builder Separates object construction from its representation
Factory
Method
Creates an instance of several derived classes
Prototype A fully initialized instance to be copied or cloned
Singleton A class of which only a single instance can exist
Structural Patterns
Adapter Match interfaces of different classes
Bridge Separates an object’s interface from its implementation
Composite A tree structure of simple and composite objects
Decorator Add responsibilities to objects dynamically
Facade A single class that represents an entire subsystem
Flyweight A fine-grained instance used for efficient sharing
Proxy An object representing another object
Behavioral Patterns
Chain of
Resp.
A way of passing a request between a chain of objects
Command Encapsulate a command request as an object
Interpreter A way to include language elements in a program
Iterator Sequentially access the elements of a collection
Mediator Defines simplified communication between classes
Memento Capture and restore an object's internal state
Observer A way of notifying change to a number of classes
State Alter an object's behavior when its state changes
Strategy Encapsulates an algorithm inside a class
Template
Method
Defer the exact steps of an algorithm to a subclass
Visitor Defines a new operation to a class without change
The Model-View-Controller (MVC) architectural pattern separates an
application into three main components: the model, the view, and the
controller.
The ASP.NET MVC framework provides an alternative to the ASP.NET Web
Forms pattern for creating Web applications.
The MVC Pattern
8
 Model–view–controller (MVC) is a software
architecture pattern
 Originally formulated in the late 1970s by
Trygve Reenskaug as part of the Smalltalk
 Code reusability and separation of concerns
 Originally developed for
desktop, then adapted
for internet applications
Model
9
 Set of classes that describes the data we are
working with as well as the business
 Rules for how the data can be
changed and manipulated
 May contain data validation rules
 Often encapsulate data stored in a database as
well as code used to manipulate the data
 Most likely a Data Access Layer of some kind
 Apart from giving the data objects, it doesn't have
significance in the framework
View
10
 Defines how the application’s user interface
(UI) will be displayed
 May support master views (layouts) and sub-
views (partial views or controls)
 Web: Template to dynamically generate HTML
Controller
11
 The core MVC component
 Process the requests with the help of views and
models
 A set of classes that handles
Communication from the user
Overall application flow
Application-specific logic
 Every controller has one or more "Actions"
MVC Steps
12
 Incoming request routed to Controller
For web: HTTP request
 Controller processes request and creates
presentation Model
Controller also selects appropriate result
(view)
 Model is passed to View
 View transforms Model into appropriate output
format (HTML)
 Response is rendered (HTTP Response)
13
The MVC Pattern for Web
14
MVC Pattern in ASP.NET MVC
ASP.NET MVC Request
15
Request Flow
Request View Controller Model
HTTP
Select
ViewHTML
Routing
Select Controller
MVC Frameworks
17
 CakePHP (PHP)
 CodeIgniter (PHP)
 Spring (Java)
 Perl: Catalyst, Dancer
 Python: Django, Flask, Grok
 Ruby: Ruby on Rails, Camping, Nitro, Sinatra
 JavaScript: AngularJS, JavaScriptMVC, Spine
 ASP.NET MVC (.NET Framework)
ASP.NET Web Forms
 Stable and mature, supported by heaps of
third party controls and tools
 Event driven web development
 Postbacks
 Viewstate
 Less control over the HTML
 Hard to test
 Rapid development
ASP.NET MVC
 Runs on top of ASP.NET
Not a replacement for WebForms
Leverage the benefits of ASP.NET
 Embrace the web
User/SEO friendly URLs, HTML 5, SPA
Adopt REST concepts
 Uses MVC pattern
Conventions and Guidance
Separation of concerns
19
ASP.NET MVC (2)
 Tight control over markup
 Testable
 Loosely coupled and extensible
 Convention over configuration
 Razor view engine
One of the greatest view engines
With intellisense, integrated in Visual Studio
 Reuse of current skills (C#, LINQ, HTML, etc.)
 Application-based (not scripts like PHP)
20
Separation of Concerns
 Each component has one responsibility
SRP – Single Responsibility Principle
DRY – Don’t Repeat Yourself
 More easily testable
TDD – Test-driven development
 Helps with concurrent development
Performing tasks concurrently
 One developer works on views
 Another works on controllers
21
Extensible
 Replace any component of the system
Interface-based architecture
 Almost anything can be replaced or extended
Model binders (request data to CLR objects)
Action/result filters (e.g. OnActionExecuting)
Custom action result types
View engine (Razor, WebForms, NHaml, Spark)
View helpers (HTML, AJAX, URL, etc.)
Custom data providers (ADO.NET), etc.
22
Clean URLs
 REST-like
/products/update
/blog/posts/2013/01/28/mvc-is-cool
 Friendlier to humans
/product.aspx?catId=123 or post.php?id=123
Becomes /products/chocolate/
 Friendlier to web crawlers
Search engine optimization (SEO)
23
The ASP.NET MVC History
24
Date Version
10 December 2007 ASP.NET MVC
13 March 2009 ASP.NET MVC 1.0
16 December 2009 ASP.NET MVC 2 RC
4 February 2010 ASP.NET MVC 2 RC 2
10 March 2010 ASP.NET MVC 2
6 October 2010 ASP.NET MVC 3 Beta
9 November 2010 ASP.NET MVC 3 RC
10 December 2010 ASP.NET MVC 3 RC 2
13 January 2011 ASP.NET MVC 3
20 September 2011
ASP.NET MVC 4 Developer
Preview
15 February 2012 ASP.NET MVC 4 Beta
31 May 2012 ASP.NET MVC 4 RC
15 August 2012 ASP.NET MVC 4
30 May 2013 ASP.NET MVC 4 4.0.30506.0
26 June 2013 ASP.NET MVC 5 Preview
23 August 2013 ASP.NET MVC 5 RC 1
17 October 2013 ASP.NET MVC 5
17 January 2014 ASP.NET MVC 5.1
10 February 2014 ASP.NET MVC 5.1.1
4 April 2014 ASP.NET MVC 5.1.2
22 June 2014 ASP.NET MVC 5.1.3
The Technologies
 Technologies that ASP.NET MVC uses
C# (OOP, Unit Testing, async, etc.)
HTML(5) and CSS
JavaScript (jQuery, KendoUI, etc.)
AJAX, Single-page apps
Databases (MS SQL)
ORM (Entity Framework and LINQ)
Web and HTTP
26
The Tools
 Tools that we need:
IDE: Visual Studio 2012 (Express for Web)
 JustCode and Web Essentals
Framework: .NET Framework 4.5
Web server: IIS 8 (Express)
Data: Microsoft SQL Sever (Express or LocalDB)
 Web Platform Installer 4.0 will install
everything we need for us
microsoft.com/web/downloads/platform.aspx
Install Visual Studio Express 2012 for Web
NuGet package management
 Free, open source package management
 Makes it easy to install and update open
source libraries and tools
 Part of Visual Studio 2012
 Configurable package sources
 Simple as adding a reference
 GUI-based package installer
 Package manager console
28

MVC - Introduction

  • 1.
  • 2.
    Design Patterns Design patternsare solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects..
  • 3.
    The 23 Gangof Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups:  Creational,  Structural, and  Behavioral
  • 4.
    Creational Patterns Abstract Factory Creates aninstance of several families of classes Builder Separates object construction from its representation Factory Method Creates an instance of several derived classes Prototype A fully initialized instance to be copied or cloned Singleton A class of which only a single instance can exist
  • 5.
    Structural Patterns Adapter Matchinterfaces of different classes Bridge Separates an object’s interface from its implementation Composite A tree structure of simple and composite objects Decorator Add responsibilities to objects dynamically Facade A single class that represents an entire subsystem Flyweight A fine-grained instance used for efficient sharing Proxy An object representing another object
  • 6.
    Behavioral Patterns Chain of Resp. Away of passing a request between a chain of objects Command Encapsulate a command request as an object Interpreter A way to include language elements in a program Iterator Sequentially access the elements of a collection Mediator Defines simplified communication between classes Memento Capture and restore an object's internal state Observer A way of notifying change to a number of classes State Alter an object's behavior when its state changes Strategy Encapsulates an algorithm inside a class Template Method Defer the exact steps of an algorithm to a subclass Visitor Defines a new operation to a class without change
  • 7.
    The Model-View-Controller (MVC)architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications.
  • 8.
    The MVC Pattern 8 Model–view–controller (MVC) is a software architecture pattern  Originally formulated in the late 1970s by Trygve Reenskaug as part of the Smalltalk  Code reusability and separation of concerns  Originally developed for desktop, then adapted for internet applications
  • 9.
    Model 9  Set ofclasses that describes the data we are working with as well as the business  Rules for how the data can be changed and manipulated  May contain data validation rules  Often encapsulate data stored in a database as well as code used to manipulate the data  Most likely a Data Access Layer of some kind  Apart from giving the data objects, it doesn't have significance in the framework
  • 10.
    View 10  Defines howthe application’s user interface (UI) will be displayed  May support master views (layouts) and sub- views (partial views or controls)  Web: Template to dynamically generate HTML
  • 11.
    Controller 11  The coreMVC component  Process the requests with the help of views and models  A set of classes that handles Communication from the user Overall application flow Application-specific logic  Every controller has one or more "Actions"
  • 12.
    MVC Steps 12  Incomingrequest routed to Controller For web: HTTP request  Controller processes request and creates presentation Model Controller also selects appropriate result (view)  Model is passed to View  View transforms Model into appropriate output format (HTML)  Response is rendered (HTTP Response)
  • 13.
  • 14.
    14 MVC Pattern inASP.NET MVC
  • 15.
  • 16.
    Request Flow Request ViewController Model HTTP Select ViewHTML Routing Select Controller
  • 17.
    MVC Frameworks 17  CakePHP(PHP)  CodeIgniter (PHP)  Spring (Java)  Perl: Catalyst, Dancer  Python: Django, Flask, Grok  Ruby: Ruby on Rails, Camping, Nitro, Sinatra  JavaScript: AngularJS, JavaScriptMVC, Spine  ASP.NET MVC (.NET Framework)
  • 18.
    ASP.NET Web Forms Stable and mature, supported by heaps of third party controls and tools  Event driven web development  Postbacks  Viewstate  Less control over the HTML  Hard to test  Rapid development
  • 19.
    ASP.NET MVC  Runson top of ASP.NET Not a replacement for WebForms Leverage the benefits of ASP.NET  Embrace the web User/SEO friendly URLs, HTML 5, SPA Adopt REST concepts  Uses MVC pattern Conventions and Guidance Separation of concerns 19
  • 20.
    ASP.NET MVC (2) Tight control over markup  Testable  Loosely coupled and extensible  Convention over configuration  Razor view engine One of the greatest view engines With intellisense, integrated in Visual Studio  Reuse of current skills (C#, LINQ, HTML, etc.)  Application-based (not scripts like PHP) 20
  • 21.
    Separation of Concerns Each component has one responsibility SRP – Single Responsibility Principle DRY – Don’t Repeat Yourself  More easily testable TDD – Test-driven development  Helps with concurrent development Performing tasks concurrently  One developer works on views  Another works on controllers 21
  • 22.
    Extensible  Replace anycomponent of the system Interface-based architecture  Almost anything can be replaced or extended Model binders (request data to CLR objects) Action/result filters (e.g. OnActionExecuting) Custom action result types View engine (Razor, WebForms, NHaml, Spark) View helpers (HTML, AJAX, URL, etc.) Custom data providers (ADO.NET), etc. 22
  • 23.
    Clean URLs  REST-like /products/update /blog/posts/2013/01/28/mvc-is-cool Friendlier to humans /product.aspx?catId=123 or post.php?id=123 Becomes /products/chocolate/  Friendlier to web crawlers Search engine optimization (SEO) 23
  • 24.
    The ASP.NET MVCHistory 24
  • 25.
    Date Version 10 December2007 ASP.NET MVC 13 March 2009 ASP.NET MVC 1.0 16 December 2009 ASP.NET MVC 2 RC 4 February 2010 ASP.NET MVC 2 RC 2 10 March 2010 ASP.NET MVC 2 6 October 2010 ASP.NET MVC 3 Beta 9 November 2010 ASP.NET MVC 3 RC 10 December 2010 ASP.NET MVC 3 RC 2 13 January 2011 ASP.NET MVC 3 20 September 2011 ASP.NET MVC 4 Developer Preview 15 February 2012 ASP.NET MVC 4 Beta 31 May 2012 ASP.NET MVC 4 RC 15 August 2012 ASP.NET MVC 4 30 May 2013 ASP.NET MVC 4 4.0.30506.0 26 June 2013 ASP.NET MVC 5 Preview 23 August 2013 ASP.NET MVC 5 RC 1 17 October 2013 ASP.NET MVC 5 17 January 2014 ASP.NET MVC 5.1 10 February 2014 ASP.NET MVC 5.1.1 4 April 2014 ASP.NET MVC 5.1.2 22 June 2014 ASP.NET MVC 5.1.3
  • 26.
    The Technologies  Technologiesthat ASP.NET MVC uses C# (OOP, Unit Testing, async, etc.) HTML(5) and CSS JavaScript (jQuery, KendoUI, etc.) AJAX, Single-page apps Databases (MS SQL) ORM (Entity Framework and LINQ) Web and HTTP 26
  • 27.
    The Tools  Toolsthat we need: IDE: Visual Studio 2012 (Express for Web)  JustCode and Web Essentals Framework: .NET Framework 4.5 Web server: IIS 8 (Express) Data: Microsoft SQL Sever (Express or LocalDB)  Web Platform Installer 4.0 will install everything we need for us microsoft.com/web/downloads/platform.aspx Install Visual Studio Express 2012 for Web
  • 28.
    NuGet package management Free, open source package management  Makes it easy to install and update open source libraries and tools  Part of Visual Studio 2012  Configurable package sources  Simple as adding a reference  GUI-based package installer  Package manager console 28