forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeStorage.cs
More file actions
308 lines (256 loc) · 9.54 KB
/
Copy pathScopeStorage.cs
File metadata and controls
308 lines (256 loc) · 9.54 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace ServiceStack.Html
{
public static class ScopeStorage
{
private static readonly IScopeStorageProvider _defaultStorageProvider = new StaticScopeStorageProvider();
private static IScopeStorageProvider _stateStorageProvider;
public static IScopeStorageProvider CurrentProvider
{
get { return _stateStorageProvider ?? _defaultStorageProvider; }
set { _stateStorageProvider = value; }
}
public static IDictionary<object, object> CurrentScope
{
get { return CurrentProvider.CurrentScope; }
}
public static IDictionary<object, object> GlobalScope
{
get { return CurrentProvider.GlobalScope; }
}
public static IDisposable CreateTransientScope(IDictionary<object, object> context)
{
var currentContext = CurrentScope;
CurrentProvider.CurrentScope = context;
return new DisposableAction(() => CurrentProvider.CurrentScope = currentContext); // Return an IDisposable that pops the item back off
}
public static IDisposable CreateTransientScope()
{
return CreateTransientScope(new ScopeStorageDictionary(baseScope: CurrentScope));
}
}
public class StaticScopeStorageProvider : IScopeStorageProvider
{
private static readonly IDictionary<object, object> _defaultContext =
new ScopeStorageDictionary(null, new ConcurrentDictionary<object, object>(ScopeStorageComparer.Instance));
private IDictionary<object, object> _currentContext;
public IDictionary<object, object> CurrentScope
{
get { return _currentContext ?? _defaultContext; }
set { _currentContext = value; }
}
public IDictionary<object, object> GlobalScope
{
get { return _defaultContext; }
}
}
public interface IScopeStorageProvider
{
IDictionary<object, object> CurrentScope { get; set; }
IDictionary<object, object> GlobalScope { get; }
}
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);
}
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);
}
}
}
/// <summary>
/// Custom comparer for the context dictionaries
/// The comparer treats strings as a special case, performing case insesitive comparison.
/// This guaratees that we remain consistent throughout the chain of contexts since PageData dictionary
/// behaves in this manner.
/// </summary>
internal class ScopeStorageComparer : IEqualityComparer<object>
{
private static IEqualityComparer<object> _instance;
private readonly IEqualityComparer<object> _defaultComparer = EqualityComparer<object>.Default;
private readonly IEqualityComparer<string> _stringComparer = StringComparer.OrdinalIgnoreCase;
private ScopeStorageComparer()
{
}
public static IEqualityComparer<object> Instance
{
get
{
if (_instance == null) {
_instance = new ScopeStorageComparer();
}
return _instance;
}
}
public new bool Equals(object x, object y)
{
string xString = x as string;
string yString = y as string;
if ((xString != null) && (yString != null)) {
return _stringComparer.Equals(xString, yString);
}
return _defaultComparer.Equals(x, y);
}
public int GetHashCode(object obj)
{
string objString = obj as string;
if (objString != null) {
return _stringComparer.GetHashCode(objString);
}
return _defaultComparer.GetHashCode(obj);
}
}
internal class DisposableAction : IDisposable
{
private Action _action;
private bool _hasDisposed;
public DisposableAction(Action action)
{
if (action == null) {
throw new ArgumentNullException("action");
}
_action = action;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
// If we were disposed by the finalizer it's because the user didn't use a "using" block, so don't do anything!
if (disposing) {
lock (this) {
if (!_hasDisposed) {
_hasDisposed = true;
_action();
}
}
}
}
}
}