SlideShare uma empresa Scribd logo
1 de 26
ASP.NET MVC
  [TITLE]
Best Practices
WHO’S THIS GUY




• Malisa Ncube
• Software Engineer at Infectious Diseases Institute
  (Uganda)
• Blogger – http://geekswithblogs.net/malisancube
• Follow me on Twitter handle - @malisancube
• My email is dev@malisancube.com
• Leader of Uganda .NET Usergroup
                                                   #MSOpenDoor
• Urban Artist
• High sense of humor
AGENDA


•   ASP.NET MVC Best Practices
•   A bit of ASP.NET MVC 4 Preview
•   We’ll see
•   Conclusion
•   Q&A
What is ASP.NET MVC?
What is ASP.NET MVC?

  • Model: The model contains the core information for an
    application. This includes the data and validation rules
    as well as data access and aggregation logic.
  • View: The view encapsulates the presentation of the
    application, and in ASP.NET this is typically the HTML
    markup.
  • Controller: The controller contains the control-flow logic.
    It interacts with the Model and Views to control the flow
    of information and execution of the application
What is ASP.NET MVC? (Continued)

  • Opensource – Released under MSL for Pre release
    components. http://aspnet.codeplex.com
  • Proven: There are many sites that are using ASP.NET
    MVC framework and many resources.
  • Testable: Enables good software development
    practices.
Practices
1) Isolate your layers properly.

  • Use the ViewModel for transmitting data to the view.
    They should be simple POCO de-normalised objects.
  • Use the Domain Model Entities for persistence,
    validation, Behaviours and complex relationships .
    Mapping with ViewModel can be done with tools like
    Automapper.
1) Isolate your layers properly (Continued).

  • Use Controllers for selecting the view to be shown and
    not for business logic.
  • Use the view for displaying Html which will be rendered
    by the browser. Not for business logic.
  • Use Services/Repositories for manipulating business
    objects.




  Let’s see come code
2) Use the PRG (PostRedirectGet) pattern

  •   Prevent reposts to the form
  •   Issues an HTTP302 with temporary redirect
  •   Watch out for Json redirects. Check the type of requests.
  •   Use proper verbs [HttpPost], [HttpGet] on you controllers
3) Secure site from forgery


  • Confused deputy problem (A confused deputy is a
    computer program that is innocently fooled by some
    other party into misusing its authority)

  • Prevent (cross site request Forgery)CSRF/XSRF
  • Prevent (cross site request Forgery)CSRF/XSRF With
    Ajax
  • Use Anti-forgery helpers for form posts
    @Html.AntiForgeryToken and
    ValidateAntiForgeryTokenAttribute which peeks into
    Request.Form collection for antiforgery token
4) Make you application testable, maintainable and extensible

  • Use IoC to make your application testable
  • Have actual tests for different segments of your
    application.
  • You can scaffold the application using NuGet packages
    like MvcScaffold and include the repository and unit
    tests.




  Let’s see come code
5) Write clean code


  • Use Action Filters for crosscutting concerns.
  • - They help clean up your code by giving you a
    declarative approach to programming, similar to Aspect
    Oriented programming. More specifically Postsharp.
  • - Handling errors, Authorisation, Tracing.




  Let’s see come code
6) Use strongly typed views


  • You may decide to inherit behaviour of all views from a
    certain base class.
  • Avoid the ViewBag




  Let’s see come code
7) JSON endpoints



  • All JSON endpoints require [HttpPost] to prevent JSON
    hijacking
  • http://haacked.com/archive/2009/06/25/json-
    hijacking.aspx
  • - With [HttpPost], returning arrays is allowed.
8) Performance Tips

  • Test the application performance. (Fiddler, YSlow)
  • Optimise /compress your images
  • Minify your scripts and CSS




  Let’s see come code
9) Productivity Tips


  •   Use "Nuget" packages that help with productivity.
  •   ELMAH
  •   MvcScafolding
  •   Create you own nuget packages




  Let’s see come code
Tips
1) ASP.NET is still has the power of ASP.NET

  • You can extend using HttpModules, HttpHandlers
  • You can use HttpCaching




  Let’s see come code
2) Think about globalization from the beginning

  • Make you application support globalisation if its going to
    be on the internet.
  • Don’t forget to make accessibility
     – http://plugins.jquery.com/project/KeyTips
ASP.NET 4.0 Preview
ASP.NET 4.0 Preview



• PageInspector
• Nuget (Enabling MSBuild and Continuous Integration
  environments)
• Mobile phone support improvements
• HTML5 support
• Easier deployment + minification (Including cloud
  deployment)
• Asynchronous / Await
• Tooling (Page Inspector)
• Web Sockets
New Features in ASP.NET MVC 4 Developer Preview
New Features in ASP.NET MVC 4 Developer Preview
Q&A
REFERENCES




• http://www.asp.net/mvc
• http://www.asp.net/vnext/whats-new

• http://haacked.com (Phil Haack)
• http://blog.stevensanderson.com
• http://hanselman.com

