forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeStorageDictionary.cs
More file actions
168 lines (140 loc) · 5.46 KB
/
Copy pathScopeStorageDictionary.cs
File metadata and controls
168 lines (140 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace System.Web.WebPages.Scope
{
public class ScopeStorageDictionary : IDictionary<object, object>
{
private static readonly StateStorageKeyValueComparer _keyValueComparer = new StateStorageKeyValueComparer();
private readonly IDictionary<object, object> _baseScope;
private readonly IDictionary<object, object> _backingStore;
public ScopeStorageDictionary()
: this(baseScope: null)
{
}
public ScopeStorageDictionary(IDictionary<object, object> baseScope)
: this(baseScope: baseScope, backingStore: new Dictionary<object, object>(ScopeStorageComparer.Instance))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ScopeStorageDictionary"/> class.
/// </summary>
/// <param name="baseScope">The base scope.</param>
/// <param name="backingStore">
/// The dictionary to use as a storage. Since the dictionary would be used as-is, we expect the implementer to
/// use the same key-value comparison logic as we do here.
/// </param>
internal ScopeStorageDictionary(IDictionary<object, object> baseScope, IDictionary<object, object> backingStore)
{
_baseScope = baseScope;
_backingStore = backingStore;
}
protected IDictionary<object, object> BackingStore
{
get { return _backingStore; }
}
protected IDictionary<object, object> BaseScope
{
get { return _baseScope; }
}
public virtual ICollection<object> Keys
{
get { return GetItems().Select(item => item.Key).ToList(); }
}
public virtual ICollection<object> Values
{
get { return GetItems().Select(item => item.Value).ToList(); }
}
public virtual int Count
{
get { return GetItems().Count(); }
}
public virtual bool IsReadOnly
{
get { return false; }
}
public object this[object key]
{
get
{
object value;
TryGetValue(key, out value);
return value;
}
set { SetValue(key, value); }
}
public virtual void SetValue(object key, object value)
{
_backingStore[key] = value;
}
public virtual bool TryGetValue(object key, out object value)
{
return _backingStore.TryGetValue(key, out value) || (_baseScope != null && _baseScope.TryGetValue(key, out value));
}
public virtual bool Remove(object key)
{
return _backingStore.Remove(key);
}
public virtual IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return GetItems().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public virtual void Add(object key, object value)
{
SetValue(key, value);
}
public virtual bool ContainsKey(object key)
{
return _backingStore.ContainsKey(key) || (_baseScope != null && _baseScope.ContainsKey(key));
}
public virtual void Add(KeyValuePair<object, object> item)
{
SetValue(item.Key, item.Value);
}
public virtual void Clear()
{
_backingStore.Clear();
}
public virtual bool Contains(KeyValuePair<object, object> item)
{
return _backingStore.Contains(item) || (_baseScope != null && _baseScope.Contains(item));
}
public virtual void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
GetItems().ToList().CopyTo(array, arrayIndex);
}
public virtual bool Remove(KeyValuePair<object, object> item)
{
return _backingStore.Remove(item);
}
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "This call might be expensive depending on how long the chain of contexts is")]
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This method is implementation specific and is not meant to be exposed as a public API.")]
protected virtual IEnumerable<KeyValuePair<object, object>> GetItems()
{
if (_baseScope == null)
{
return _backingStore;
}
return Enumerable.Concat(_backingStore, _baseScope).Distinct(_keyValueComparer);
}
private class StateStorageKeyValueComparer : IEqualityComparer<KeyValuePair<object, object>>
{
private IEqualityComparer<object> _stateStorageComparer = ScopeStorageComparer.Instance;
public bool Equals(KeyValuePair<object, object> x, KeyValuePair<object, object> y)
{
return _stateStorageComparer.Equals(x.Key, y.Key);
}
public int GetHashCode(KeyValuePair<object, object> obj)
{
return _stateStorageComparer.GetHashCode(obj.Key);
}
}
}
}