-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Context.Dictionary.cs
159 lines (120 loc) · 6.85 KB
/
Context.Dictionary.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Polly
{
/// <summary>
/// Context that carries with a single execution through a Policy. Commonly-used properties are directly on the class. Backed by a dictionary of string key / object value pairs, to which user-defined values may be added.
/// <remarks>Do not re-use an instance of <see cref="Context"/> across more than one execution.</remarks>
/// </summary>
public partial class Context : IDictionary<string, object>, IDictionary
#if !NET40
, IReadOnlyDictionary<string, object>
#endif
{
// For an individual execution through a policy or policywrap, it is expected that all execution steps (for example executing the user delegate, invoking policy-activity delegates such as onRetry, onBreak, onTimeout etc) execute sequentially.
// Therefore, this class is intentionally not constructed to be safe for concurrent access from multiple threads.
private Dictionary<string, object> wrappedDictionary = null;
private Dictionary<string, object> WrappedDictionary => wrappedDictionary ?? (wrappedDictionary = new Dictionary<string, object>());
/// <summary>
/// Initializes a new instance of the <see cref="Context"/> class, with the specified <paramref name="operationKey" /> and the supplied <paramref name="contextData"/>.
/// </summary>
/// <param name="operationKey">The operation key.</param>
/// <param name="contextData">The context data.</param>
public Context(String operationKey, IDictionary<string, object> contextData) : this(contextData)
{
OperationKey = operationKey;
}
internal Context(IDictionary<string, object> contextData) : this()
{
if (contextData == null) throw new ArgumentNullException(nameof(contextData));
wrappedDictionary = new Dictionary<string, object>(contextData);
}
#region IDictionary<string,object> implementation
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public ICollection<string> Keys => WrappedDictionary.Keys;
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public ICollection<object> Values => WrappedDictionary.Values;
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public int Count => WrappedDictionary.Count;
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
bool ICollection<KeyValuePair<string, object>>.IsReadOnly => ((IDictionary<string, object>)WrappedDictionary).IsReadOnly;
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public object this[string key]
{
get => WrappedDictionary[key];
set => WrappedDictionary[key] = value;
}
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public void Add(string key, object value)
{
WrappedDictionary.Add(key, value);
}
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public bool ContainsKey(string key) => WrappedDictionary.ContainsKey(key);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public bool Remove(string key) => WrappedDictionary.Remove(key);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public bool TryGetValue(string key, out object value) => WrappedDictionary.TryGetValue(key, out value);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item) => ((IDictionary<string, object>)WrappedDictionary).Add(item);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public void Clear() => WrappedDictionary.Clear();
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item) => ((IDictionary<string, object>)WrappedDictionary).Contains(item);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) => ((IDictionary<string, object>) WrappedDictionary).CopyTo(array, arrayIndex);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item) => ((IDictionary<string, object>)WrappedDictionary).Remove(item);
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => WrappedDictionary.GetEnumerator();
/// <inheritdoc cref="IDictionary{TKey,Value}"/>
IEnumerator IEnumerable.GetEnumerator() => WrappedDictionary.GetEnumerator();
/// <inheritdoc cref="IDictionary"/>
public void Add(object key, object value)
{
((IDictionary)WrappedDictionary).Add(key, value);
}
/// <inheritdoc cref="IDictionary"/>
public bool Contains(object key)
{
return ((IDictionary)WrappedDictionary).Contains(key);
}
/// <inheritdoc cref="IDictionary"/>
IDictionaryEnumerator IDictionary.GetEnumerator()
{
return ((IDictionary)WrappedDictionary).GetEnumerator();
}
/// <inheritdoc cref="IDictionary"/>
public void Remove(object key)
{
((IDictionary)WrappedDictionary).Remove(key);
}
/// <inheritdoc cref="IDictionary"/>
public void CopyTo(Array array, int index)
{
((IDictionary)WrappedDictionary).CopyTo(array, index);
}
#endregion
#if !NET40
#region IReadOnlyDictionary<string, object> implementation
IEnumerable<string> IReadOnlyDictionary<string, object>.Keys => ((IReadOnlyDictionary<string, object>)WrappedDictionary).Keys;
IEnumerable<object> IReadOnlyDictionary<string, object>.Values => ((IReadOnlyDictionary<string, object>)WrappedDictionary).Values;
#endregion
#endif
#region IDictionary implementation
/// <inheritdoc cref="IDictionary"/>
bool IDictionary.IsFixedSize => ((IDictionary)WrappedDictionary).IsFixedSize;
/// <inheritdoc cref="IDictionary"/>
bool IDictionary.IsReadOnly => ((IDictionary)WrappedDictionary).IsReadOnly;
ICollection IDictionary.Keys => ((IDictionary)WrappedDictionary).Keys;
ICollection IDictionary.Values => ((IDictionary)WrappedDictionary).Values;
/// <inheritdoc cref="IDictionary"/>
bool ICollection.IsSynchronized => ((IDictionary)WrappedDictionary).IsSynchronized;
/// <inheritdoc cref="IDictionary"/>
object ICollection.SyncRoot => ((IDictionary)WrappedDictionary).SyncRoot;
/// <inheritdoc cref="IDictionary"/>
object IDictionary.this[object key] { get => ((IDictionary)WrappedDictionary)[key]; set => ((IDictionary)WrappedDictionary)[key] = value; }
#endregion
}
}