E N D
1. Hattan Shobokshi ASP.NET MVC Best Practices
2. Housekeeping Stuttering is a communication disorder involving disruptions, or “disfluencies,” in a person’s speech.
Across all cultures, roughly 1% of people currently has a stuttering disorder.
http://westutter.org/
3. Who am I?
4. Goals for this talk Best Practices
Modular Design, Keep Controllers Thin, Service / Repository Pattern, Generic Repository, Unit Test, HTTP Verbs, Only modify data behind HTTP POST, PRG Pattern, No domain logic in the views, Avoid large code blocks in the view, Build Views, CDN, Anti Forgery Token, Model Binding to a List, Checkbox label click, Mobile Request
Tools
EF Code First, Ninject (IOC), MvcScaffolding, , T4MVC, Custom T4 Templates , Telerik Grid, ASP.NET sprite framework, Asynchronous File Upload, MVC 3 Charting, Remote Validator, , Blueprint CSS,
Diagnostics
MVC Diagnostics, RouteDebugger, ELMAH
5. What is ASP.NET MVC? Controller gets data from the model, Gives it to a view.
Controller doesn’t care where model gets data, and likewise controller doesn’t care how the view renders.
Each component only cares about itself. Easy to swap out.
Benefits :
Separation of Concerns
Testability
Easy to modify
Lack of Complexity
Flow:
Request comes through. Controller class handles the request, fetching data from the model. Controller then sends that data to the View and the result is given to the framework for renderingController gets data from the model, Gives it to a view.
Controller doesn’t care where model gets data, and likewise controller doesn’t care how the view renders.
Each component only cares about itself. Easy to swap out.
Benefits :
Separation of Concerns
Testability
Easy to modify
Lack of Complexity
Flow:
Request comes through. Controller class handles the request, fetching data from the model. Controller then sends that data to the View and the result is given to the framework for rendering
7. Keep Controllers Thin Remove Clutter
Business Logic Layer
Validation Layer
Small Action Methods
8. Modular Design Multiple Projects
Separation of Concerncs
Separate UI from Business Layer
Interchangeable Data Access Layer
10. Repository Pattern Encapsulate CRUD in a Repository
Business Logic / Validation in Service
Controller -> Repository
11. HTTP Verbs Always include HTTP Verbs
[HTTPGET]
[HTTPPOST]
Only Modify data in an HTTP Post
12. PRG Pattern POST
Redirect
GET
The PRG pattern is used to ensure that the intended modification of data only happens once.
13. Logging! ELMAHError Logging Modules and Handlers
Logging of nearly all unhandled exceptions.
Web page to remotely view the entire log of recoded exception
Email Notification
14. Minimize Code in Views No domain logic (data access) in the View
Avoid large code blocks in the View
Encapsulate into HTML Helper methods
Build Views
15. Microsoft Ajax Content Delivery Network Content Delivery Network
Improve Application Performance
Reuse Javascript files Cached by different domains
Available Content Files:
jQuery
jQuery Validation
jQuery UI
Ajax Control Toolkit
ASP.NET Ajax
ASP.NET MVC JavaScript Files
http://www.asp.net/ajaxlibrary/cdn.ashx<script src='http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.min.js' type='text/javascript'></script>
16. Remove Magic Strings
17. T4 : Text Template Transformation Toolkit Code Generation Tool
Built into Visual Studio (2008 and 2010)
.tt File Extension
No syntax highlighting by default
Clarius Visual T4 Add-on http://www.visualt4.com/
More informationhttp://www.olegsych.com/2007/12/text-template-transformation-toolkit/
18. T4MVC T4 template for ASP.NET MVC
Creates strongly typed helpers
Eliminates magic strings (View names, HtmlHelper parameters)
Download via NuGet orhttp://mvccontrib.codeplex.com/wikipage?title=T4MVC
http://blogs.msdn.com/b/davidebb/archive/2010/03/12/a-better-way-to-auto-run-t4mvc-when-you-build.aspx
19. T4MVC <%= Html.ActionLink(“New Dinner", “Create", "Dinners")%>
<%= Html.ActionLink(" New Dinner", MVC.Dinners.Create())%>
<img src="/Content/nerd.jpg" />
<img src="<%= Links.Content.nerd_jpg %>" />
http://mvccontrib.codeplex.com/wikipage?title=T4MVC_doc
20. T4 Code Templates Custom Tooling
Customize Default View Templates
Add new View Templates
Customize Controller Template
T4 Templates
~\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3
~\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\VisualBasic\Web\MVC 3 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3
<h2><#= mvcHost.ViewName #></h2>
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3
<h2><#= mvcHost.ViewName #></h2>
21. Telerik Grid Control http://demos.telerik.com/aspnet-mvc/
Grid Control
Out of the box support for Sorting, Filtering
Out of the box paging support
Out of the box ajax support
Very little javascript you have to write! ?
22. Route Debugger Enable route debugging with 1 line of codeRouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
23. MVC Diagnostics Environment Information
MVC Assembly Version
Referenced Assembly Information
Web Forms page, not an mvc page
http://bradwilson.typepad.com/blog/2010/03/diagnosing-aspnet-mvc-problems.html
24. What did we just talk about? Best Practices
Modular Design, Keep Controllers Thin, Service / Repository Pattern, Generic Repository, Unit Test, HTTP Verbs, Only modify data behind HTTP POST, PRG Pattern, No domain logic in the views, Avoid large code blocks in the view, Build Views, CDN, Anti Forgery Token, Model Binding to a List, Checkbox label click, Mobile Request
Tools
EF Code First, Ninject (IOC), MvcScaffolding, , T4MVC, Custom T4 Templates , Telerik Grid, ASP.NET sprite framework, Asynchronous File Upload, MVC 3 Charting, Remote Validator, , Blueprint CSS,
Diagnostics
MVC Diagnostics, RouteDebugger, ELMAH
25. Thank You!