• http://orchardproject.net

Mais conteúdo relacionado

Mais procurados

ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
eldorina
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
Khaled Musaied
 

Mais procurados (20)

ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
What's new in Visual Studio 2013 & TFS 2013
What's new in Visual Studio 2013 & TFS 2013What's new in Visual Studio 2013 & TFS 2013
What's new in Visual Studio 2013 & TFS 2013
 
Asp.net c# MVC-5 Training-Day-1 of Day-9
Asp.net c# MVC-5 Training-Day-1 of Day-9Asp.net c# MVC-5 Training-Day-1 of Day-9
Asp.net c# MVC-5 Training-Day-1 of Day-9
 
Develop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val KarpovDevelop a Basic REST API from Scratch Using TDD with Val Karpov
Develop a Basic REST API from Scratch Using TDD with Val Karpov
 
What's new in asp.net mvc 4
What's new in asp.net mvc 4What's new in asp.net mvc 4
What's new in asp.net mvc 4
 
ASP.Net MVC ile Web Uygulamaları -5(Logger)
ASP.Net MVC ile Web Uygulamaları -5(Logger)ASP.Net MVC ile Web Uygulamaları -5(Logger)
ASP.Net MVC ile Web Uygulamaları -5(Logger)
 
ASP.NET MVC overview
ASP.NET MVC overviewASP.NET MVC overview
ASP.NET MVC overview
 
ASP .NET MVC
ASP .NET MVC ASP .NET MVC
ASP .NET MVC
 
Asp.net MVC training session
Asp.net MVC training sessionAsp.net MVC training session
Asp.net MVC training session
 
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
Discuss About ASP.NET MVC 6 and ASP.NET MVC 5
 
Codeigniter Training Part3
Codeigniter Training Part3Codeigniter Training Part3
Codeigniter Training Part3
 
Full stack web development with c# and web assembly - Blazor.Net
Full stack web development with c# and web assembly - Blazor.NetFull stack web development with c# and web assembly - Blazor.Net
Full stack web development with c# and web assembly - Blazor.Net
 
ASP.NET MVC 3 in area of Javascript and Ajax improvement
ASP.NET MVC 3 in area of Javascript and Ajax improvementASP.NET MVC 3 in area of Javascript and Ajax improvement
ASP.NET MVC 3 in area of Javascript and Ajax improvement
 
Kentico and MVC
Kentico and MVCKentico and MVC
Kentico and MVC
 
Knockout Introduction
Knockout IntroductionKnockout Introduction
Knockout Introduction
 
Module2
Module2Module2
Module2
 
Angular
AngularAngular
Angular
 
TDD with ASP.NET MVC 1.0
TDD with ASP.NET MVC 1.0TDD with ASP.NET MVC 1.0
TDD with ASP.NET MVC 1.0
 
Whys and Hows of Automation
Whys and Hows of AutomationWhys and Hows of Automation
Whys and Hows of Automation
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 

Destaque (6)

Getting Started with ASP.NET MVC
Getting Started with ASP.NET MVCGetting Started with ASP.NET MVC
Getting Started with ASP.NET MVC
 
Asp.Net MVC Intro
Asp.Net MVC IntroAsp.Net MVC Intro
Asp.Net MVC Intro
 
Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performance
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 

Semelhante a ASP.NET MVC Best Practices malisa ncube

Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
Long Nguyen
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
divzi1913
 
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
tilejak773
 
DotNet 5.1Years Experience
DotNet 5.1Years ExperienceDotNet 5.1Years Experience
DotNet 5.1Years Experience
chandra sekhar
 

Semelhante a ASP.NET MVC Best Practices malisa ncube (20)

Asp 1a-aspnetmvc
Asp 1a-aspnetmvcAsp 1a-aspnetmvc
Asp 1a-aspnetmvc
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Asp.net,mvc
Asp.net,mvcAsp.net,mvc
Asp.net,mvc
 
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
4 Anguadasdfasdasdfasdfsdfasdfaslar (1).pptx
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
What is ASP.NET MVC
What is ASP.NET MVCWhat is ASP.NET MVC
What is ASP.NET MVC
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC  Cloud compiler - Minor Project by students of CBPGEC
Cloud compiler - Minor Project by students of CBPGEC
 
A Smooth Transition to HTML5 Using MVVM
A Smooth Transition to HTML5 Using MVVMA Smooth Transition to HTML5 Using MVVM
A Smooth Transition to HTML5 Using MVVM
 
Angularjs
AngularjsAngularjs
Angularjs
 
Asp.NETZERO - A Workshop Presentation by Citytech Software
Asp.NETZERO - A Workshop Presentation by Citytech SoftwareAsp.NETZERO - A Workshop Presentation by Citytech Software
Asp.NETZERO - A Workshop Presentation by Citytech Software
 
DotNet 5.1Years Experience
DotNet 5.1Years ExperienceDotNet 5.1Years Experience
DotNet 5.1Years Experience
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
Code igniter overview
Code igniter overviewCode igniter overview
Code igniter overview
 
