ASP.NET MVC Presentation


Published on

My ASP.NET MVC presentation at the San Diego .NET User Group

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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
  • ASP.NET MVC Presentation

    1. 1. ASP.NET MVC<br />VolkanUzun<br />
    2. 2. Web Developer at California State University, San Bernardino<br />MS. Computer Science, MCP, and MCTS<br />Interest Areas: TDD, Web Programming, Secure Coding<br />User group maniac, visiting many user groups in a month<br />Blog:,<br />Email:<br />Twitter:<br />About Me<br />
    3. 3.
    4. 4. Classic ASP (Another Slow Page!?)<br />.NET Framework (Hurraay!!)<br />ASP.NET v1.0 (No more slow pages)<br />ASP.NET v1.1<br />ASP.NET v2.0<br />ASP.NET Ajax <br />ASP.NET 3.5<br />Silverlight ?!?!<br />ASP.NET MVC<br />ASP.NET WebForms 4.0<br />….<br />MS Web Developer History<br />
    5. 5. Easy to learn but?<br />Maintenance?<br />Test?<br />Debugging?<br />Complexity?<br />What was wrong with ASP?<br />
    6. 6. Desktop like programming paradigm<br />Lot’s of out ofbox controls<br />No more spaghetti code?<br />ASP.NET Web Forms<br />
    7. 7. ViewState (supposed to help you but?)<br />DRY<br />Can’t control HTML(Power is nothing without control)<br />Best Practices? <br />Separation of Concerns? (I don’t have any concern)<br />What is wrong with ASP.NET WebForms?<br />
    8. 8. New Patterns<br />
    9. 9. MVC<br />Different people reading about MVC in different places take different ideas from it and describe these as “MVC”.<br />Martin Fowler, GUI Architectures Essay (July 2006)<br />
    10. 10. Model–View–Controller (MVC) is an architectural pattern <br />Formulated in 1979 by TrygveMikkjel Heyerdahl Reenskaug<br />The model is the domain-specific representation of the data upon which the application operates<br />The controller receives input and initiates a response by making calls on model objects.<br />The view renders the model into a form suitable for interaction<br />Wikipedia: MVC<br />
    11. 11. <ul><li>A new web project type built on top of ASP.NET.
    12. 12. NOT replacing ASP.NET Web Forms.
    13. 13. Giving you more control over html.
    14. 14. An easier to test framework.
    15. 15. Another option to web development.
    16. 16. Open source.
    17. 17. Convention over configuration.</li></ul>ASP.NET MVC<br />
    18. 18. No View State, No Postback<br />Very flexible, pluggable.<br />Very clean URLs, rest friendly urls.<br />Supports ASP.NET Providers: Membership Provider, Role Provider, Session, Cache etc…<br />Open Source.<br />Community Support.<br />Supports Separation of Concerns.<br />Plays well with others<br />Why is it good?<br />
    19. 19. Complete control over html!!<br />No View State, No Postback!!<br />Paradigm change.<br />Few components .<br />No formal validation support.<br />Any Disadvantage?<br />
    20. 20. WebForms are dead?<br />MVC does not have as much support as WebForms?<br />MVC guarantees better coding?<br />A project is either MVC or WebForms?<br />MVC is faster than WebForms?<br />True / False<br />
    21. 21. Browser requests /Products/List<br />Route is selected<br />Controller is activated (Products)<br />Controller executes the action method (List)<br />Action talks to Model, prepares the ViewData<br />Action renders View<br />But what is it?<br />
    22. 22. Located in Controllers folder, You can change it<br />derived from Controller base class<br />The class name should be controller name + “Controller”<br />Action Methods does the work<br />Delete AccountController<br />Move Controller’s to a separate project<br />Controller<br />
    23. 23. The ambiguous part of ASP.NET MVC<br />M is the only part of MVC that is not implemented in ASP.NET MVC<br />View Model: How your data looks on the page, flat data<br />Domain Model: How your business data is, hierarchical data<br />Any ORM is fine<br />MODEL<br />
    24. 24. Just renders the page, no biz logic!!!<br />No ViewState, no server controls<br />NO Codebehind, pls DON’T<br />Default ViewEngine is WebForms<br />Communicates with Controller with ViewData Dictionary<br />Strongly Typed View<br />VIEW<br />
    25. 25. FILE => NEW…<br />Project Structure<br />
    26. 26. Separate Domain Model and View Model<br />Use an ORM<br />Use Repository Pattern<br />Domain Model should only validate domain rules<br />Presentation layer should NOT care about persistence layer<br />Best Practices on Model<br />
    27. 27. FALSE<br />ViewModel == DomainModel<br />The most annoying part of Presentation Model is the synchronization between Presentation Model and view…<br />Martin Fowler, GUI Architectures Essay, July 2004<br />
    28. 28. Music Store App<br />Display Albums<br />Display Customers<br />Refactor the Model<br />Use AutoMapper<br />DEMO<br />
    29. 29. No Business Logic<br />No Concrete Dependencies<br />Small Action Methods<br />Don’t mix HttpVerbs<br />Avoid magic strings<br />Use controller as a traffic flow manager<br />Unit test your expectations<br />All the public methods are web accessible<br />Use OutputCache<br />Best Practices on Controller<br />
    30. 30. Define Views in an enum:return View(viewNames.Index);<br />Even Better use T4MVC<br />View.Model<br />MVCContrib for RedirectToAction:return RedirectToAction<HomeController>(c=>c.Index)<br />Removing Magics in Controller<br />
    31. 31. Refactor View Names and Links with T4MVC<br />Cache the output<br />Write simple unit test<br />DEMO<br />
    32. 32. Dependency Injection<br />private IBookRepositorybookRepository;public HomeController(){bookRepository = new BookRepository();}<br /><ul><li> What if BookRepository has dependencies?
    33. 33. Is it HomeContoller’s job to satisfy dependencies?
    34. 34. Why does Home Controller have to know about BookRepository()?
    35. 35. What if HomeController needs initialization param in the constructor?</li></li></ul><li>private IBookRepositorybookRepository;public HomeController(IBookRepository repo){bookRepository = repo;}<br /> But who calls the controller with a parameterized constructor?<br /> NOT MVC Framework, at least not the default ControllerFactory<br />Better Injection<br />
    36. 36. Extend ControllerFactory<br />Use one of the IOC frameworks:StructureMap, Ninject, Windsor etc…<br />Even Better Injection<br />
    37. 37. CastleWindsorController Factory<br />DEMO<br />
    38. 38. No Magic String (use T4MVC)<br />Change ../ paths<br />Strongly Typed View<br />RenderPartial<br />HtmlHelpers<br />Use HTML if you can<br />If there is an “If” , time to check HtmlHelpers<br />Encode input string<br />Best Practices On View<br />
    39. 39. RenderPartial Customer<br />RefactorStyleSheet<br />DEMO<br />
    40. 40. Separate Controllers<br />Use IoC<br />No Business Logic in Controllers<br />No Magic Strings in any component<br />Use HTML Helpers<br />DRY<br />Test<br />Strongly Types Views<br />List of Practices<br />
    41. 41. Client side validation with JQuery<br />Templating<br />Strongly typed helpers<br />Areas Support<br />Async Controller Actions<br />New Things With MVC 2<br />
    42. 42. Thanks to Jonas Follesø (<br />Thank you<br />