2. Web Developer at California State University, San Bernardino MS. Computer Science, MCP, and MCTS Interest Areas: TDD, Web Programming, Secure Coding User group maniac, visiting many user groups in a month Blog: http://www.msnetprogrammer.net/blog, Email: volkan@iedotnetug.org Twitter: http://www.twitter.com/volkanuzun About Me
3.
4. Classic ASP (Another Slow Page!?) .NET Framework (Hurraay!!) ASP.NET v1.0 (No more slow pages) ASP.NET v1.1 ASP.NET v2.0 ASP.NET Ajax ASP.NET 3.5 Silverlight ?!?! ASP.NET MVC ASP.NET WebForms 4.0 …. MS Web Developer History
5. Easy to learn but? Maintenance? Test? Debugging? Complexity? What was wrong with ASP?
6. Desktop like programming paradigm Lot’s of out ofbox controls No more spaghetti code? ASP.NET Web Forms
7. ViewState (supposed to help you but?) DRY Can’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?
9. MVC Different people reading about 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 architectural pattern Formulated in 1979 by TrygveMikkjel Heyerdahl Reenskaug The model is the domain-specific representation of the data upon which the application operates The controller receives input and initiates a response by making calls on model objects. The view renders the model into a form suitable for interaction Wikipedia: MVC
18. No View State, No Postback Very 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 others Why is it good?
19. Complete control over html!! No View State, No Postback!! Paradigm change. Few components . No formal validation support. Any Disadvantage?
20. WebForms are dead? MVC does 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/List Route is selected Controller is activated (Products) Controller executes the action method (List) Action talks to Model, prepares the ViewData Action renders View But what is it?
22. Located in Controllers folder, You can change it derived from Controller base class The class name should be controller name + “Controller” Action Methods does the work Delete AccountController Move Controller’s to a separate project Controller
23. The ambiguous part of ASP.NET MVC M is the only part of MVC that is not implemented in ASP.NET MVC View Model: How your data looks on the page, flat data Domain Model: How your business data is, hierarchical data Any ORM is fine MODEL
24. Just renders the page, no biz logic!!! No ViewState, no server controls NO Codebehind, pls DON’T Default ViewEngine is WebForms Communicates with Controller with ViewData Dictionary Strongly Typed View VIEW
26. Separate Domain Model and View Model Use an ORM Use Repository Pattern Domain Model should only validate domain rules Presentation layer should NOT care about persistence layer Best Practices on Model
27. FALSE ViewModel == DomainModel The most annoying part of Presentation Model is the synchronization between Presentation Model and view… Martin Fowler, GUI Architectures Essay, July 2004
28. Music Store App Display Albums Display Customers Refactor the Model Use AutoMapper DEMO
29. No Business Logic No Concrete Dependencies Small Action Methods Don’t mix HttpVerbs Avoid magic strings Use controller as a traffic flow manager Unit test your expectations All the public methods are web accessible Use OutputCache Best Practices on Controller
30. Define Views in an enum:return View(viewNames.Index); Even Better use T4MVC View.Model MVCContrib for RedirectToAction:return RedirectToAction<HomeController>(c=>c.Index) Removing Magics in Controller
31. Refactor View Names and Links with T4MVC Cache the output Write simple unit test DEMO
32.
33. Is it HomeContoller’s job to satisfy dependencies?
34. Why does Home Controller have to know about BookRepository()?
35.
36. Extend ControllerFactory Use one of the IOC frameworks:StructureMap, Ninject, Windsor etc… Even Better Injection
38. No Magic String (use T4MVC) Change ../ paths Strongly Typed View RenderPartial HtmlHelpers Use HTML if you can If there is an “If” , time to check HtmlHelpers Encode input string Best Practices On View
40. Separate Controllers Use IoC No Business Logic in Controllers No Magic Strings in any component Use HTML Helpers DRY Test Strongly Types Views List of Practices
41. Client side validation with JQuery Templating Strongly typed helpers Areas Support Async Controller Actions New Things With MVC 2
42. Thanks to Jonas Follesø (http://jonas.follesoe.no) Thank you
Notes de l'éditeur
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…
Use automapper, silverlight and wpf has binding support , and mvvm support