-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzz_Extensions.cs
314 lines (280 loc) · 9.72 KB
/
zz_Extensions.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
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using Spectre.Console;
namespace NotNot;
public static class zz_Extensions_Spectre_Console_Color
{
public static string _MarkupString(this Color color, string? message)
{
var markup = color.ToMarkup();
return $"[{markup}]{message}[/]";
}
}
//public static class zz_Extensions_OneOf
//{
// public static async Task<OneOf.OneOf<T0_new, T1>> _MapT0<T0, T1, T0_new, T1_derived>(this OneOf.OneOf<T0, T1> oneOf, Func<T0, Task<OneOf<T0_new, T1_derived>>> mapFunc
// )
// where T1 : class
// where T1_derived : T1
// {
// var tmp = oneOf.MapT0(mapFunc);
// if (tmp.TryPickT0(out var t0, out var t1))
// {
// var result = await t0;
// return result.MapT1(p => p as T1);
// }
// return t1;
// }
// public static OneOf.OneOf<T0_new, T1> _MapT0<T0, T1, T0_new>(this OneOf.OneOf<T0, T1> oneOf, Func<T0, T0_new> mapFunc
// )
// //where TProblem : class
// {
// var tmp = oneOf.MapT0(mapFunc);
// return tmp;
// // return tmp.MapT1(p => p as ProblemDetails);
// }
// /// <summary>
// /// allows mapping async functions to OneOf results
// /// </summary>
// public static async Task<OneOf.OneOf<T0_new, T1>> _MapT0<T0, T1, T0_new>(this OneOf.OneOf<T0, T1> oneOf, Func<T0, Task<T0_new>> mapFunc)
// {
// var tmp = oneOf.MapT0(mapFunc);
// if (tmp.TryPickT0(out var t0, out var t1))
// {
// return await t0;
// }
// return t1;
// }
// /// <summary>
// /// allows mapping async functions to OneOf results
// /// </summary>
// public static async Task<OneOf.OneOf<T0, T1_new>> _MapT1<T0, T1, T1_new>(this OneOf.OneOf<T0, T1> oneOf, Func<T1, Task<T1_new>> mapFunc)
// {
// var tmp = oneOf.MapT1(mapFunc);
// if (tmp.TryPickT0(out var t0, out var t1))
// {
// return t0;
// }
// return await t1;
// }
// /// <summary>
// /// easily map from OneOf to a web request's expected IResult return response
// /// </summary>
// /// <typeparam name="TResponse"></typeparam>
// /// <param name="oneOfResult"></param>
// /// <returns></returns>
// public static IResult _ToResult<TResponse>(this OneOf<TResponse, TProblemDetails> oneOfResult)
// {
// return oneOfResult.Match(
// response => Results.Ok(response),
// problem => Results.Problem(problem)
// );
// }
//}
//public static class zz_Extensions_ProblemDetails
//{
// //public static string? _CallSite(this ProblemDetails problem, string value)
// //{
// // if (value is null)
// // {
// // problem.Extensions.Remove("Callsite");
// // }
// // else
// // {
// // problem.Extensions["Callsite"] = value;
// // }
// // return value;
// //}
// //public static string? _CallSite(this ProblemDetails problem, [CallerMemberName] string memberName = "",
// // [CallerFilePath] string sourceFilePath = "",
// // [CallerLineNumber] int sourceLineNumber = 0)
// //{
// // var callsite = $"{memberName}:{sourceFilePath}:{sourceLineNumber}";
// // problem.Extensions["Callsite"] = callsite;
// // return callsite;
// //}
// //public static bool TryGetCallsite(this ProblemDetails problem, out string? callsite)
// //{
// // if (problem.Extensions.TryGetValue("Callsite", out var callsiteObj))
// // {
// // callsite = callsiteObj as string;
// // return true;
// // }
// // callsite = null;
// // return false;
// //}
// /// <summary>
// /// useful to hint to upstream callers that a problem is recoverable.
// /// not needed to call to set value as false, as it's redundant.
// /// </summary>
// public static bool _IsRecoverable(this ProblemDetails problem, bool value)
// {
// if (value is false)
// {
// problem.Extensions.Remove("isRecoverable");
// }
// else
// {
// problem.Extensions["isRecoverable"] = value;
// }
// return value;
// }
// public static bool _IsRecoverable(this ProblemDetails problem)
// {
// if(problem.Extensions.TryGetValue("isRecoverable", out var result)){
// switch (result)
// {
// case bool toReturn:
// return toReturn;
// break;
// default:
// __.GetLogger()._EzError( "problem.isRecoverable is not a bool", result);
// break;
// }
// }
// return false;
// //return problem.Extensions["isRecoverable"] as bool? ?? false;
// }
// public static Exception _Ex(this ProblemDetails problem, Exception value)
// {
// problem.Extensions["ex"] = value;
// return value;
// }
// /// <summary>
// /// extract exception from problemDetails. if not found, return null
// /// </summary>
// /// <param name="problem"></param>
// /// <returns></returns>
// public static Exception? _Ex(this ProblemDetails problem)
// {
// if (problem.Extensions["ex"] is Exception ex)
// {
// return ex;
// }
// foreach (var (key, value) in problem.Extensions)
// {
// if (value is Exception ex2)
// {
// return ex2;
// }
// }
// return null;
// }
//}
public static class zz_Extensions_HttpRequest
{
public static StringValues _GetKeyValues(this HttpRequest request, string key, string? altKey = null)
{
if (request.Query.TryGetValue(key, out var value))
{
return value;
}
if (altKey is not null && request.Query.TryGetValue(altKey, out var altValue))
{
return altValue;
}
if (request.Headers.TryGetValue(key, out value))
{
return value;
}
if (altKey is not null && request.Headers.TryGetValue(altKey, out altValue))
{
return altValue;
}
return StringValues.Empty;
}
}
public static class zz_Extensions_DbSet
{
/// <summary>
/// sets all entities in the dbSet to detached (which allows it's cache to be cleared, freeing GC)
/// BE SURE TO SAVE CHANGES FIRST!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbSet"></param>
/// <returns></returns>
[Obsolete("not working properly, clearing makes dbSet think entity doesn't exist (doesn't reaquire from underlying db)")]
public static async Task _ClearCache<T>(this DbSet<T> dbSet) where T : class
{
//disabling as seems to not work properly
foreach (var entity in dbSet.Local)
{
switch (dbSet.Entry(entity).State)
{
case EntityState.Deleted:
case EntityState.Modified:
case EntityState.Added:
__.GetLogger()._EzError(false, "should have saved changes before clearing cache");
break;
case EntityState.Unchanged:
case EntityState.Detached:
break;
default:
__.GetLogger()._EzErrorThrow(false, $"unhandled EntityState: {dbSet.Entry(entity).State}");
break;
}
dbSet.Entry(entity).State = EntityState.Detached;
}
//clearing via the changeTracker has the same problem.
//dbSet._Context().ChangeTracker.Clear();
}
/// <summary>
/// for reducing memory use. save all dbContext changes (including other dbsets!), and clears all local cached entities
/// (from ONLY this dbSet).
/// </summary>
public static async ValueTask _SaveAndClearCache<T>(this DbSet<T> dbSet, DbContext context, CancellationToken ct) where T : class
{
//var context = dbSet._Context();
await context.SaveChangesAsync(ct);
//mark all entities as detached (which allows it's cache to be cleared, freeing GC)
foreach (var entity in dbSet.Local)
{
var entry = dbSet.Entry(entity);
switch (entry.State)
{
case EntityState.Deleted:
case EntityState.Modified:
case EntityState.Added:
__.GetLogger()._EzError("threading race condition? should have saved changes before clearing cache", entry.State, entity);
break;
case EntityState.Unchanged:
case EntityState.Detached:
break;
default:
__.GetLogger()._EzError("unhandled EntityState", entry.State, entity);
break;
}
entry.State = EntityState.Detached;
}
//save changes for detach to take effect properly
//otherwise if detatched eneity would be returned, nothing would (it won't re-aquire a new copy from db either)
await context.SaveChangesAsync(ct);
}
/// <summary>
/// expensive way to get the context. don't use this method if at all possible
/// </summary>
public static DbContext _Context<T>(this DbSet<T> dbSet) where T : class
{
var infrastructureInterface = dbSet as IInfrastructure<IServiceProvider>;
var serviceProvider = infrastructureInterface.Instance;
var currentDbContext = serviceProvider.GetRequiredService<ICurrentDbContext>();
//var currentDbContext = serviceProvider.GetService(typeof(ICurrentDbContext))
// as ICurrentDbContext;
return currentDbContext.Context;
}
}
public static class zz_Extensions_DbContext
{
/// <summary>
/// for reducing memory use. save all dbContext changes, and clears all local cached entities (from ALL dbsets).
/// </summary>
public static async ValueTask _SaveAndClearCache(this DbContext context, CancellationToken ct)
{
await context.SaveChangesAsync(ct);
context.ChangeTracker.Clear();
//await context.SaveChangesAsync(ct);
}
}