ASP.Net MVC : Patterns and AntiPatterns<br />Chris Hefley, Bandit Software<br />Blog: http://indomitablehef.com<br />Email...
Model View Controller<br />ASP.Net ISAPI filter<br />HttpModules and HttpHandlers<br />HttpRequest<br />Request<br />Route...
Anemic Controller<br />“Skinny Controller, Fat Model”<br />GOOD<br />
BAD<br />Magic Strings<br />
ASP.Net MVC Magic Strings<br />
Banishing Magic Strings: Views<br />Instead of:<br />  Use:<br />
Banishing Magic Strings: Controllers<br />
Banishing Magic Strings: Controllers<br />
GOOD<br />One Action, One View<br />Instead of:<br />
One Action, One View<br />Do this:<br />
GOOD<br />View Models<br />
BAD<br />ViewData Dictionary<br />
GOOD<br />ViewModel Base Class<br />Instead of ViewData Dictionary:<br />
ViewModel Base Class<br />Instead:<br />
w00t!<br />Use MVC Contrib<br />
BAD<br />JavaScript in Views<br />If you must:<br />More goodness from MVC Contrib:<br />
BAD<br />Branching Logic in Views<br />Don’t do this:<br />Or even this:<br />
Branching Logic in Views<br />
Branching Logic in Views<br />
BAD<br />HttpContext<br />
GOOD<br />IOC Container<br />
IOC Container<br />
GOOD<br />Extensions to HtmlHelper<br />
BAD<br />DataBinding to Domain Object<br />
GOOD<br />Custom ActionResult<br />
Miscellaneous<br />Use Html.Encode<br />jQuery!<br />Use AcceptVerbs Attribute on Controller Methods (POST for those that ...
GOOD<br />After-Market Parts<br />
Spark View Engine<br />Sparkviewengine.com<br />
S#arp Architecture<br />
Questions?<br />Chris Hefley, Bandit Software<br />Blog: http://indomitablehef.com<br />Email: indomitablehef@gmail.com<br...
Upcoming SlideShare
Loading in...5
×

Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley

2,997

Published on

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,997
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
76
Comments
0
Likes
2
Embeds 0
No embeds

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
  • Dev Link2009 Asp Net Mvc Pattern And Ani Patterns Chris Hefley

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

      Clipping is a handy way to collect important slides you want to go back to later.

    ×