Frameworks Galore: A Pragmatic Review
Frameworks Galore: A Pragmatic ReviewFrameworks Galore: A Pragmatic Review
Frameworks Galore: A Pragmatic Review
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

ASP.NET MVC Best Practices malisa ncube

  • 1. ASP.NET MVC [TITLE] Best Practices
  • 2. WHO’S THIS GUY • Malisa Ncube • Software Engineer at Infectious Diseases Institute (Uganda) • Blogger – http://geekswithblogs.net/malisancube • Follow me on Twitter handle - @malisancube • My email is dev@malisancube.com • Leader of Uganda .NET Usergroup #MSOpenDoor • Urban Artist • High sense of humor
  • 3. AGENDA • ASP.NET MVC Best Practices • A bit of ASP.NET MVC 4 Preview • We’ll see • Conclusion • Q&A
  • 5. What is ASP.NET MVC? • Model: The model contains the core information for an application. This includes the data and validation rules as well as data access and aggregation logic. • View: The view encapsulates the presentation of the application, and in ASP.NET this is typically the HTML markup. • Controller: The controller contains the control-flow logic. It interacts with the Model and Views to control the flow of information and execution of the application
  • 6. What is ASP.NET MVC? (Continued) • Opensource – Released under MSL for Pre release components. http://aspnet.codeplex.com • Proven: There are many sites that are using ASP.NET MVC framework and many resources. • Testable: Enables good software development practices.
  • 8. 1) Isolate your layers properly. • Use the ViewModel for transmitting data to the view. They should be simple POCO de-normalised objects. • Use the Domain Model Entities for persistence, validation, Behaviours and complex relationships . Mapping with ViewModel can be done with tools like Automapper.
  • 9. 1) Isolate your layers properly (Continued). • Use Controllers for selecting the view to be shown and not for business logic. • Use the view for displaying Html which will be rendered by the browser. Not for business logic. • Use Services/Repositories for manipulating business objects. Let’s see come code
  • 10. 2) Use the PRG (PostRedirectGet) pattern • Prevent reposts to the form • Issues an HTTP302 with temporary redirect • Watch out for Json redirects. Check the type of requests. • Use proper verbs [HttpPost], [HttpGet] on you controllers
  • 11. 3) Secure site from forgery • Confused deputy problem (A confused deputy is a computer program that is innocently fooled by some other party into misusing its authority) • Prevent (cross site request Forgery)CSRF/XSRF • Prevent (cross site request Forgery)CSRF/XSRF With Ajax • Use Anti-forgery helpers for form posts @Html.AntiForgeryToken and ValidateAntiForgeryTokenAttribute which peeks into Request.Form collection for antiforgery token
  • 12. 4) Make you application testable, maintainable and extensible • Use IoC to make your application testable • Have actual tests for different segments of your application. • You can scaffold the application using NuGet packages like MvcScaffold and include the repository and unit tests. Let’s see come code
  • 13. 5) Write clean code • Use Action Filters for crosscutting concerns. • - They help clean up your code by giving you a declarative approach to programming, similar to Aspect Oriented programming. More specifically Postsharp. • - Handling errors, Authorisation, Tracing. Let’s see come code
  • 14. 6) Use strongly typed views • You may decide to inherit behaviour of all views from a certain base class. • Avoid the ViewBag Let’s see come code
  • 15. 7) JSON endpoints • All JSON endpoints require [HttpPost] to prevent JSON hijacking • http://haacked.com/archive/2009/06/25/json- hijacking.aspx • - With [HttpPost], returning arrays is allowed.
  • 16. 8) Performance Tips • Test the application performance. (Fiddler, YSlow) • Optimise /compress your images • Minify your scripts and CSS Let’s see come code
  • 17. 9) Productivity Tips • Use "Nuget" packages that help with productivity. • ELMAH • MvcScafolding • Create you own nuget packages Let’s see come code
  • 18. Tips
  • 19. 1) ASP.NET is still has the power of ASP.NET • You can extend using HttpModules, HttpHandlers • You can use HttpCaching Let’s see come code
  • 20. 2) Think about globalization from the beginning • Make you application support globalisation if its going to be on the internet. • Don’t forget to make accessibility – http://plugins.jquery.com/project/KeyTips
  • 22. ASP.NET 4.0 Preview • PageInspector • Nuget (Enabling MSBuild and Continuous Integration environments) • Mobile phone support improvements • HTML5 support • Easier deployment + minification (Including cloud deployment) • Asynchronous / Await • Tooling (Page Inspector) • Web Sockets
  • 23. New Features in ASP.NET MVC 4 Developer Preview
  • 24. New Features in ASP.NET MVC 4 Developer Preview
  • 25. Q&A
  • 26. REFERENCES • http://www.asp.net/mvc • http://www.asp.net/vnext/whats-new • http://haacked.com (Phil Haack) • http://blog.stevensanderson.com • http://hanselman.com • http://orchardproject.net