Your SlideShare is downloading. ×
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley

2,946

Published on

ASP.Net MVC: Patterns and AntiPatterns. Presentation from Devlink 2009, Nashville, TN, Chris Hefley http://indomitablehef.com

ASP.Net MVC: Patterns and AntiPatterns. Presentation from Devlink 2009, Nashville, TN, Chris Hefley http://indomitablehef.com

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,946
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
74
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Hi everyone, thanks for coming. This is ASP.NET MVC: Patterns and AntiPatterns, and I’m Chris Hefley. You’ll often see a talk like this one labeled as “Asp.Net MVC Best Practices”, but I intentionally stayed away from that formulation. I don’t really like the use of the term “Best Practices” most of the time, because it’s usually being used by someone who wants to sound authoritative about something they don’t understand well enough to explain why they feel the way they do about it.These are things I’ve found useful, and of course this is all “in my opinion”, but I’ll do my best to explain why each of the items we’ll cover is considered good or bad. This is not an introduction to ASP.Net MVC, but…since this is the only ASP.Net MVC talk here this year, I’m going to spend exactly one slide on explaining how MVC works, so that any of you are completely new to it may have a chance of remembering some of these things once you do get more familiar with it.
  • Ok, now on with the show.
  • When you hear talk about an “Anemic Domain Model”, it’s referring to a domain model that is almost all data and no behavior, such that all the behavior of the system is captured in some other layer, like a service layer or worse, in the presentation layer. That’s bad. An anemic controller, however is Good. Just like it’s a bad idea to put domain logic in your code-behind pages in web forms, it’s not a good idea to fill your controllers with that kind of logic either. Your controller actions should just call methods on some service layer or domain layer class, prepare any view models you are using (more on that later), and render the ViewResult or other ActionResult as needed. Think of them almost like Event handlers. They capture the incoming request, bind to the ViewModel passed back from the view, and pass that model on to another layer for processing.I like to find an illustrative image for these patterns, but when I googled “Skinny Controller, Fat Model” the results I got, while very illustrative of the point, were not really appropriate for display in a public forum.
  • Why? Because it will cause bugs in your code later. Expensive, hard to find bugs.
  • And this bit of code leads us right into our next pattern…
  • Don’t use ViewData[“key”] and don’t explicitly assign ViewData.model = model; Instead, use strongly typed views, and return View(model);For things like ViewData[“ErrorMessage”], create a base class for your ViewModel, that has properties like string ErrorMessage; etc.
  • Don’t use ViewData[“key”] and don’t explicitly assign ViewData.model = model; Instead, use strongly typed views, and return View(model);For things like ViewData[“ErrorMessage”], create a base class for your ViewModel, that has properties like string ErrorMessage; etc.
  • Don’t use ViewData[“key”] and don’t explicitly assign ViewData.model = model; Instead, use strongly typed views, and return View(model);For things like ViewData[“ErrorMessage”], create a base class for your ViewModel, that has properties like string ErrorMessage; etc.
  • User interface segregation principle insteadShow code: IUserContextShow Code: one-off-view-model
  • MvcContrib comes with ControllerFactories for several of the most popular IOC containers
  • MvcContrib comes with ControllerFactories for several of the most popular IOC containers
  • Show Ktransaction attribute
  • Transcript

    • 1. ASP.Net MVC : Patterns and AntiPatterns
      Chris Hefley, Bandit Software
      Blog: http://indomitablehef.com
      Email: indomitablehef@gmail.com
      Twitter: @indomitablehef
    • 2. Model View Controller
      ASP.Net ISAPI filter
      HttpModules and HttpHandlers
      HttpRequest
      Request
      Routed
      To
      Retrieve Data from
      Some Class
      Some Model
      System.Web.UI.Page
      System.Web.Mvc.Controller
      Render
      Some View
    • 3. Anemic Controller
      “Skinny Controller, Fat Model”
      GOOD
    • 4. BAD
      Magic Strings
    • 5. ASP.Net MVC Magic Strings
    • 6. Banishing Magic Strings: Views
      Instead of:
      Use:
    • 7. Banishing Magic Strings: Controllers
    • 8. Banishing Magic Strings: Controllers
    • 9. GOOD
      One Action, One View
      Instead of:
    • 10. One Action, One View
      Do this:
    • 11. GOOD
      View Models
    • 12. BAD
      ViewData Dictionary
    • 13. GOOD
      ViewModel Base Class
      Instead of ViewData Dictionary:
    • 14. ViewModel Base Class
      Instead:
    • 15. w00t!
      Use MVC Contrib
    • 16. BAD
      JavaScript in Views
      If you must:
      More goodness from MVC Contrib:
    • 17. BAD
      Branching Logic in Views
      Don’t do this:
      Or even this:
    • 18. Branching Logic in Views
    • 19. Branching Logic in Views
    • 20. BAD
      HttpContext
    • 21. GOOD
      IOC Container
    • 22. IOC Container
    • 23. GOOD
      Extensions to HtmlHelper
    • 24. BAD
      DataBinding to Domain Object
    • 25. GOOD
      Custom ActionResult
    • 26. Miscellaneous
      Use Html.Encode
      jQuery!
      Use AcceptVerbs Attribute on Controller Methods (POST for those that modify Data)
      Use ASP.Net MVC to create REST-ful APIs
      Extension Methods to UrlHelper
      Make good use of ActionFilters
    • 27. GOOD
      After-Market Parts
    • 28. Spark View Engine
      Sparkviewengine.com
    • 29. S#arp Architecture
    • 30. Questions?
      Chris Hefley, Bandit Software
      Blog: http://indomitablehef.com
      Email: indomitablehef@gmail.com
      Twitter: @indomitablehef

    ×