asp.net mvc best practices
DESCRIPTION
12 Best Practices that every developer working on ASP.NET MVC web applications must adopt. Taken from a talk I held in Rome. Read more about this at: http://codeclimber.net.nz/archive/2009/10/27/12-asp.net-mvc-best-practices.aspxTRANSCRIPT
![Page 1: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/1.jpg)
ASP.NET MVC Best Practices
Simone ChiarettaSolution Developer, Avanadehttp://codeclimber.net.nzTwitter: @simonech
21 Ottobre 2009
![Page 2: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/2.jpg)
Thanks to the Sponsors
![Page 3: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/3.jpg)
Who the hell am I?
► Simone Chiaretta► Work for Avanade Italy► Microsoft MVP ASP.NET► Blogger – http://codeclimber.net.nz ► Founder of UGIALT.NET► OpenSource developer► Climber► All Around Nice Guy
![Page 4: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/4.jpg)
Agenda
Would you like someone to tell you the final a movie before you watch it?
4
ASP.NET MVC Best Practices
![Page 5: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/5.jpg)
What ASP.NET MVC is?
► It’s an advanced session... You should already know
![Page 6: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/6.jpg)
Just in case
6
Model
View
Controller
1
5
2
4
3
Browser
The request hits the controller
The Controller asks the Model for data
The Model gives the data back to the Controller
The controller formats the data and passes them to the View
The view renders the HTML that needs to be sent to the
client
![Page 7: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/7.jpg)
Controller
![Page 8: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/8.jpg)
Delete “AccountController”Best Practice n° 1
![Page 9: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/9.jpg)
1 – Delete “AccountController”
► You will probably never use these account management pages
► Keeping demo code in a production application is EVIL
► Delete it
![Page 10: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/10.jpg)
Isolate controllers from the external WorldBest Practice n° 2
![Page 11: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/11.jpg)
2 - Isolate controllers from the outside World► HttpContext► Data Access classes► Configuration management► Logging► Clock► Etc…
![Page 12: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/12.jpg)
2 - Isolate controllers from the outside World► Not testable application► Not flexible application
![Page 13: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/13.jpg)
Use a IoC ContainerBest Practice n° 3
![Page 14: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/14.jpg)
What’s Dependency Injection
14
![Page 15: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/15.jpg)
What’s Dependency Injection
BAD
![Page 16: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/16.jpg)
What’s Dependency Injection
BETTER
![Page 17: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/17.jpg)
What’s Dependency Injection
BUT
![Page 18: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/18.jpg)
Inversion of Control
With IoC
![Page 19: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/19.jpg)
IoC inside ASP.NET MVC
► Extend ControllerFactory► Many ControllerFactory ready available
– StructureMap– Spring– Unity– Windsor– Ninject– ...
![Page 20: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/20.jpg)
IoC inside ASP.NET MVC using Ninject v2► Global.asax inherits from NinjectHttpApplication
► Helper to configure all controllers:– RegisterAllControllersIn(“assemblyName”);
![Page 21: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/21.jpg)
Don’t use “Magic strings”Best Practice n° 4
![Page 22: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/22.jpg)
Say NO to Magic Strings
► Never use ViewData[“key”]► Always create a ViewModel for each View► View must inherit from
– System.Web.Mvc.ViewPage<ListViewModel>
![Page 23: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/23.jpg)
Build your own “personal” conventionsBest Practice n° 5
![Page 24: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/24.jpg)
Build your own “personal” conventions
► ASP.NET MVC is the base on which to build your own reference architecture
► Controllers (and views) inherint from your own base class
![Page 25: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/25.jpg)
Pay attention to VerbsBest Practice n° 6
![Page 26: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/26.jpg)
Pay attention to Verbs
What happens when you refresh (or go back) after you submit a form?
26
![Page 27: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/27.jpg)
PRG Pattern
► View sends data in POST► Controller validates
– Renders the View with errors (POST)– Redirect in GET
► View renders the results in GET
![Page 28: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/28.jpg)
Pay attention to Verbs
► Show data in GET► Modify data in POST
![Page 29: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/29.jpg)
Model
![Page 30: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/30.jpg)
DomainModel != ViewModelBest Practice n° 7
![Page 31: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/31.jpg)
DomainModel != ViewModel
► DomainModel– Data + Behaviours– hierarchical, complex types
► ViewModel– Only Data– Flat, only strings
![Page 32: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/32.jpg)
DomainModel != ViewModel
► How to avoid getting bored writing tedious mapping code?
AutoMapperMapper.Map<Post, ShowPostModel>(post)
![Page 33: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/33.jpg)
Use ActionFilter for “shared” dataBest Practice n° 8
![Page 34: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/34.jpg)
Components in ASP.NET MVC
► RenderPartial– The Controller must “create” all data needed by all the partials
► RenderAction (futures)– Smells (view calls a controller)– More difficult to test
► Custom HtmlHelpers– Ok for some HTML, but must not have logic
![Page 35: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/35.jpg)
Action Filtes
► Defined as Attributi► Allow you to execute “code”
– During the Autenthorization phase– If an exception occurs– Before an Action– After an Action– Before the rendering of a view– After the rendering of a view
► “Core” filters– Authorize– OutputCache
![Page 36: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/36.jpg)
Action Filter + Render Partial
► Controller:– Has code for his “main concern” and “create” the main data
► View:– Renders the main output– Calls the various PartialViews
► Action Filters:– Load data for the partial views
► Partial views– Render data loaded via Action Filters
![Page 37: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/37.jpg)
View
![Page 38: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/38.jpg)
Do NOT use code-behindBest Practice n° 9
![Page 39: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/39.jpg)
Do NOT use code-behind
NEVER
![Page 40: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/40.jpg)
Write HTML when you canBest Practice n° 10
![Page 41: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/41.jpg)
Write HTML when you can
► You MUST learn HTML► Do never use HtmlHelpers that ONLY abstract HTML awat
<%= Html.Submit(“Salva”) %>vs
<input type=“submit” value=“Salva” />
![Page 42: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/42.jpg)
If there is an if, write an HtmlHelperBest Practice n° 11
![Page 43: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/43.jpg)
If there is an if, write an HtmlHelper► View must not have logic► Allowed: if - foreach► When possible, “hides” them in HtmlHelpers
![Page 44: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/44.jpg)
Choose your View Engine carefullyBest Practice n° 12
![Page 45: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/45.jpg)
Choose your View Engine carefully
► The default is WebFormViewEngine► Not the best available► Choose the one that most suits you
![Page 46: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/46.jpg)
Choose your View Engine carefully
► Spark View Engine– The flow is managed by HTML– It’s a templating engine
► Other Features– Renders PDF– Evaluates templates also with Javascript
![Page 47: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/47.jpg)
Beginning ASP.NET MVC
► Simone Chiaretta & Keyvan Nayyeri
► TOC:– MVC– Testing– And more...
http://bit.ly/BeginningASPNETMVC
![Page 48: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/48.jpg)
Contacts – Simone Chiaretta
► MSN: [email protected]► Blog:
– English: http://codeclimber.net.nz/– Italian: http://blogs.ugidotnet.org/piyo/
► Twitter: @simonech
48
![Page 49: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/49.jpg)
Credits
► These talk has been inspired by Sebastien Lambla (founder of Caffeine IT) and his ASP.NET MVC Best Practices
► Watch his talk (which is way better than mine): http://serialseb.blogspot.com/2009/05/my-mvc-best-practices-talk.html
► Read his blog: http://serialseb.blogspot.com/
49
![Page 50: ASP.NET MVC Best Practices](https://reader033.vdocuments.mx/reader033/viewer/2022061221/54bd88844a7959644c8b4590/html5/thumbnails/50.jpg)
Q&A
50