Exemple #1
0
        private MongodbData ConvertItemToMongodbData(object item)
        {
            var type         = item.GetType();
            var typeFullName = type.FullName;

            try
            {
                var data = new MongodbData();
                data.TypeFullName = typeFullName;
                data.TableName    = "General";

                if (!mongodbDatabaseDescriptionCache.ContainsKey(typeFullName))
                {
                    LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "ConvertItemToMongodbData", typeFullName,
                                              string.Format("没取到类型为 '{0}' 的元数据!", typeFullName));
                    return(null);
                }

                var mongodbDatabaseDescription = mongodbDatabaseDescriptionCache[typeFullName];
                data.DatabaseName = mongodbDatabaseDescription.DatabasePrefix;

                JavaScriptSerializer s = new JavaScriptSerializer();
                var rawData            = GetMongodbData(data, type, item);
                var jsonData           = s.Serialize(rawData);
                data.Data = jsonData;
                return(data);
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "ConvertItemToMongodbData", typeFullName, ex.Message);
                throw;
            }
        }
Exemple #2
0
        private void InternalGetMongodbData(MongodbData data, string typeFullName, object item, Dictionary <string, object> dic, PropertyInfo pi)
        {
            try
            {
                var val = pi.FastGetValue(item);
                if (val == null)
                {
                    return;
                }

                var configList = propertyConfigCache.ContainsKey(data.TypeFullName) ? propertyConfigCache[data.TypeFullName] : null;
                var config     = configList != null ? (configList.ContainsKey(pi) ? configList[pi] : null) : null;
                if (config != null && config.IsIgnore)
                {
                    return;
                }

                var columnName = pi.Name;

                if (config != null && config.IsTableName && !string.IsNullOrEmpty(val.ToString()))
                {
                    data.TableName = val.ToString();
                }
                if (config != null && !string.IsNullOrEmpty(config.ColumnName))
                {
                    columnName = config.ColumnName;
                }
                if (config != null && config.IsDateColumn)
                {
                    data.DatabaseName = string.Format("{0}__{1}", data.DatabaseName, ((DateTime)val).ToString("yyyyMM"));
                }

                if (typeof(Delegate).IsAssignableFrom(pi.PropertyType))
                {
                    return;
                }

                if (val is IList)
                {
                    var listData = val as IList;

                    var subdic = new Dictionary <string, object>();
                    lock (listData)
                    {
                        for (int i = 0; i < listData.Count; i++)
                        {
                            var    subitem     = listData[i];
                            var    subitemType = subitem.GetType();
                            object subData     = null;
                            if (!subitemType.Assembly.GlobalAssemblyCache && subitemType.BaseType != typeof(Enum))
                            {
                                subData = GetMongodbData(data, subitemType, subitem);
                            }
                            else if (subitem != null)
                            {
                                subData = subitem.ToString();
                            }
                            if (subData != null)
                            {
                                subdic.Add(string.Format("{0}__{1}", subitemType.Name, i.ToString()), subData);
                            }
                        }
                        if (dic.ContainsKey(columnName))
                        {
                            LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "InternalGetMongodbData", typeFullName,
                                                        string.Format("遇到重复的列,类型:{0},列名:{1}", typeFullName, columnName));
                        }
                        else
                        {
                            dic.Add(columnName, subdic);
                        }
                        return;
                    }
                }

                if (val is IDictionary)
                {
                    var dicData = val as IDictionary;
                    var subdic  = new Dictionary <string, object>();
                    lock (dicData)
                    {
                        foreach (object key in dicData.Keys)
                        {
                            var    subitem     = dicData[key];
                            var    subitemType = subitem.GetType();
                            object subData     = null;
                            if (!subitemType.Assembly.GlobalAssemblyCache && subitemType.BaseType != typeof(Enum))
                            {
                                subData = GetMongodbData(data, subitemType, subitem);
                            }
                            else if (subitem != null)
                            {
                                subData = subitem.ToString();
                            }
                            if (subData != null)
                            {
                                subdic.Add(string.Format("{0}__{1}", subitemType.Name, key.ToString().Replace(".", "")), subData);
                            }
                        }
                        if (dic.ContainsKey(columnName))
                        {
                            LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "InternalGetMongodbData", typeFullName,
                                                        string.Format("遇到重复的列,类型:{0},列名:{1}", typeFullName, columnName));
                        }
                        else
                        {
                            dic.Add(columnName, subdic);
                        }
                        return;
                    }
                }

                if (pi.PropertyType.Assembly.GlobalAssemblyCache || pi.PropertyType.BaseType == typeof(Enum))
                {
                    if (dic.ContainsKey(columnName))
                    {
                        LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "InternalGetMongodbData", typeFullName,
                                                    string.Format("遇到重复的列,类型:{0},列名:{1}", typeFullName, columnName));
                    }
                    else
                    {
                        dic.Add(columnName, val);
                    }
                }
                else
                {
                    var subdic     = new Dictionary <string, object>();
                    var properties = GetPropertyListFromCache(val.GetType());
                    if (properties != null)
                    {
                        foreach (var property in properties)
                        {
                            InternalGetMongodbData(data, typeFullName, val, subdic, property);
                        }
                        if (dic.ContainsKey(columnName))
                        {
                            LocalLoggingService.Warning("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "InternalGetMongodbData", typeFullName,
                                                        string.Format("遇到重复的列,类型:{0},列名:{1}", typeFullName, columnName));
                        }
                        else
                        {
                            dic.Add(columnName, subdic);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "InternalGetMongodbData", typeFullName, ex.Message);
                throw;
            }
        }
