|
| 1 | +using System; |
| 2 | +using System.ComponentModel.DataAnnotations; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Reflection; |
| 6 | +using TrackerEnabledDbContext.Common.Extensions; |
| 7 | + |
| 8 | +namespace TrackerEnabledDbContext.Common.Configuration |
| 9 | +{ |
| 10 | + public class PropertyTrackingConfiguration<T> |
| 11 | + { |
| 12 | + public PropertyTrackingConfiguration<T> SkipProperty(Expression<Func<T, object>> property) |
| 13 | + { |
| 14 | + PropertyInfo info = property.GetPropertyInfo(); |
| 15 | + var newValue = new TrackingConfigurationValue(false, TrackingConfigurationPriority.High); |
| 16 | + |
| 17 | + TrackingDataStore.PropertyConfigStore.AddOrUpdate( |
| 18 | + new PropertyConfiguerationKey(info.Name, info.DeclaringType.FullName), |
| 19 | + newValue, |
| 20 | + (existingKey, existingvalue) => newValue); |
| 21 | + |
| 22 | + return this; |
| 23 | + } |
| 24 | + |
| 25 | + public PropertyTrackingConfiguration<T> TrackProperty(Expression<Func<T, object>> property) |
| 26 | + { |
| 27 | + PropertyInfo info = property.GetPropertyInfo(); |
| 28 | + var newValue = new TrackingConfigurationValue(true, TrackingConfigurationPriority.High); |
| 29 | + TrackingDataStore.PropertyConfigStore.AddOrUpdate( |
| 30 | + new PropertyConfiguerationKey(info.Name, info.DeclaringType.FullName), |
| 31 | + newValue, |
| 32 | + (key, value) => newValue); |
| 33 | + |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public static bool IsTrackingEnabled(Expression<Func<T, object>> property) |
| 38 | + { |
| 39 | + var key = new PropertyConfiguerationKey(property.GetPropertyInfo().Name, |
| 40 | + property.GetPropertyInfo().DeclaringType.FullName); |
| 41 | + |
| 42 | + string propertyName = property.GetPropertyInfo().Name; |
| 43 | + |
| 44 | + var result = TrackingDataStore.PropertyConfigStore |
| 45 | + .GetOrAdd(key, |
| 46 | + (x) => |
| 47 | + PropertyTrackingConfiguration.PropertyConfigValueFactory(propertyName, typeof(T))); |
| 48 | + |
| 49 | + return result.Value; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static class PropertyTrackingConfiguration |
| 54 | + { |
| 55 | + public static bool IsTrackingEnabled(PropertyConfiguerationKey property, Type entityType) |
| 56 | + { |
| 57 | + var result = TrackingDataStore.PropertyConfigStore |
| 58 | + .GetOrAdd(property, |
| 59 | + (x) => |
| 60 | + PropertyConfigValueFactory(property.PropertyName, entityType)); |
| 61 | + |
| 62 | + return result.Value; |
| 63 | + } |
| 64 | + |
| 65 | + internal static TrackingConfigurationValue PropertyConfigValueFactory(string propertyName, |
| 66 | + Type entityType) |
| 67 | + { |
| 68 | + SkipTrackingAttribute skipTrackingAttribute = |
| 69 | + entityType.GetProperty(propertyName) |
| 70 | + .GetCustomAttributes(false) |
| 71 | + .OfType<SkipTrackingAttribute>() |
| 72 | + .SingleOrDefault(); |
| 73 | + |
| 74 | + bool trackValue = skipTrackingAttribute == null || !skipTrackingAttribute.Enabled; |
| 75 | + |
| 76 | + return new TrackingConfigurationValue(trackValue); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments