Пример #1
0
 public static void ApplyTo(
     this IEnumerable <KeyValuePair <string, JObject> > pathAndTrackerJObjects,
     ITrackable trackable,
     JsonSerializerSettings jsonSerializerSettings)
 {
     foreach (var item in pathAndTrackerJObjects)
     {
         var targetTrackable = trackable.GetTrackableByPath(item.Key);
         if (targetTrackable != null)
         {
             var trackerType = TrackerResolver.GetDefaultTracker(targetTrackable.GetType());
             var tracker     = (ITracker)item.Value.ToObject(
                 trackerType, JsonSerializer.Create(jsonSerializerSettings));
             tracker.ApplyTo(targetTrackable);
         }
     }
 }
        private static PropertyItem[] ConstructPropertyItems(RedisTypeConverter typeConverter)
        {
            var trackerType = TrackerResolver.GetDefaultTracker(typeof(T));

            var items = new List <PropertyItem>();

            foreach (var property in typeof(T).GetProperties())
            {
                var fieldName = property.Name;

                var attr = property.GetCustomAttribute <TrackablePropertyAttribute>();
                if (attr != null)
                {
                    if (attr["redis.ignore"] != null)
                    {
                        continue;
                    }
                    fieldName = attr["redis.field:"] ?? fieldName;
                }

                var item = new PropertyItem
                {
                    Name                  = property.Name,
                    FieldName             = fieldName,
                    PropertyInfo          = property,
                    TrackerPropertyInfo   = trackerType.GetProperty(property.Name + "Tracker"),
                    ConvertToRedisValue   = typeConverter.GetToRedisValueFunc(property.PropertyType),
                    ConvertFromRedisValue = typeConverter.GetFromRedisValueFunc(property.PropertyType),
                };

                if (item.TrackerPropertyInfo == null)
                {
                    throw new ArgumentException($"Cannot find tracker type of '{property.Name}'");
                }

                if (item.ConvertToRedisValue == null || item.ConvertFromRedisValue == null)
                {
                    throw new ArgumentException("Cannot find type converter. Property=" + property.Name);
                }

                items.Add(item);
            }
            return(items.ToArray());
        }
        private static PropertyItem[] ConstructPropertyItems(ISqlProvider sqlProvider,
                                                             Tuple <string, object[]>[] mapperParameters)
        {
            var trackerType        = TrackerResolver.GetDefaultTracker(typeof(T));
            var mapperParameterMap = mapperParameters.ToDictionary(x => x.Item1, x => x.Item2);

            var items = new List <PropertyItem>();

            foreach (var property in typeof(T).GetProperties())
            {
                var attr = property.GetCustomAttribute <TrackablePropertyAttribute>();
                if (attr != null)
                {
                    if (attr["sql.ignore"] != null)
                    {
                        continue;
                    }
                }

                var item = new PropertyItem
                {
                    Name                = property.Name,
                    PropertyInfo        = property,
                    TrackerPropertyInfo = trackerType.GetProperty(property.Name + "Tracker")
                };

                if (item.TrackerPropertyInfo == null)
                {
                    throw new ArgumentException($"Cannot find tracker type of '{property.Name}'");
                }

                object[] mapperParameter;
                if (mapperParameterMap.TryGetValue(property.Name, out mapperParameter) == false)
                {
                    throw new ArgumentException($"{property.Name} needs mapperParameter.");
                }

                if (TrackableResolver.IsTrackablePoco(property.PropertyType))
                {
                    typeof(TrackableContainerSqlMapper <T>)
                    .GetMethod("BuildTrackablePocoProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(TrackableResolver.GetPocoType(property.PropertyType))
                    .Invoke(null, new object[] { sqlProvider, item, mapperParameter });
                }
                else if (TrackableResolver.IsTrackableDictionary(property.PropertyType))
                {
                    typeof(TrackableContainerSqlMapper <T>)
                    .GetMethod("BuildTrackableDictionaryProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(property.PropertyType.GetGenericArguments())
                    .Invoke(null, new object[] { sqlProvider, item, mapperParameter });
                }
                else if (TrackableResolver.IsTrackableSet(property.PropertyType))
                {
                    typeof(TrackableContainerSqlMapper <T>)
                    .GetMethod("BuildTrackableSetProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(property.PropertyType.GetGenericArguments())
                    .Invoke(null, new object[] { sqlProvider, item, mapperParameter });
                }
                else
                {
                    throw new InvalidOperationException("Cannot resolve property: " + property.Name);
                }

                items.Add(item);
            }
            return(items.ToArray());
        }
Пример #4
0
        private static PropertyItem[] ConstructPropertyItems(RedisTypeConverter typeConverter)
        {
            var trackerType = TrackerResolver.GetDefaultTracker(typeof(T));

            var items = new List <PropertyItem>();

            foreach (var property in typeof(T).GetProperties())
            {
                var keySuffix = ":" + property.Name;

                var attr = property.GetCustomAttribute <TrackablePropertyAttribute>();
                if (attr != null)
                {
                    if (attr["redis.ignore"] != null)
                    {
                        continue;
                    }
                    keySuffix = attr["redis.keysuffix:"] ?? keySuffix;
                }

                var item = new PropertyItem
                {
                    Name                = property.Name,
                    KeySuffix           = keySuffix,
                    PropertyInfo        = property,
                    TrackerPropertyInfo = trackerType.GetProperty(property.Name + "Tracker")
                };

                if (item.TrackerPropertyInfo == null)
                {
                    throw new ArgumentException($"Cannot find tracker type of '{property.Name}'");
                }

                if (TrackableResolver.IsTrackablePoco(property.PropertyType))
                {
                    typeof(TrackableContainerRedisMapper <T>)
                    .GetMethod("BuildTrackablePocoProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(TrackableResolver.GetPocoType(property.PropertyType))
                    .Invoke(null, new object[] { item, typeConverter });
                }
                else if (TrackableResolver.IsTrackableDictionary(property.PropertyType))
                {
                    typeof(TrackableContainerRedisMapper <T>)
                    .GetMethod("BuildTrackableDictionaryProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(property.PropertyType.GetGenericArguments())
                    .Invoke(null, new object[] { item, typeConverter });
                }
                else if (TrackableResolver.IsTrackableSet(property.PropertyType))
                {
                    typeof(TrackableContainerRedisMapper <T>)
                    .GetMethod("BuildTrackableSetProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(property.PropertyType.GetGenericArguments())
                    .Invoke(null, new object[] { item, typeConverter });
                }
                else if (TrackableResolver.IsTrackableList(property.PropertyType))
                {
                    typeof(TrackableContainerRedisMapper <T>)
                    .GetMethod("BuildTrackableListProperty", BindingFlags.Static | BindingFlags.NonPublic)
                    .MakeGenericMethod(property.PropertyType.GetGenericArguments())
                    .Invoke(null, new object[] { item, typeConverter });
                }
                else
                {
                    throw new InvalidOperationException("Cannot resolve property: " + property.Name);
                }

                items.Add(item);
            }
            return(items.ToArray());
        }