Monthly Archives: February 2011

1 post

What features would you like to see in ASP.NET MVC 4?

If there was a single feature I would like to see in ASP.NET MVC 4 that would be to remove/deprecate ViewBag/ViewData. Those two constructs lead to very ugly code in the views and should be avoided. Here are few of the things I hate about them:

  • They are not strongly typed and you need to cast in your views in order to obtain the actual type
  • They are not refactor friendly because they rely on magic strings
  • They lead to brittle unit tests because of the magic strings
  • They lead to spaghetti code in the views

Here’s the diff patch I would love to see applied for the ViewDataDictionary.cs class in ASP.NET MVC 4:

diff --git ViewDataDictionary.cs ViewDataDictionary.cs
index 4c6299f..1965e3e 100644
--- ViewDataDictionary.cs
+++ ViewDataDictionary.cs
@@ -95,12 +95,10 @@ namespace System.Web.Mvc {

         public object this[string key] {
             get {
-                object value;
-                _innerDictionary.TryGetValue(key, out value);
-                return value;
+                throw new Expcetion("Don't use ViewData");
             }
             set {
-                _innerDictionary[key] = value;
+                throw new Expcetion("Don't use ViewData");
             }
         }