#38 - Provide a default dictionary for HttpContext.Items that returns null for missing values.

This commit is contained in:
Chris Ross 2014-04-24 16:27:18 -07:00
parent 8ad7b489e2
commit 78bb008681
3 changed files with 113 additions and 1 deletions

View File

@ -0,0 +1,111 @@
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.PipelineCore
{
public class ItemsDictionary : IDictionary<object, object>
{
public ItemsDictionary()
: this(new Dictionary<object, object>())
{
}
public ItemsDictionary(IDictionary<object, object> items)
{
Items = items;
}
public IDictionary<object, object> Items { get; private set; }
// Replace the indexer with one that returns null for missing values
object IDictionary<object, object>.this[object key]
{
get
{
object value;
if (Items.TryGetValue(key, out value))
{
return value;
}
return null;
}
set { Items[key] = value; }
}
void IDictionary<object, object>.Add(object key, object value)
{
Items.Add(key, value);
}
bool IDictionary<object, object>.ContainsKey(object key)
{
return Items.ContainsKey(key);
}
ICollection<object> IDictionary<object, object>.Keys
{
get { return Items.Keys; }
}
bool IDictionary<object, object>.Remove(object key)
{
return Items.Remove(key);
}
bool IDictionary<object, object>.TryGetValue(object key, out object value)
{
return Items.TryGetValue(key, out value);
}
ICollection<object> IDictionary<object, object>.Values
{
get { return Items.Values; }
}
void ICollection<KeyValuePair<object, object>>.Add(KeyValuePair<object, object> item)
{
Items.Add(item);
}
void ICollection<KeyValuePair<object, object>>.Clear()
{
Items.Clear();
}
bool ICollection<KeyValuePair<object, object>>.Contains(KeyValuePair<object, object> item)
{
return Items.Contains(item);
}
void ICollection<KeyValuePair<object, object>>.CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
Items.CopyTo(array, arrayIndex);
}
int ICollection<KeyValuePair<object, object>>.Count
{
get { return Items.Count; }
}
bool ICollection<KeyValuePair<object, object>>.IsReadOnly
{
get { return Items.IsReadOnly; }
}
bool ICollection<KeyValuePair<object, object>>.Remove(KeyValuePair<object, object> item)
{
return Items.Remove(item);
}
IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator()
{
return Items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Items.GetEnumerator();
}
}
}

View File

@ -6,7 +6,7 @@ namespace Microsoft.AspNet.PipelineCore
{
public DefaultCanHasItems()
{
Items = new Dictionary<object, object>();
Items = new ItemsDictionary();
}
public IDictionary<object, object> Items { get; private set; }

View File

@ -23,6 +23,7 @@
<Compile Include="Builder.cs" />
<Compile Include="Collections\FormCollection.cs" />
<Compile Include="Collections\HeaderDictionary.cs" />
<Compile Include="Collections\ItemsDictionary.cs" />
<Compile Include="Collections\ReadableStringCollection.cs" />
<Compile Include="Collections\RequestCookiesCollection.cs" />
<Compile Include="Collections\ResponseCookies.cs" />