ASP.NET MVC Best Practices Simone Chiaretta Solution Developer, Avanade 21 Ottobre 2009.

Slides:



Advertisements
Similar presentations
ASP.NET MVC v2 Cosa cè di nuovo Simone Chiaretta Solution Developer, Avanade 21 Ottobre 2009.
Advertisements

INTRODUCTION TO ASP.NET MVC AND EXAMPLE WALKTHROUGH RAJAT ARYA EFECS - OIM DAWG – 4/21/2009 ASP.NET MVC.
Fabian Vilers Hands on ASP.NET MVC.
Handling Security Threats in Kentico CMS Karol Jarkovsky Sr. Solution Architect Kentico Software
AMS304: Introduction to the ASP.NET Model View Controller (MVC) Framework Scott Hanselman Eilon Lipton Microsoft Microsoft
UNIT-V The MVC architecture and Struts Framework.
Ori Calvo, 2010 “If people want to have maximum reach across *all* devices then HTML will provide the broadest reach” Scott Guthrie,
LAYING OUT THE FOUNDATIONS. OUTLINE Analyze the project from a technical point of view Analyze and choose the architecture for your application Decide.
Entity Framework Code First End to End
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Presenter - Donn Felker.  Senior Consultant for Microsoft Gold Certified Partner- Statêra.  8 years of experience in developing and architecting enterprise.
Doing Something Useful with Enterprise Library 3.0 Benjamin Day Level: Intermediate.
ASP.NET Web API Udaiappa Ramachandran NHDN-Nashua.NET/Cloud Computing UG Lead Blog:
ASP.NET and Model View Control Jesper Tørresø ITNET2 F08.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Building Web Sites with ASP.NET MVC Framework Noam King CTO Sela College
ASP.NET Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital.
Introduction to ASP.NET MVC Information for this presentation was taken from Pluralsight Building Applications with ASP.NET MVC 4.
DaveAndAl.net Do Application Design Patterns Make Sense in ASP.NET? Alex Homer You may like to write these down now...
JavaScript – Quiz #9 Lecture Code:
Inversion Of Control & Dependency Injection Break Apart The Dependencies Oren Eini Senior Developer We! Consulting Group
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer Architecture.
Architectural Patterns Support Lecture. Software Architecture l Architecture is OVERLOADED System architecture Application architecture l Architecture.
JSF Framework Java Server Faces Presented by Songkran Totiya (6/10/2014)
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
ASP.NET MVC Architecture Layouts, Filters, Sections, Helpers, Partial Views, Areas… SoftUni Team Technical Trainers Software University
Getting Started with ASP.NET MVC BRIJ BHUSHAN MISHRA.
Getting started with ASP.NET MVC Dhananjay
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
ASP.Net MVC Extensibility, scalability and testability Andrew Locatelli Woodcock.
Securing Angular Apps Brian Noyes
MVC WITH CODEIGNITER Presented By Bhanu Priya.
TDD, DI, and SoC with ASP.NET MVC Presented by Jeffrey Palermo CTO, Headspring Systems Microsoft MVP, MCSD.Net
Introduction  “M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner.
Virtual techdays INDIA │ 9 th – 11 th January 2011 ASP.Net MVC Sundararajan S │ Associate Tech Architect, Aditi Technologies.
Working with Data Model Binders, Display Templates, Editor Templates, Validation… SoftUni Team Technical Trainers Software University
ASP.NET MVC An Introduction. What is MVC The Model-View-Controller (MVC) is an architectural pattern separates an application into three main components:
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
Virtual techdays INDIA │ 9-11 February 2011 SESSION TITLE Kamala Rajan S │ Technical Manager, Marlabs.
#SPSSAN June 30, 2012 San Diego Convention Center WRITING TESTABLE CODE In SharePoint.
Testing WebForms w/ Model-View-Presenter Erik Peterson.
Jean-Claude Trachsel Senior Consultant, Trivadis AG The good news.
Intro to MVC5 Bryan Soltis Bit-Wizards - Director of Technology & Research.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
The New Face of ASP.NET ASP.NET MVC, Razor, and jQuery Ido Flatow | Senior Architect | Sela | This session is.
Understanding Dependency Injection… and those pesky containers Miguel A. Castro Architect -
Bob German Principal Architect Developing SharePoint Applications with MVC and Entity Framework.
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
© Copyright SELA software & Education Labs Ltd Baruch Hirsch St.Bnei Brak Israel
Introduction to MVC Slavomír Moroz. Revision from Previous Lesson o ASP.NET WebForms applications Abstract away HTTP (similar to desktop app development)
Build Data Driven Apps with ASP.NET Core Rachel Appel.
Spice up Your Forms and Views
Web Technology Solutions
Jim Fawcett CSE686 – Internet Programming Spring 2014
DYNAMIC CONTENT DELIVERY
Getting Started with MVC 5 and Visual Studio 2013
Introduction to .NET Florin Olariu
An introduction to ASP.Net with MVC Nischal S
Social Media And Global Computing Introduction to The MVC Pattern
Building Web Applications with Microsoft ASP
MVC Partial View.
SQL Server Integration Services
Unit Testing & Test-Driven Development for Mere Mortals
Unit Testing & Test-Driven Development for Mere Mortals
Spice up Your Forms and Views
A Quick Overview of ASP.NET Core 1.0
ASP.NET Core* MVC and Web API Shahed Chowdhuri
Unit Testing & Test-Driven Development for Mere Mortals
ASP.NET and Model View Control
How to build a T-SQL Framework SQL Performance for Developers
Presentation transcript:

