SlideShare a Scribd company logo
1 of 26
Download to read offline
ASP.NET MVC: Full Throttle

        Andrea Saltarello
        C.T.O.
        http://blogs.ugidotnet.org/pape
        http://twitter.com/andysal74
        http://slideshare.net/andysal
        Managed Designs s.r.l.

http://creativecommons.org/licenses/by-nc-nd/2.5/
Sponsor
Sponsor Platinum




 Sponsor Gold
Agenda
•   #ifdef NIUBBO MVC 1-2-3
•   Controller->View
•   View->Controller
•   AspNetMvc.More();
Model View Controller
Formulato nel 1979 da Trygve Reenskaug, membro
  del team Smalltalk @ Xerox PARC

La prima motivazione di design è:
Separare gli aspetti di presentazione da
quelli di dominio.

E' la “radice concettuale” di tutti gli attuali pattern:
Model View Presenter, Presenter-First, Passive View,
   Presentation Abstraction Control, Presentation
   Model Model View ViewModel...
Model view controller

                                    Model
    State                                                 State
   Query             Change                               Change
                     Notification


                                    View Selection

            View                                     Controller
                                    User Gestures


Method Invocations

Events
MVC: falsi miti

• Lo scopo del Controller non è di
  separare la View dal Model.

• La responsabilità del Controller è di fare
  da mediatore tra l'utente e l'applicazione,
  non tra la View e il Model.

• Spesso si parla di MVC, ma si intende
  Model 2
Model 2
• In a Model 2 application, requests from the client
  browser are passed to the controller, which is a
  servlet. The controller decides which view (JSP) it
  will pass the request to. The view then invokes
  methods in a JavaBean (which may access a
  database) and returns the Response object to the
  Web container, which is then passed on to the client
  browser. [Wikipedia]
• Legenda:
• Servlet->HttpHandler->Front Controller [P of EAA,
  344]
• JSP->Controller->Page Controller [P of EAA, 333]
Introduzione a ASP.NET MVC (1/4)
http://host/webapp/Customer/Detail/1
Introduzione a ASP.NET MVC (2/4)
http://host/webapp/Customer/Detail/1
Introduzione a ASP.NET MVC (3/4)
http://host/webapp/Customer/Detail/1
Introduzione a ASP.NET MVC (4/4)
http://host/webapp/Customer/Detail/1
Navigation Flow
Le action restituiscono un valore di tipo
  ActionResult. “Pragmapolimorficamente”
  parlando:
•  ViewResult. Restituito dal metodo View.
•  PartialViewResult. Restituito dal metodo PartialView.
•  ContentResult. Restituito dal metodo Content.
•  JsonResult. Restituito dal metodo Json.
•  EmptyResult. Restituito dalle action che vogliano restituire “null”.
•  YourOwnPersonalResult (semi-cit ), restituito da una factory ad hoc
•  RedirectToRouteResult. Restituito dai metodi RedirectToAction e
   RedirectToRoute.
• RedirectResult. Restituito dal metodo Redirect.
In MVC 3, i metodi Redirect sono disponibili anche nel gusto «Permanent»
View: i gusti
MVC utilizza view engine per
renderizzare delle view basate su
paradigmi differenti. Out of the box:
• Ye Olde WebFormViewEngine
• Un Razor nuovo di pacca [MVC3]
ASP.Net MVC
Dati: Controller->View

Per trasferire dati dal controller alla view:
• ViewData
• YourView.Model = viewModel
• ViewBag.YourProperty = yourValue [MVC3]
ASP.Net MVC
Dati: View->Controller
Per trasferire dati dalla view al
 controller:
• Parametri della action (convention over
  configuration):
  – ogni elemento in query string è mappato su un
    parametro omonimo
  – ogni elemento del <form /> è mappato su un
    parametro omonimo
• FormCollection

• >>>Model binding<<<
ASP.Net MVC
Data Entry, a.k.a. «Gioca Jouer»

• Mostrare!
• Validare!
• Jquery!
Data Entry: Mostrare
Built in:
• Usare i metodi helper per definire
  “campi” e «label»
• [Opzionale]: EditorTemplates,
  DisplayTemplates
Data Entry: Validare!
• Taggare il ViewModel mediante
  DataAnnotations, es:
  – RegularExpression
  – Required
  – StringLength
  – Compare [MVC3]
  – Remote [MVC3]
• Posizionare
  Html.ValidationMessageFor<>() e/o
  Html.ValidationSummary()
• [Opzionale] Impostare .input-validation-
  error nel css
Data Entry: JQuery!
• Client side validation
   – Inclusione degli script JS
   – <% Html.EnableClientSideValidation();
     %>
• JQuery plug-in, es:
   – JQuery UI (tab, datepicker, …)
   – JQGrid
