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());
        }
예제 #2
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());
        }