Patrick Desjardins Blog
Patrick Desjardins picture from a conference

TempData why you should use bracket instead of .Add

Posted on: 2012-08-13

TempDataDictionary (TempData is of type of TempDataDictionary) is useful if you need to keep you data between redirection. I have see some case of error with programmer who use the TempData with the Add method.

First the Add method won't let you add more than 1 same key. This mean that it will crash if you write:

 TempData.Add("key1","value1"); 
 TempData.Add("key1","value2"); 

You will tell me why someone will do this, well, in the scenario of redirection, which should occur if you are using TempData instead of ViewBag you may comeback in the same path of code that the user set the data. In this case, the value are set twice cause the TempData in a redirection keep its values.

So, you can add some validation with the ContainsKey method.

if(!TempData.ContainsKey("key1")) { 
  TempData.Add("key1","value1"); 
} 

Second, this solve the problem but create overwhelming code for a simple task. The solution in my opinion is to simply use the bracket overload of the TempDataDictionary.

 TempData["key1"] = "value1"; 

The code is shorter, cleaner and do what we want. It set the value and if it's a redirection to the same code who has set the value this one will just reset the same value. That's it! Simple and clean.

If you are curious about the bracket code, here is the source code of the TempData for the property [].

private Dictionary<string, object>_data; 
private HashSet<string>_initialKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase); 
//... 
public object this[string key] { 
  get { object value; 
    if (TryGetValue(key, out value)) {
      _initialKeys.Remove(key); 
      return value; 
    } 
    return null; 
  } 

  set {
    _data[key] = value;_initialKeys.Add(key); 
  } 
} 

Of course, the real question now is why does this code has redirection that double set this value, but this is another story.