-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
DbContextHelper.cs
586 lines (554 loc) · 23.5 KB
/
DbContextHelper.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#if EF_CORE
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
#else
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
#endif
using Audit.Core.Extensions;
using Audit.Core;
using Audit.EntityFramework.ConfigurationApi;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace Audit.EntityFramework
{
public partial class DbContextHelper
{
// Entities Include/Ignore attributes cache
private static readonly ConcurrentDictionary<Type, bool?> EntitiesIncludeIgnoreAttrCache = new ConcurrentDictionary<Type, bool?>();
// Ignored properties per entity type attribute cache
private static readonly ConcurrentDictionary<Type, HashSet<string>> PropertiesIgnoreAttrCache = new ConcurrentDictionary<Type, HashSet<string>>();
// Overriden properties per entity type attribute cache
private static readonly ConcurrentDictionary<Type, Dictionary<string, AuditOverrideAttribute>> PropertiesOverrideAttrCache = new ConcurrentDictionary<Type, Dictionary<string, AuditOverrideAttribute>>();
// AuditDbContext Attribute cache
private static readonly ConcurrentDictionary<Type, AuditDbContextAttribute> _auditAttributeCache = new ConcurrentDictionary<Type, AuditDbContextAttribute>();
/// <summary>
/// Sets the configuration values from attribute, local and global
/// </summary>
public void SetConfig(IAuditDbContext context)
{
var type = context.DbContext.GetType();
if (!_auditAttributeCache.ContainsKey(type))
{
_auditAttributeCache[type] = type.GetTypeInfo().GetCustomAttribute(typeof(AuditDbContextAttribute)) as AuditDbContextAttribute;
}
var attrConfig = _auditAttributeCache[type]?.InternalConfig;
var localConfig = Audit.EntityFramework.Configuration.GetConfigForType(type);
var globalConfig = Audit.EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext));
context.Mode = attrConfig?.Mode ?? localConfig?.Mode ?? globalConfig?.Mode ?? AuditOptionMode.OptOut;
context.IncludeEntityObjects = attrConfig?.IncludeEntityObjects ?? localConfig?.IncludeEntityObjects ?? globalConfig?.IncludeEntityObjects ?? false;
context.ExcludeValidationResults = attrConfig?.ExcludeValidationResults ?? localConfig?.ExcludeValidationResults ?? globalConfig?.ExcludeValidationResults ?? false;
context.AuditEventType = attrConfig?.AuditEventType ?? localConfig?.AuditEventType ?? globalConfig?.AuditEventType;
context.EntitySettings = MergeEntitySettings(attrConfig?.EntitySettings, localConfig?.EntitySettings, globalConfig?.EntitySettings);
context.ExcludeTransactionId = attrConfig?.ExcludeTransactionId ?? localConfig?.ExcludeTransactionId ?? globalConfig?.ExcludeTransactionId ?? false;
context.EarlySavingAudit = attrConfig?.EarlySavingAudit ?? localConfig?.EarlySavingAudit ?? globalConfig?.EarlySavingAudit ?? false;
#if EF_FULL
context.IncludeIndependantAssociations = attrConfig?.IncludeIndependantAssociations ?? localConfig?.IncludeIndependantAssociations ?? globalConfig?.IncludeIndependantAssociations ?? false;
#endif
}
internal Dictionary<Type, EfEntitySettings> MergeEntitySettings(Dictionary<Type, EfEntitySettings> attr, Dictionary<Type, EfEntitySettings> local, Dictionary<Type, EfEntitySettings> global)
{
var settings = new List<Dictionary<Type, EfEntitySettings>>();
if (global != null && global.Count > 0)
{
settings.Add(global);
}
if (local != null && local.Count > 0)
{
settings.Add(local);
}
if (attr != null && attr.Count > 0)
{
settings.Add(attr);
}
if (settings.Count == 0)
{
return null;
}
var merged = new Dictionary<Type, EfEntitySettings>(settings[0]);
for (int i = 1; i < settings.Count; i++)
{
foreach(var kvp in settings[i])
{
if (!merged.ContainsKey(kvp.Key))
{
merged[kvp.Key] = kvp.Value;
}
else
{
foreach (var ip in kvp.Value.IgnoredProperties)
{
merged[kvp.Key].IgnoredProperties.Add(ip);
}
foreach (var op in kvp.Value.OverrideProperties)
{
merged[kvp.Key].OverrideProperties[op.Key] = op.Value;
}
}
}
}
return merged;
}
/// <summary>
/// Gets the validation results, return NULL if there are no validation errors.
/// </summary>
public static List<ValidationResult> GetValidationResults(object entity)
{
var validationContext = new ValidationContext(entity);
var validationResults = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(entity, validationContext, validationResults, true);
return valid ? null : validationResults;
}
/// <summary>
/// Gets the name for an entity state.
/// </summary>
public static string GetStateName(EntityState state)
{
switch (state)
{
case EntityState.Added:
return "Insert";
case EntityState.Modified:
return "Update";
case EntityState.Deleted:
return "Delete";
default:
return "Unknown";
}
}
/// <summary>
/// Saves the scope.
/// </summary>
public void SaveScope(IAuditDbContext context, IAuditScope scope, EntityFrameworkEvent @event)
{
UpdateAuditEvent(@event, context);
(scope.Event as AuditEventEntityFramework).EntityFrameworkEvent = @event;
context.OnScopeSaving(scope);
scope.Save();
context.OnScopeSaved(scope);
}
/// <summary>
/// Saves the scope asynchronously.
/// </summary>
public async Task SaveScopeAsync(IAuditDbContext context, IAuditScope scope, EntityFrameworkEvent @event)
{
UpdateAuditEvent(@event, context);
(scope.Event as AuditEventEntityFramework).EntityFrameworkEvent = @event;
context.OnScopeSaving(scope);
await scope.SaveAsync();
context.OnScopeSaved(scope);
}
// Determines whether to include the entity on the audit log or not
private bool IncludeEntity(IAuditDbContext context, object entity, AuditOptionMode mode)
{
var type = entity.GetType();
#if EF_FULL
type = ObjectContext.GetObjectType(type);
#else
if (type.FullName.StartsWith("Castle.Proxies."))
{
type = type.GetTypeInfo().BaseType;
}
#endif
bool ? result = EnsureEntitiesIncludeIgnoreAttrCache(type); //true:excluded false=ignored null=unknown
if (result == null)
{
// No static attributes, check the filters
var localConfig = EntityFramework.Configuration.GetConfigForType(context.DbContext.GetType());
var globalConfig = EntityFramework.Configuration.GetConfigForType(typeof(AuditDbContext));
var included = EvalIncludeFilter(type, localConfig, globalConfig);
var ignored = EvalIgnoreFilter(type, localConfig, globalConfig);
result = included ? true : ignored ? false : (bool?)null;
}
if (mode == AuditOptionMode.OptIn)
{
// Include only explicitly included entities
return result.GetValueOrDefault();
}
// Include all, except the explicitly ignored entities
return result == null || result.Value;
}
private bool? EnsureEntitiesIncludeIgnoreAttrCache(Type type)
{
if (!EntitiesIncludeIgnoreAttrCache.ContainsKey(type))
{
var includeAttr = type.GetTypeInfo().GetCustomAttribute(typeof(AuditIncludeAttribute), true);
if (includeAttr != null)
{
EntitiesIncludeIgnoreAttrCache[type] = true; // Type Included by IncludeAttribute
}
else if (type.GetTypeInfo().GetCustomAttribute(typeof(AuditIgnoreAttribute), true) != null)
{
EntitiesIncludeIgnoreAttrCache[type] = false; // Type Ignored by IgnoreAttribute
}
else
{
EntitiesIncludeIgnoreAttrCache[type] = null; // No attribute
}
}
return EntitiesIncludeIgnoreAttrCache[type];
}
private HashSet<string> EnsurePropertiesIgnoreAttrCache(Type type)
{
if (!PropertiesIgnoreAttrCache.ContainsKey(type))
{
var ignoredProps = new HashSet<string>();
foreach(var prop in type.GetTypeInfo().GetProperties())
{
var ignoreAttr = prop.GetCustomAttribute(typeof(AuditIgnoreAttribute), true);
if (ignoreAttr != null)
{
ignoredProps.Add(prop.Name);
}
}
if (ignoredProps.Count > 0)
{
PropertiesIgnoreAttrCache[type] = ignoredProps;
}
else
{
PropertiesIgnoreAttrCache[type] = null;
}
}
return PropertiesIgnoreAttrCache[type];
}
private Dictionary<string, AuditOverrideAttribute> EnsurePropertiesOverrideAttrCache(Type type)
{
if (!PropertiesOverrideAttrCache.ContainsKey(type))
{
var overrideProps = new Dictionary<string, AuditOverrideAttribute>();
foreach (var prop in type.GetTypeInfo().GetProperties())
{
var overrideAttr = prop.GetCustomAttribute<AuditOverrideAttribute>(true);
if (overrideAttr != null)
{
overrideProps[prop.Name] = overrideAttr;
}
}
if (overrideProps.Count > 0)
{
PropertiesOverrideAttrCache[type] = overrideProps;
}
else
{
PropertiesOverrideAttrCache[type] = null;
}
}
return PropertiesOverrideAttrCache[type];
}
/// <summary>
/// Gets the include value for a given entity type.
/// </summary>
private bool EvalIncludeFilter(Type type, EfSettings localConfig, EfSettings globalConfig)
{
var includedExplicit = localConfig?.IncludedTypes.Contains(type) ?? globalConfig?.IncludedTypes.Contains(type) ?? false;
if (includedExplicit)
{
return true;
}
var includedFilter = localConfig?.IncludedTypesFilter ?? globalConfig?.IncludedTypesFilter;
if (includedFilter != null)
{
return includedFilter.Invoke(type);
}
return false;
}
/// <summary>
/// Gets the exclude value for a given entity type.
/// </summary>
private bool EvalIgnoreFilter(Type type, EfSettings localConfig, EfSettings globalConfig)
{
var ignoredExplicit = localConfig?.IgnoredTypes.Contains(type) ?? globalConfig?.IgnoredTypes.Contains(type) ?? false;
if (ignoredExplicit)
{
return true;
}
var ignoredFilter = localConfig?.IgnoredTypesFilter ?? globalConfig?.IgnoredTypesFilter;
if (ignoredFilter != null)
{
return ignoredFilter.Invoke(type);
}
return false;
}
/// <summary>
/// Creates the Audit scope.
/// </summary>
public IAuditScope CreateAuditScope(IAuditDbContext context, EntityFrameworkEvent efEvent)
{
var typeName = context.DbContext.GetType().Name;
var eventType = context.AuditEventType?.Replace("{context}", typeName).Replace("{database}", efEvent.Database) ?? typeName;
var auditEfEvent = new AuditEventEntityFramework
{
EntityFrameworkEvent = efEvent
};
if (context.ExtraFields != null && context.ExtraFields.Count > 0)
{
auditEfEvent.CustomFields = new Dictionary<string, object>(context.ExtraFields);
}
var factory = context.AuditScopeFactory ?? Core.Configuration.AuditScopeFactory;
var options = new AuditScopeOptions()
{
EventType = eventType,
CreationPolicy = EventCreationPolicy.Manual,
DataProvider = context.AuditDataProvider,
AuditEvent = auditEfEvent,
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
SkipExtraFrames = 5
#elif EF_CORE && !NETSTANDARD2_0 && !NET461 && !NET472
SkipExtraFrames = 4
#else
SkipExtraFrames = 3
#endif
};
var scope = factory.Create(options);
context.OnScopeCreated(scope);
return scope;
}
/// <summary>
/// Creates the Audit scope asynchronously.
/// </summary>
public async Task<IAuditScope> CreateAuditScopeAsync(IAuditDbContext context, EntityFrameworkEvent efEvent)
{
var typeName = context.DbContext.GetType().Name;
var eventType = context.AuditEventType?.Replace("{context}", typeName).Replace("{database}", efEvent.Database) ?? typeName;
var auditEfEvent = new AuditEventEntityFramework
{
EntityFrameworkEvent = efEvent
};
if (context.ExtraFields != null && context.ExtraFields.Count > 0)
{
auditEfEvent.CustomFields = new Dictionary<string, object>(context.ExtraFields);
}
var factory = context.AuditScopeFactory ?? Core.Configuration.AuditScopeFactory;
var options = new AuditScopeOptions()
{
EventType = eventType,
CreationPolicy = EventCreationPolicy.Manual,
DataProvider = context.AuditDataProvider,
AuditEvent = auditEfEvent,
SkipExtraFrames = 3
};
var scope = await factory.CreateAsync(options);
context.OnScopeCreated(scope);
return scope;
}
/// <summary>
/// Gets the modified entries to process.
/// </summary>
#if EF_CORE
public List<EntityEntry> GetModifiedEntries(IAuditDbContext context)
#else
public List<DbEntityEntry> GetModifiedEntries(IAuditDbContext context)
#endif
{
return context.DbContext.ChangeTracker.Entries()
.Where(x => x.State != EntityState.Unchanged
&& x.State != EntityState.Detached
&& IncludeEntity(context, x.Entity, context.Mode))
.ToList();
}
/// <summary>
/// Gets a unique ID for the current SQL transaction.
/// </summary>
/// <param name="transaction">The transaction.</param>
/// <param name="clientConnectionId">The client connection identifier.</param>
/// <returns>System.String.</returns>
private static string GetTransactionId(DbTransaction transaction, string clientConnectionId)
{
var propIntTran = transaction.GetType().GetTypeInfo().GetProperty("InternalTransaction", BindingFlags.NonPublic | BindingFlags.Instance);
object intTran = propIntTran?.GetValue(transaction);
var propTranId = intTran?.GetType().GetTypeInfo().GetProperty("TransactionId", BindingFlags.NonPublic | BindingFlags.Instance);
var tranId = propTranId?.GetValue(intTran);
return string.Format("{0}_{1}", clientConnectionId, tranId ?? 0);
}
public string GetClientConnectionId(DbContext dbContext)
{
#if EF_CORE
var connection = IsRelational(dbContext) ? dbContext.Database.GetDbConnection() : null;
#else
var connection = dbContext.Database.Connection;
#endif
return GetClientConnectionId(connection);
}
public string GetClientConnectionId(DbConnection dbConnection)
{
if (dbConnection == null)
{
return null;
}
#if EF_CORE && (NETSTANDARD1_5 || NETSTANDARD2_0 || NETSTANDARD2_1 || NET472 || NET5_0_OR_GREATER)
try
{
var connId = ((dbConnection as dynamic).ClientConnectionId) as Guid?;
return connId.HasValue && !connId.Value.Equals(Guid.Empty) ? connId.Value.ToString() : null;
}
catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)
{
return null;
}
#else
// Get the connection id (returns NULL if the connection is not open)
var sqlConnection = dbConnection as System.Data.SqlClient.SqlConnection;
var connId = sqlConnection?.ClientConnectionId;
return connId.HasValue && !connId.Value.Equals(Guid.Empty) ? connId.Value.ToString() : null;
#endif
}
/// <summary>
/// Saves the changes synchronously.
/// </summary>
public int SaveChanges(IAuditDbContext context, Func<int> baseSaveChanges)
{
return SaveChangesGetAuditImpl(context, baseSaveChanges).Result;
}
/// <summary>
/// Saves the changes asynchronously.
/// </summary>
public async Task<int> SaveChangesAsync(IAuditDbContext context, Func<Task<int>> baseSaveChanges)
{
return (await SaveChangesGetAuditAsyncImpl(context, baseSaveChanges)).Result;
}
/// <summary>
/// Saves the changes and returns the audit event generated.
/// </summary>
public EntityFrameworkEvent SaveChangesGetAudit(IAuditDbContext context, Func<int> baseSaveChanges)
{
return SaveChangesGetAuditImpl(context, baseSaveChanges);
}
/// <summary>
/// Saves the changes asynchronously and returns the audit event generated.
/// </summary>
public async Task<EntityFrameworkEvent> SaveChangesGetAuditAsync(IAuditDbContext context, Func<Task<int>> baseSaveChanges)
{
return await SaveChangesGetAuditAsyncImpl(context, baseSaveChanges);
}
private async Task<EntityFrameworkEvent> SaveChangesGetAuditAsyncImpl(IAuditDbContext context, Func<Task<int>> baseSaveChanges)
{
if (context.AuditDisabled)
{
return new EntityFrameworkEvent() { Result = await baseSaveChanges() };
}
var efEvent = CreateAuditEvent(context);
if (efEvent == null)
{
return new EntityFrameworkEvent() { Result = await baseSaveChanges() };
}
var scope = await CreateAuditScopeAsync(context, efEvent);
if (context.EarlySavingAudit)
{
await SaveScopeAsync(context, scope, efEvent);
}
try
{
efEvent.Result = await baseSaveChanges();
}
catch (Exception ex)
{
efEvent.Success = false;
efEvent.ErrorMessage = ex.GetExceptionInfo();
await SaveScopeAsync(context, scope, efEvent);
throw;
}
efEvent.Success = true;
await SaveScopeAsync(context, scope, efEvent);
return efEvent;
}
private EntityFrameworkEvent SaveChangesGetAuditImpl(IAuditDbContext context, Func<int> baseSaveChanges)
{
if (context.AuditDisabled)
{
return new EntityFrameworkEvent() { Result = baseSaveChanges() };
}
var efEvent = CreateAuditEvent(context);
if (efEvent == null)
{
return new EntityFrameworkEvent() { Result = baseSaveChanges() };
}
var scope = CreateAuditScope(context, efEvent);
if (context.EarlySavingAudit)
{
SaveScope(context, scope, efEvent);
}
try
{
efEvent.Result = baseSaveChanges();
}
catch (Exception ex)
{
efEvent.Success = false;
efEvent.ErrorMessage = ex.GetExceptionInfo();
SaveScope(context, scope, efEvent);
throw;
}
efEvent.Success = true;
SaveScope(context, scope, efEvent);
return efEvent;
}
public IAuditScope BeginSaveChanges(IAuditDbContext context)
{
if (context.AuditDisabled)
{
return null;
}
var efEvent = CreateAuditEvent(context);
if (efEvent == null)
{
return null;
}
var scope = CreateAuditScope(context, efEvent);
if (context.EarlySavingAudit)
{
SaveScope(context, scope, efEvent);
}
return scope;
}
public async Task<IAuditScope> BeginSaveChangesAsync(IAuditDbContext context)
{
if (context.AuditDisabled)
{
return null;
}
var efEvent = CreateAuditEvent(context);
if (efEvent == null)
{
return null;
}
var scope = await CreateAuditScopeAsync(context, efEvent);
if (context.EarlySavingAudit)
{
await SaveScopeAsync(context, scope, efEvent);
}
return scope;
}
public void EndSaveChanges(IAuditDbContext context, IAuditScope scope, int result, Exception exception = null)
{
var efEvent = scope.GetEntityFrameworkEvent();
if (efEvent == null)
{
return;
}
efEvent.Success = exception == null;
efEvent.Result = result;
efEvent.ErrorMessage = exception?.GetExceptionInfo();
SaveScope(context, scope, efEvent);
}
public async Task EndSaveChangesAsync(IAuditDbContext context, IAuditScope scope, int result, Exception exception = null)
{
var efEvent = scope.GetEntityFrameworkEvent();
if (efEvent == null)
{
return;
}
efEvent.Success = exception == null;
efEvent.Result = result;
efEvent.ErrorMessage = exception?.GetExceptionInfo();
await SaveScopeAsync(context, scope, efEvent);
}
}
}