tags:

views:

496

answers:

6

Basically, this 'question' is intended to pool information about best practices in the community. The reasons for this are:

  • The distinct lack of an community based ASP.NET MVC resource for best practices. Yes, I am aware of wiki.asp.net.
  • I'm painfully aware that the ASP.NET wiki on ASP.NET MVC is woefully small and terribly out of date and needs fleshing out more by Microsoft before the community does anything with it.
  • I'm very much interested in best practices in general, having had a fair degree of experience with ASP.NET MVC over the last year, but I'm also mindful that I'm no expert, and that I can definitely improve. This post, I believe, can help that, and you.

I gave some serious thought on whether or not I should ask this question, even going to the lengths of creating a question on meta about creating this question. :)

What I am looking for is for best practice information on all aspects of ASP.NET MVC. Controllers, models, extensions, HTML helpers, route builders, TDD.

In the past, I have found resources, such as Kazi Manzur Rashid's best practices (part 1 and 2) post quite useful, along with Maarten Balliauw's weigh-in, but these have dried up somewhat, and I was hoping for a more dynamic and growing repository of information from different people, rather than just the occasional blog entry from learned individuals. Information like this is few and far between, and ASP.NET MVC community resources even less so.


MVC in general

Model

Feel free to add model links

Views

Controller

Feel free to add controller links


Guidelines:

  • One "best practice" per answer
  • Take the time to explain why it should be preferred over the alternatives.
  • Read existing answers first - if you see one you agree with, up-vote it; if you disagree, down-vote and leave a comment explaining why.
  • Feel free to edit any answer if you can expand on why it is "best"!

P.S. Please don't just put "Anything by Phil Haack, Scott Guthrie, Rob Conery or Scott Hanselman"!

+2  A: 

While not a best practices site, the below site is doing great things with asp.net MVC that could probably be considered best practice.

http://www.codeplex.com/MVCContrib

klabranche
+6  A: 
  1. IoC/DI for Controller factory (so I can inject IRepository, ISomeService in controllers constructor)
  2. never access HttpContext directly, build wrapper, so it can be unit tested
  3. Validation framework for model binding validations (xVal or FluentValidation). Built-in validation inside MVC 1 is basic
  4. never use "magic strings": for calling controllers/actions from View, for RouteLink, RenderPartial, RenderAction, ...
  5. never use ViewData, build DTO ViewModel classes. Use AutoMapper for mapping data from domain entities to ViewModel DTO objects for View

ViewModel DTO objects:
BaseViewModel abstract class, with properties for rendering page meta data, menus and all other stuff that appears on every page. All other ViewModel classes inherits from BaseViewModel.

Hrvoje
sorry i didn't put one best practice per answer, i couldn't choose one ;)
Hrvoje
You could have written five separate answers.
Jan Aagaard
If possible, can you provide examples of these, or prior art?
Dan Atkinson
I don't believe that it's practical to not use ViewData in very application. For example, master pages, and their partials. Unless there is a better solution?
Dan Atkinson
@jan: i think it's SO best practices to have one answer, which every one can edit and improve
Hrvoje
+4  A: 

Keep any and all logic out of your view. Your controller should determine what gets shown - your view should be dumb.

MunkiPhD
I think *some* logic is acceptable/inevitable. For example... <% if(Request.IsAuthenticated) { %>Logout<% } else { %>Login<% } %>. Putting that somewhere else like a helper isn't any better.
Dan Atkinson
@Dan Atkinson - actually, I'd put <% if (Foo.ShowLogout) {%>Logout<%}%> instead - why rely on the request in the view? What if you're rendering it in a test?
orip
+7  A: 

Use T4MVC to eliminate all magic strings and magic anonymous types in your entire project. This will help you in refactoring later in your project, and (apart from having to still ensure your routes are well-defined) all Action calls get their proper parameters. It changes calls like this:

<%= Html.ActionLink("Link text", "Products", "Details", new { id = Model.Id }) %>

Into:

<%= Html.ActionLink("Link text", MVC.Products.Details(Model.Id)) %>
Jason
This seems to buy you very little. Plus, in all the MVC books I've read and websites I visited, this is the first time I have seen this ...
Martin
Looking forward, ASP.NET MVC v2 will be using strongly-typed controls such as ActionLink.
Dan Atkinson
@Martin, it's extremely helpful if you have these calls scattered over many views. Using Resharper or other refactoring tools will catch these calls and it saves a *lot* of time and headache. @Dan, yes, but nothing so far in Preview 1.
Jason
The main thing this appears to buy you is tool supported refactoring/renaming. If you change ProductsController.Details to ProductsController.Examine (!?) using a tool, the the second case looks after itself while in the first case, you'd need to manually change every instance.
Colin Desmond
+1  A: 

There are 2 free chapters available for the book "MVC in Action".

http://www.manning.com/palermo/

Chapter 12 is best practices.

Martin
Is Chapter 12 one of the free chapters?
Dan Atkinson
Yep ... under Downloads -> Sample Chapter 12 ...
Martin
+1  A: 

Keep your controllers as "thin" as possible. Simply have them validate data coming in from the form, and then calling the model (which does all the real work) to get the data to be returned in your next View.

Having complex controllers kind of breaks the MVC concept.

Colin Desmond