-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathStatics.cs
346 lines (278 loc) · 9.11 KB
/
Statics.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System.Threading;
using System.Collections;
using System.Collections.Concurrent;
namespace Signum.Utilities;
public static class Statics
{
static readonly ConcurrentDictionary<string, IThreadVariable> threadVariables = new ConcurrentDictionary<string, IThreadVariable>();
public static IThreadVariable? GetVariable(string name)
{
return threadVariables.TryGetC(name);
}
public static AsyncThreadVariable<T> ThreadVariable<T>(string name, bool avoidExportImport = false)
{
var variable = new AsyncThreadVariable<T>(name) { AvoidExportImport = avoidExportImport };
threadVariables.AddOrThrow(name, variable, "Thread variable {0} already defined");
return variable;
}
public static Dictionary<string, object?> ExportThreadContext(bool force = false)
{
return threadVariables.Where(t => !t.Value.IsClean && (!t.Value.AvoidExportImport || force)).ToDictionaryEx(kvp => kvp.Key, kvp => kvp.Value.UntypedValue);
}
public static IDisposable ImportThreadContext(Dictionary<string, object> context)
{
foreach (var kvp in threadVariables)
{
var val = context.TryGetC(kvp.Key);
if (val != null)
kvp.Value.UntypedValue = val;
else
kvp.Value.Clean();
}
return new Disposable(() =>
{
foreach (var v in threadVariables.Values)
{
v.Clean();
}
});
}
public static void CleanThreadContextAndAssert()
{
string errors = threadVariables.Values.Where(v => !v.IsClean).ToString(v => "{0} contains the non-default value {1}".FormatWith(v.Name, v.UntypedValue), "\r\n");
foreach (var v in threadVariables.Values)
{
v.Clean();
}
if (errors.HasText())
throw new InvalidOperationException("The thread variable \r\n" + errors);
}
static readonly Dictionary<string, IUntypedVariable> sessionVariables = new Dictionary<string, IUntypedVariable>();
public static SessionVariable<T> SessionVariable<T>(string name)
{
var variable = SessionFactory.CreateVariable<T>(name);
sessionVariables.AddOrThrow(name, variable, "Session variable {0} already defined");
return variable;
}
static ISessionFactory sessionFactory = new ScopeSessionFactory(new SingletonSessionFactory());
public static ISessionFactory SessionFactory
{
get
{
if (sessionFactory == null)
throw new InvalidOperationException("Statics.SessionFactory not determined. (Tip: add a StaticSessionFactory, ScopeSessionFactory or AspNetSessionFactory)");
return sessionFactory;
}
set { sessionFactory = value; }
}
}
public interface IUntypedVariable
{
string Name { get; }
object? UntypedValue { get; set; }
bool IsClean { get; }
void Clean();
}
public abstract class Variable<T> : IUntypedVariable
{
public string Name { get; private set; }
public Variable(string name)
{
this.Name = name;
}
public abstract T Value { get; set; }
public object? UntypedValue
{
get { return Value; }
set { Value = (T)value!; }
}
public bool IsClean
{
get
{
if (Value == null)
return true;
if (Value.Equals(default(T)!))
return true;
if (Value is IEnumerable col)
{
foreach (var item in col)
{
return false;
}
return true;
}
return false;
}
}
public abstract void Clean();
}
public interface IThreadVariable : IUntypedVariable
{
bool AvoidExportImport { get; }
}
public class AsyncThreadVariable<T> : Variable<T>, IThreadVariable
{
readonly AsyncLocal<T> store = new AsyncLocal<T>();
internal AsyncThreadVariable(string name) : base(name) { }
public override T Value
{
get { return store.Value!; }
set { store.Value = value; }
}
public bool AvoidExportImport { get; set; }
public override void Clean()
{
Value = default!;
}
}
public abstract class SessionVariable<T> : Variable<T>
{
public abstract Func<T>? ValueFactory { get; set; }
protected internal SessionVariable(string name)
: base(name)
{
}
public T GetDefaulValue()
{
if (ValueFactory == null)
return default!;
Value = ValueFactory();
return Value;
}
}
public interface ISessionFactory
{
SessionVariable<T> CreateVariable<T>(string name);
}
public class VoidSessionFactory : ISessionFactory
{
public SessionVariable<T> CreateVariable<T>(string name)
{
return new VoidVariable<T>(name);
}
class VoidVariable<T> : SessionVariable<T>
{
public override Func<T>? ValueFactory { get; set; }
public VoidVariable(string name)
: base(name)
{
}
public override T Value
{
get { return default!; }
set { throw new InvalidOperationException("No session found to set '{0}'".FormatWith(this.Name)); }
}
public override void Clean()
{
}
}
}
public class SingletonSessionFactory : ISessionFactory
{
public static Dictionary<string, object?> singletonSession = new Dictionary<string, object?>();
public static Dictionary<string, object?> SingletonSession
{
get { return singletonSession; }
set { singletonSession = value; }
}
public SessionVariable<T> CreateVariable<T>(string name)
{
return new SingletonVariable<T>(name);
}
class SingletonVariable<T> : SessionVariable<T>
{
public override Func<T>? ValueFactory { get; set; }
public SingletonVariable(string name)
: base(name)
{ }
public override T Value
{
get
{
if (singletonSession.TryGetValue(Name, out object? result))
return (T)result!;
return GetDefaulValue();
}
set { singletonSession[Name] = value; }
}
public override void Clean()
{
singletonSession.Remove(Name);
}
}
}
public class ScopeSessionFactory : ISessionFactory
{
public ISessionFactory Factory;
static readonly AsyncThreadVariable<Dictionary<string, object?>> overridenSession = Statics.ThreadVariable<Dictionary<string, object?>>("overridenSession");
public static bool IsOverriden
{
get { return overridenSession.Value != null; }
}
public ScopeSessionFactory(ISessionFactory factory)
{
this.Factory = factory;
}
public static IDisposable OverrideSession()
{
return OverrideSession(new Dictionary<string, object?>());
}
public static IDisposable OverrideSession(Dictionary<string, object?> sessionDictionary)
{
if (!(Statics.SessionFactory is ScopeSessionFactory))
throw new InvalidOperationException("Impossible to OverrideSession because Statics.SessionFactory is not a ScopeSessionFactory");
var old = overridenSession.Value;
overridenSession.Value = sessionDictionary;
return new Disposable(() => overridenSession.Value = old);
}
public SessionVariable<T> CreateVariable<T>(string name)
{
return new OverrideableVariable<T>(Factory.CreateVariable<T>(name));
}
class OverrideableVariable<T> : SessionVariable<T>
{
readonly SessionVariable<T> variable;
public OverrideableVariable(SessionVariable<T> variable)
: base(variable.Name)
{
this.variable = variable;
}
public override Func<T>? ValueFactory
{
get { return variable.ValueFactory; }
set { variable.ValueFactory = value; }
}
public override T Value
{
get
{
var dic = overridenSession.Value;
if (dic != null)
{
if (dic.TryGetValue(Name, out object? result))
return (T)result!;
return GetDefaulValue();
}
else
return variable.Value;
}
set
{
var dic = overridenSession.Value;
if (dic != null)
dic[Name] = value;
else
variable.Value = value;
}
}
public override void Clean()
{
var dic = overridenSession.Value;
if (dic != null)
dic.Remove(Name);
else
variable.Clean();
}
}
}