• Assert.IsTrueLove(EditorTemplates,
  JQuery)
ASP.Net MVC
AspNetMvc.More();
Esempi:
• [FX4 only]<%: … %> (equivalente a <% =Html.Encode(…)
  %>)
• IoC
• Custom ActionResult:
   – Generare Thumbnail
   – Generare Atom/RSS
     (http://www.ugidotnet.org/Article/Detail/280)
• “Componentization”: [Render]Partial()+[Render]Action()
• Test
• Aree
Demo code: NSK

Progetto open source (licenza CPL)
disponibile su CodePlex:
http://nsk.codeplex.com
ASP.NET MVC: Full Throttle

More Related Content

Similar to ASP.NET MVC: Full Throttle

Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo MicrosoftAsp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo MicrosoftStefano Benedetti
 
Web Api – The HTTP Way
Web Api – The HTTP WayWeb Api – The HTTP Way
Web Api – The HTTP WayLuca Milan
 
ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%DomusDotNet
 
Asp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community TourAsp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community TourAndrea Balducci
 
Hands on MVC - Mastering the Web
Hands on MVC - Mastering the WebHands on MVC - Mastering the Web
Hands on MVC - Mastering the WebClaudio Gandelli
 
Migliora il tuo codice con knockout.js
Migliora il tuo codice con knockout.jsMigliora il tuo codice con knockout.js
Migliora il tuo codice con knockout.jsAndrea Dottor
 
Design pattern architetturali Model View Controller, MVP e MVVM
Design pattern architetturali   Model View Controller, MVP e MVVMDesign pattern architetturali   Model View Controller, MVP e MVVM
Design pattern architetturali Model View Controller, MVP e MVVMRiccardo Cardin
 
Cert03 70-486 developing asp.net mvc 4 web applications
Cert03   70-486 developing asp.net mvc 4 web applicationsCert03   70-486 developing asp.net mvc 4 web applications
Cert03 70-486 developing asp.net mvc 4 web applicationsDotNetCampus
 
Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010Fabrizio Bernabei
 
ASP.NET MVC 3: se non ora, quando?
ASP.NET MVC 3: se non ora, quando?ASP.NET MVC 3: se non ora, quando?
ASP.NET MVC 3: se non ora, quando?Giorgio Di Nardo
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0XeDotNet
 
Niccolò Becchi: Introduzione a GWT
Niccolò Becchi: Introduzione a GWTNiccolò Becchi: Introduzione a GWT
Niccolò Becchi: Introduzione a GWTfirenze-gtug
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)Giorgio Di Nardo
 
Model View Controller - Semplificare Il Codice E Minimizzare I Tempi
Model View Controller - Semplificare Il Codice E Minimizzare I TempiModel View Controller - Semplificare Il Codice E Minimizzare I Tempi
Model View Controller - Semplificare Il Codice E Minimizzare I TempiMarco Parenzan
 
Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)Fabio Armani
 

Similar to ASP.NET MVC: Full Throttle (20)

Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo MicrosoftAsp.Net MVC 3 - Il Model View Controller secondo Microsoft
Asp.Net MVC 3 - Il Model View Controller secondo Microsoft
 
ASP.NET MVC
ASP.NET MVCASP.NET MVC
ASP.NET MVC
 
Web Api – The HTTP Way
Web Api – The HTTP WayWeb Api – The HTTP Way
Web Api – The HTTP Way
 
ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%ASP.NET MVC: sfruttare la piattaforma al 100%
ASP.NET MVC: sfruttare la piattaforma al 100%
 
Asp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community TourAsp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community Tour
 
Hands on MVC - Mastering the Web
Hands on MVC - Mastering the WebHands on MVC - Mastering the Web
Hands on MVC - Mastering the Web
 
Migliora il tuo codice con knockout.js
Migliora il tuo codice con knockout.jsMigliora il tuo codice con knockout.js
Migliora il tuo codice con knockout.js
 
Design pattern architetturali Model View Controller, MVP e MVVM
Design pattern architetturali   Model View Controller, MVP e MVVMDesign pattern architetturali   Model View Controller, MVP e MVVM
Design pattern architetturali Model View Controller, MVP e MVVM
 
Cert03 70-486 developing asp.net mvc 4 web applications
Cert03   70-486 developing asp.net mvc 4 web applicationsCert03   70-486 developing asp.net mvc 4 web applications
Cert03 70-486 developing asp.net mvc 4 web applications
 
Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010
 
ASP.NET MVC 3: se non ora, quando?
ASP.NET MVC 3: se non ora, quando?ASP.NET MVC 3: se non ora, quando?
ASP.NET MVC 3: se non ora, quando?
 
Knockout.js
Knockout.jsKnockout.js
Knockout.js
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0
 
