MVC 4 Best Practices
Use the NuGet Package Manager to Manage Dependencies
The NuGet package manager is a great boon to developers and teams alike. Instead of spending your time getting up and running and checking to see if the projects that your application depends on have released new versions, let NuGet handle all of that for you!
If your organization has several teams that share common libraries, consider creating custom NuGet packages for those shared libraries and hosting a custom NuGet repository to provide more effective distribution and versioning.
If your organization has several teams that share common libraries, consider creating custom NuGet packages for those shared libraries and hosting a custom NuGet repository to provide more effective distribution and versioning.
Depend On Abstractions
Abstractions encourage loosely-coupled systems with a healthy separation of contracts and implementations. Abstractions are easily interchanged which not only provides easier maintenance, but is also crucial to unit testing.Avoid Referring To HttpContext Directly (use HttpContextBase)
ASP.NET MVC (and later, .NET 4) introduced System.Web.Abstractions, a set of abstractions over many of the core parts of the ASP.NET Framework. The "depend on abstractions" advice extends to these classes as well. In particular, one of the most often referenced objects in ASP.NET development is HttpContext — prefer using the HttpContextBase abstraction instead.Avoid "Magic Strings"
"Magic Strings" are crucial, yet arbitrary string values — may be convenient and in many situations even required, however they have many issues. Some of the biggest issues with magic strings are that they:- don't have any intrinsic meaning (e.g. it's difficult to tell how or if one "ID" relates to another "ID")
- are easily broken with misspelling or case sensitivity
- don't react well to refactoring
- promote rampant, pervasive duplication
Here are two examples, the first using magic strings to access data in a View Data dictionary, and the second refactored example with that same data in a strongly-typed model:
Using magic strings.
Comments