This document discusses ASP.NET MVC, an open source web application framework that implements the model-view-controller pattern. It provides an overview of ASP.NET MVC, including its advantages over ASP.NET Web Forms such as more control over HTML, easier testing, and support for clean URLs. The document also covers best practices for ASP.NET MVC projects, including separating concerns between models, views, and controllers, using dependency injection, avoiding direct dependencies between components, and writing tests.
Web Developer atCalifornia State University, San BernardinoMS. Computer Science, MCP, and MCTSInterest Areas: TDD, Web Programming, Secure CodingUser group maniac, visiting many user groups in a monthBlog: http://www.msnetprogrammer.net/blog,Email: volkan@iedotnetug.orgTwitter: http://www.twitter.com/volkanuzunAbout Me
4.
Classic ASP (AnotherSlow Page!?).NET Framework (Hurraay!!)ASP.NET v1.0 (No more slow pages)ASP.NET v1.1ASP.NET v2.0ASP.NET Ajax ASP.NET 3.5Silverlight ?!?!ASP.NET MVCASP.NET WebForms 4.0….MS Web Developer History
5.
Easy to learnbut?Maintenance?Test?Debugging?Complexity?What was wrong with ASP?
6.
Desktop like programmingparadigmLot’s of out ofbox controlsNo more spaghetti code?ASP.NET Web Forms
7.
ViewState (supposed to help you but?)DRYCan’t control HTML(Power is nothing without control)Best Practices? Separation of Concerns? (I don’t have any concern)What is wrong with ASP.NET WebForms?
MVCDifferent people readingabout MVC in different places take different ideas from it and describe these as “MVC”.Martin Fowler, GUI Architectures Essay (July 2006)
10.
Model–View–Controller (MVC) is an architecturalpattern Formulated in 1979 by TrygveMikkjel Heyerdahl ReenskaugThe model is the domain-specific representation of the data upon which the application operatesThe controller receives input and initiates a response by making calls on model objects.The view renders the model into a form suitable for interactionWikipedia: MVC
No View State,No PostbackVery flexible, pluggable.Very clean URLs, rest friendly urls.Supports ASP.NET Providers: Membership Provider, Role Provider, Session, Cache etc…Open Source.Community Support.Supports Separation of Concerns.Plays well with othersWhy is it good?
19.
Complete control overhtml!!No View State, No Postback!!Paradigm change.Few components .No formal validation support.Any Disadvantage?
20.
WebForms are dead?MVCdoes not have as much support as WebForms?MVC guarantees better coding?A project is either MVC or WebForms?MVC is faster than WebForms?True / False
21.
Browser requests /Products/ListRouteis selectedController is activated (Products)Controller executes the action method (List)Action talks to Model, prepares the ViewDataAction renders ViewBut what is it?
22.
Located in Controllersfolder, You can change itderived from Controller base classThe class name should be controller name + “Controller”Action Methods does the workDelete AccountControllerMove Controller’s to a separate projectController
23.
The ambiguous partof ASP.NET MVCM is the only part of MVC that is not implemented in ASP.NET MVCView Model: How your data looks on the page, flat dataDomain Model: How your business data is, hierarchical dataAny ORM is fineMODEL
24.
Just renders thepage, no biz logic!!!No ViewState, no server controlsNO Codebehind, pls DON’TDefault ViewEngine is WebFormsCommunicates with Controller with ViewData DictionaryStrongly Typed ViewVIEW
Separate Domain Modeland View ModelUse an ORMUse Repository PatternDomain Model should only validate domain rulesPresentation layer should NOT care about persistence layerBest Practices on Model
27.
FALSEViewModel == DomainModelThemost annoying part of Presentation Model is the synchronization between Presentation Model and view…Martin Fowler, GUI Architectures Essay, July 2004
No Business LogicNoConcrete DependenciesSmall Action MethodsDon’t mix HttpVerbsAvoid magic stringsUse controller as a traffic flow managerUnit test your expectationsAll the public methods are web accessibleUse OutputCacheBest Practices on Controller
30.
Define Views inan enum:return View(viewNames.Index);Even Better use T4MVCView.ModelMVCContrib for RedirectToAction:return RedirectToAction<HomeController>(c=>c.Index)Removing Magics in Controller
31.
Refactor View Namesand Links with T4MVCCache the outputWrite simple unit testDEMO
Why doesHome Controller have to know about BookRepository()?
35.
What ifHomeController needs initialization param in the constructor?private IBookRepositorybookRepository;public HomeController(IBookRepository repo){bookRepository = repo;} But who calls the controller with a parameterized constructor? NOT MVC Framework, at least not the default ControllerFactoryBetter Injection
No Magic String(use T4MVC)Change ../ pathsStrongly Typed ViewRenderPartialHtmlHelpersUse HTML if you canIf there is an “If” , time to check HtmlHelpersEncode input stringBest Practices On View
Separate ControllersUse IoCNoBusiness Logic in ControllersNo Magic Strings in any componentUse HTML HelpersDRYTestStrongly Types ViewsList of Practices
41.
Client side validationwith JQueryTemplatingStrongly typed helpersAreas SupportAsync Controller ActionsNew Things With MVC 2
#18 Use Repository Pattern Separate the Model layer from Control layerMVC is in presentation layer, model is in persistence layerDon’t let your UI talk to business model directlyUse orm, orm is not for only if I change the db later…
#22 Use automapper, silverlight and wpf has binding support , and mvvm support