ASP.NET MVC Best Practices Simone Chiaretta Solution Developer, Avanade 21 Ottobre 2009

Thanks to the Sponsors

Who the hell am I? ► Simone Chiaretta ► Work for Avanade Italy ► Microsoft MVP ASP.NET ► Blogger – ► Founder of UGIALT.NET ► OpenSource developer ► Climber ► All Around Nice Guy

Agenda Would you like someone to tell you the final a movie before you watch it? 3 ASP.NET MVC Best Practices

What ASP.NET MVC is? ► It’s an advanced session... You should already know

Just in case 5 Model View Controller 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

Controller

Delete “AccountController” Best Practice n° 1

1 – Delete “AccountController” ► You will probably never use these account management pages ► Keeping demo code in a production application is EVIL ► Delete it

Isolate controllers from the external World Best Practice n° 2

2 - Isolate controllers from the outside World ► HttpContext ► Data Access classes ► Configuration management ► Logging ► Clock ► Etc…

2 - Isolate controllers from the outside World ► Not testable application ► Not flexible application

Use a IoC Container Best Practice n° 3

What’s Dependency Injection 13

What’s Dependency Injection BAD

What’s Dependency Injection BETTER

What’s Dependency Injection BUT

Inversion of Control With IoC

IoC inside ASP.NET MVC ► Extend ControllerFactory ► Many ControllerFactory ready available –StructureMap –Spring –Unity –Windsor –Ninject –...

IoC inside ASP.NET MVC using Ninject v2 ► Global.asax inherits from NinjectHttpApplication ► Helper to configure all controllers: –RegisterAllControllersIn(“assemblyName”);

Don’t use “Magic strings” Best Practice n° 4

Say NO to Magic Strings ► Never use ViewData[“key”] ► Always create a ViewModel for each View ► View must inherit from –System.Web.Mvc.ViewPage

Build your own “personal” conventions Best Practice n° 5

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

Pay attention to Verbs Best Practice n° 6

Pay attention to Verbs What happens when you refresh (or go back) after you submit a form? 25

PRG Pattern ► View sends data in POST ► Controller validates –Renders the View with errors (POST) –Redirect in GET ► View renders the results in GET

Pay attention to Verbs ► Show data in GET ► Modify data in POST

Model

DomainModel != ViewModel Best Practice n° 7

DomainModel != ViewModel ► DomainModel –Data + Behaviours –hierarchical, complex types ► ViewModel –Only Data –Flat, only strings

DomainModel != ViewModel ► How to avoid getting bored writing tedious mapping code? AutoMapper Mapper.Map (post)

Use ActionFilter for “shared” data Best Practice n° 8

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

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

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

View

Do NOT use code-behind Best Practice n° 9

Do NOT use code-behind NEVER

Write HTML when you can Best Practice n° 10

Write HTML when you can ► You MUST learn HTML ► Do never use HtmlHelpers that ONLY abstract HTML awat vs

If there is an if, write an HtmlHelper Best Practice n° 11

If there is an if, write an HtmlHelper ► View must not have logic ► Allowed: if - foreach ► When possible, “hides” them in HtmlHelpers

Choose your View Engine carefully Best Practice n° 12

Choose your View Engine carefully ► The default is WebFormViewEngine ► Not the best available ► Choose the one that most suits you

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

Beginning ASP.NET MVC ► Simone Chiaretta & Keyvan Nayyeri ► TOC: –MVC –Testing –And more...

Contacts – Simone Chiaretta ► MSN: ► Blog: –English: –Italian: ► Twitter: 47

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): best-practices-talk.html best-practices-talk.html ► Read his blog: 48

Q&A 49