Powerful asp.net 4 e ie9
Powerful asp.net 4 e ie9Powerful asp.net 4 e ie9
Powerful asp.net 4 e ie9
 
Niccolò Becchi: Introduzione a GWT
Niccolò Becchi: Introduzione a GWTNiccolò Becchi: Introduzione a GWT
Niccolò Becchi: Introduzione a GWT
 
Novità di Asp.Net 4.0
Novità di Asp.Net 4.0Novità di Asp.Net 4.0
Novità di Asp.Net 4.0
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)
 
Model View Controller - Semplificare Il Codice E Minimizzare I Tempi
Model View Controller - Semplificare Il Codice E Minimizzare I TempiModel View Controller - Semplificare Il Codice E Minimizzare I Tempi
Model View Controller - Semplificare Il Codice E Minimizzare I Tempi
 
Spring - Ecosistema
Spring - EcosistemaSpring - Ecosistema
Spring - Ecosistema
 
Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)
 

More from Andrea Saltarello

Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open SourceDa Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open SourceAndrea Saltarello
 
The Fine Art of Time Travelling: implementing Event Sourcing
The Fine Art of Time Travelling: implementing Event SourcingThe Fine Art of Time Travelling: implementing Event Sourcing
The Fine Art of Time Travelling: implementing Event SourcingAndrea Saltarello
 
Implementing Event Sourcing in .NET
Implementing Event Sourcing in .NETImplementing Event Sourcing in .NET
Implementing Event Sourcing in .NETAndrea Saltarello
 
Idiomatic Domain Driven Design: implementing CQRS
Idiomatic Domain Driven Design: implementing CQRSIdiomatic Domain Driven Design: implementing CQRS
Idiomatic Domain Driven Design: implementing CQRSAndrea Saltarello
 
Architecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionArchitecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionAndrea Saltarello
 
Never Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven DesignNever Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven DesignAndrea Saltarello
 
Layered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRSLayered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRSAndrea Saltarello
 
How I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven DesignHow I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven DesignAndrea Saltarello
 
Code Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled RepositoryCode Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled RepositoryAndrea Saltarello
 
Idiomatic Domain Driven Design
Idiomatic Domain Driven DesignIdiomatic Domain Driven Design
Idiomatic Domain Driven DesignAndrea Saltarello
 
Build a LINQ-enabled Repository
Build a LINQ-enabled RepositoryBuild a LINQ-enabled Repository
Build a LINQ-enabled RepositoryAndrea Saltarello
 
Layered Expression Trees: una terza via (idiomatica) verso il DDD
Layered Expression Trees: una terza via (idiomatica) verso il DDDLayered Expression Trees: una terza via (idiomatica) verso il DDD
Layered Expression Trees: una terza via (idiomatica) verso il DDDAndrea Saltarello
 
From relational data to object spaces
From relational data to object spacesFrom relational data to object spaces
From relational data to object spacesAndrea Saltarello
 

More from Andrea Saltarello (14)

Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open SourceDa Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
 
The Fine Art of Time Travelling: implementing Event Sourcing
The Fine Art of Time Travelling: implementing Event SourcingThe Fine Art of Time Travelling: implementing Event Sourcing
The Fine Art of Time Travelling: implementing Event Sourcing
 
ASP.NET Core essentials
ASP.NET Core essentialsASP.NET Core essentials
ASP.NET Core essentials
 
Implementing Event Sourcing in .NET
Implementing Event Sourcing in .NETImplementing Event Sourcing in .NET
Implementing Event Sourcing in .NET
 
Idiomatic Domain Driven Design: implementing CQRS
Idiomatic Domain Driven Design: implementing CQRSIdiomatic Domain Driven Design: implementing CQRS
Idiomatic Domain Driven Design: implementing CQRS
 
Architecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionArchitecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC Solution
 
Never Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven DesignNever Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven Design
 
Layered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRSLayered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRS
 
How I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven DesignHow I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven Design
 
Code Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled RepositoryCode Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled Repository
 
Idiomatic Domain Driven Design
Idiomatic Domain Driven DesignIdiomatic Domain Driven Design
Idiomatic Domain Driven Design
 
Build a LINQ-enabled Repository
Build a LINQ-enabled RepositoryBuild a LINQ-enabled Repository
Build a LINQ-enabled Repository
 
Layered Expression Trees: una terza via (idiomatica) verso il DDD
Layered Expression Trees: una terza via (idiomatica) verso il DDDLayered Expression Trees: una terza via (idiomatica) verso il DDD
Layered Expression Trees: una terza via (idiomatica) verso il DDD
 
From relational data to object spaces
From relational data to object spacesFrom relational data to object spaces
From relational data to object spaces
 