Exemple #3
0
        private Dictionary <string, object> GetMongodbData(MongodbData data, Type type, object item)
        {
            var typeFullName = type.FullName;

            try
            {
                var returnDictionary = new Dictionary <string, object>();
                var properties       = GetPropertyListFromCache(type);
                if (properties != null)
                {
                    foreach (var property in properties)
                    {
                        var configList = propertyConfigCache.ContainsKey(typeFullName) ? propertyConfigCache[typeFullName] : null;
                        if (configList == null)
                        {
                            continue;
                        }

                        var config = configList.ContainsKey(property) ? configList[property] : null;
                        if (config == null)
                        {
                            continue;
                        }

                        if (config.IsCascadeFilterLevelOne)
                        {
                            var value = (string)property.FastGetValue(item);
                            property.FastSetValue(item, value.Replace("__", "_"));
                        }
                        if (config.IsCascadeFilterLevelTwo)
                        {
                            var parentLevel = configList.Where(pc => pc.Value.IsCascadeFilterLevelOne).Select(pc => pc.Key).FirstOrDefault();
                            if (parentLevel != null)
                            {
                                var two = (string)property.FastGetValue(item);
                                if (two != null)
                                {
                                    two = two.Replace("__", "_");
                                }
                                property.FastSetValue(item, string.Format("{0}__{1}", parentLevel.FastGetValue(item), two));
                            }
                        }
                        else if (config.IsCascadeFilterLevelThree)
                        {
                            var parentLevel = configList.Where(pc => pc.Value.IsCascadeFilterLevelTwo).Select(pc => pc.Key).FirstOrDefault();
                            if (parentLevel != null)
                            {
                                var three = (string)property.FastGetValue(item);
                                if (three != null)
                                {
                                    three = three.Replace("__", "_");
                                }
                                property.FastSetValue(item, string.Format("{0}__{1}", parentLevel.FastGetValue(item), three));
                            }
                        }
                    }

                    foreach (var property in properties)
                    {
                        InternalGetMongodbData(data, typeFullName, item, returnDictionary, property);
                    }
                }

                return(returnDictionary);
            }
            catch (Exception ex)
            {
                LocalLoggingService.Error("{0} {1} {2} {3}", MongodbServiceConfiguration.ModuleName, "GetMongodbData", typeFullName, ex.Message);
                throw;
            }
        }