|
1 | 1 | using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Concurrent; |
2 | 4 | using System.Collections.Generic; |
3 | 5 | using Microsoft.EntityFrameworkCore.ChangeTracking; |
4 | 6 |
|
5 | 7 | namespace EntityFrameworkCore.Triggered.Internal |
6 | 8 | { |
7 | 9 | public readonly struct TriggerContextDescriptor |
8 | 10 | { |
9 | | - [ThreadStatic] |
10 | | - static Dictionary<Type, Func<object, PropertyValues?, ChangeType, object>>? _cachedTriggerContextFactories; |
| 11 | + static readonly ConcurrentDictionary<Type, Func<object, PropertyValues?, ChangeType, object>> _cachedTriggerContextFactories = new(); |
11 | 12 |
|
12 | 13 | readonly EntityEntry _entityEntry; |
13 | 14 | readonly ChangeType _changeType; |
@@ -37,19 +38,10 @@ public object GetTriggerContext() |
37 | 38 |
|
38 | 39 | var entityType = entityEntry.Entity.GetType(); |
39 | 40 |
|
40 | | - if (_cachedTriggerContextFactories == null) |
41 | | - { |
42 | | - _cachedTriggerContextFactories = new Dictionary<Type, Func<object, PropertyValues?, ChangeType, object>>(); |
43 | | - } |
44 | | - |
45 | | - if (!_cachedTriggerContextFactories.TryGetValue(entityType, out var triggerContextFactory)) |
46 | | - { |
47 | | - triggerContextFactory = (Func<object, PropertyValues?, ChangeType, object>)typeof(TriggerContextFactory<>).MakeGenericType(entityType) |
| 41 | + var triggerContextFactory = _cachedTriggerContextFactories.GetOrAdd(entityType, entityType => |
| 42 | + (Func<object, PropertyValues?, ChangeType, object>)typeof(TriggerContextFactory<>).MakeGenericType(entityType) |
48 | 43 | .GetMethod(nameof(TriggerContextFactory<object>.Activate)) |
49 | | - .CreateDelegate(typeof(Func<object, PropertyValues?, ChangeType, object>)); |
50 | | - |
51 | | - _cachedTriggerContextFactories.Add(entityType, triggerContextFactory); |
52 | | - } |
| 44 | + .CreateDelegate(typeof(Func<object, PropertyValues?, ChangeType, object>))); |
53 | 45 |
|
54 | 46 | return triggerContextFactory(entityEntry.Entity, originalValues, changeType); |
55 | 47 | } |
|
0 commit comments