コード例 #1
0
        public static bool RegisterTrackablePocoMap(Type trackablePocoType)
        {
            if (BsonClassMap.IsClassMapRegistered(trackablePocoType))
            {
                return(false);
            }

            var classMap = new BsonClassMap(trackablePocoType);
            var pocoType = TrackableResolver.GetPocoType(trackablePocoType);

            // start with auto map
            classMap.AutoMap();

            // ignore extra elements for smooth schema change
            classMap.SetIgnoreExtraElements(true);

            // unmap all members which T doesn't have
            var propertyNames   = new HashSet <string>(pocoType.GetProperties().Select(p => p.Name));
            var deletingMembers = classMap.DeclaredMemberMaps.Where(m =>
            {
                var propertyInfo = m.MemberInfo as PropertyInfo;
                return(propertyInfo == null ||
                       propertyNames.Contains(propertyInfo.Name) == false);
            }).ToList();

            foreach (var m in deletingMembers)
            {
                classMap.UnmapMember(m.MemberInfo);
            }

            // set default ignore for saving spaces
            foreach (var memberMap in classMap.DeclaredMemberMaps)
            {
                var mt           = memberMap.MemberType;
                var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null;
                memberMap.SetDefaultValue(defaultValue);
                memberMap.SetIgnoreIfDefault(true);
            }

            // tell customized id to mongo-db
            var identityColumn = pocoType.GetProperties().FirstOrDefault(
                p => TrackablePropertyAttribute.GetParameter(p, "mongodb.identity") != null);

            if (identityColumn != null)
            {
                classMap.MapIdProperty(identityColumn.Name);
            }

            try
            {
                BsonClassMap.RegisterClassMap(classMap);
            }
            catch (ArgumentException)
            {
                // if duplicate key exists
                return(false);
            }

            return(true);
        }
コード例 #2
0
        CreatePocoUpdateFunc <TTrackablePoco>()
        {
            var pocoType = TrackableResolver.GetPocoType(typeof(TTrackablePoco));

            if (pocoType == null)
            {
                return(null);
            }

            var genericMethod = typeof(TrackableMongoDbMapper).GetMethod(
                "GeneratePocoUpdateBson", BindingFlags.Static | BindingFlags.NonPublic);
            var method = genericMethod.MakeGenericMethod(pocoType, typeof(TTrackablePoco));
            var mapper = Activator.CreateInstance(GetMapperType(typeof(TTrackablePoco)));
            var func   = method.Invoke(null, new object[] { mapper });

            return((Func <UpdateDefinition <BsonDocument>, TTrackablePoco, object[], UpdateDefinition <BsonDocument> >)func);
        }
コード例 #3
0
        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());
        }