ASP.NET MVC: Full Throttle

  • 1. ASP.NET MVC: Full Throttle Andrea Saltarello C.T.O. http://blogs.ugidotnet.org/pape http://twitter.com/andysal74 http://slideshare.net/andysal Managed Designs s.r.l. http://creativecommons.org/licenses/by-nc-nd/2.5/
  • 3. Agenda • #ifdef NIUBBO MVC 1-2-3 • Controller->View • View->Controller • AspNetMvc.More();
  • 4. Model View Controller Formulato nel 1979 da Trygve Reenskaug, membro del team Smalltalk @ Xerox PARC La prima motivazione di design è: Separare gli aspetti di presentazione da quelli di dominio. E' la “radice concettuale” di tutti gli attuali pattern: Model View Presenter, Presenter-First, Passive View, Presentation Abstraction Control, Presentation Model Model View ViewModel...
  • 5. Model view controller Model State State Query Change Change Notification View Selection View Controller User Gestures Method Invocations Events
  • 6. MVC: falsi miti • Lo scopo del Controller non è di separare la View dal Model. • La responsabilità del Controller è di fare da mediatore tra l'utente e l'applicazione, non tra la View e il Model. • Spesso si parla di MVC, ma si intende Model 2
  • 7. Model 2 • In a Model 2 application, requests from the client browser are passed to the controller, which is a servlet. The controller decides which view (JSP) it will pass the request to. The view then invokes methods in a JavaBean (which may access a database) and returns the Response object to the Web container, which is then passed on to the client browser. [Wikipedia] • Legenda: • Servlet->HttpHandler->Front Controller [P of EAA, 344] • JSP->Controller->Page Controller [P of EAA, 333]
  • 8. Introduzione a ASP.NET MVC (1/4) http://host/webapp/Customer/Detail/1
  • 9. Introduzione a ASP.NET MVC (2/4) http://host/webapp/Customer/Detail/1
  • 10. Introduzione a ASP.NET MVC (3/4) http://host/webapp/Customer/Detail/1
  • 11. Introduzione a ASP.NET MVC (4/4) http://host/webapp/Customer/Detail/1
  • 12. Navigation Flow Le action restituiscono un valore di tipo ActionResult. “Pragmapolimorficamente” parlando: • ViewResult. Restituito dal metodo View. • PartialViewResult. Restituito dal metodo PartialView. • ContentResult. Restituito dal metodo Content. • JsonResult. Restituito dal metodo Json. • EmptyResult. Restituito dalle action che vogliano restituire “null”. • YourOwnPersonalResult (semi-cit ), restituito da una factory ad hoc • RedirectToRouteResult. Restituito dai metodi RedirectToAction e RedirectToRoute. • RedirectResult. Restituito dal metodo Redirect. In MVC 3, i metodi Redirect sono disponibili anche nel gusto «Permanent»
  • 13. View: i gusti MVC utilizza view engine per renderizzare delle view basate su paradigmi differenti. Out of the box: • Ye Olde WebFormViewEngine • Un Razor nuovo di pacca [MVC3]
  • 15. Dati: Controller->View Per trasferire dati dal controller alla view: • ViewData • YourView.Model = viewModel • ViewBag.YourProperty = yourValue [MVC3]
  • 17. Dati: View->Controller Per trasferire dati dalla view al controller: • Parametri della action (convention over configuration): – ogni elemento in query string è mappato su un parametro omonimo – ogni elemento del <form /> è mappato su un parametro omonimo • FormCollection • >>>Model binding<<<
  • 19. Data Entry, a.k.a. «Gioca Jouer» • Mostrare! • Validare! • Jquery!
  • 20. Data Entry: Mostrare Built in: • Usare i metodi helper per definire “campi” e «label» • [Opzionale]: EditorTemplates, DisplayTemplates
  • 21. Data Entry: Validare! • Taggare il ViewModel mediante DataAnnotations, es: – RegularExpression – Required – StringLength – Compare [MVC3] – Remote [MVC3] • Posizionare Html.ValidationMessageFor<>() e/o Html.ValidationSummary() • [Opzionale] Impostare .input-validation- error nel css
  • 22. Data Entry: JQuery! • Client side validation – Inclusione degli script JS – <% Html.EnableClientSideValidation(); %> • JQuery plug-in, es: – JQuery UI (tab, datepicker, …) – JQGrid • Assert.IsTrueLove(EditorTemplates, JQuery)
  • 24. AspNetMvc.More(); Esempi: • [FX4 only]<%: … %> (equivalente a <% =Html.Encode(…) %>) • IoC • Custom ActionResult: – Generare Thumbnail – Generare Atom/RSS (http://www.ugidotnet.org/Article/Detail/280) • “Componentization”: [Render]Partial()+[Render]Action() • Test • Aree
  • 25. Demo code: NSK Progetto open source (licenza CPL) disponibile su CodePlex: http://nsk.codeplex.com