Пример #1
0
        /// <summary>
        /// 获取配置实体
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static ConfigModel Get(string key = null, string projectName = null, string dbFile = "db.json")
        {
            var list     = new List <ConfigModel>();
            var item     = new ConfigModel();
            var cacheKey = "FastData.Core.Config";

            if (DbCache.Exists(CacheType.Web, cacheKey))
            {
                list = DbCache.Get <List <ConfigModel> >(CacheType.Web, cacheKey);
            }
            else if (projectName != null)
            {
                var assembly = Assembly.Load(projectName);
                using (var resource = assembly.GetManifestResourceStream(string.Format("{0}.{1}", projectName, dbFile)))
                {
                    if (resource != null)
                    {
                        using (var reader = new StreamReader(resource))
                        {
                            var content = reader.ReadToEnd();
                            list = BaseJson.JsonToList <ConfigModel>(BaseJson.ModelToJson(BaseJson.JsonToDic(content).GetValue("DataConfig")));
                            list.ForEach(a => { a.IsUpdateCache = false; });
                            DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list);
                        }
                    }
                    else
                    {
                        list = BaseConfig.GetListValue <ConfigModel>(AppSettingKey.Config, dbFile);
                        DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list);
                    }
                }
            }
            else
            {
                list = BaseConfig.GetListValue <ConfigModel>(AppSettingKey.Config, dbFile);
                DbCache.Set <List <ConfigModel> >(CacheType.Web, cacheKey, list);
            }

            if (string.IsNullOrEmpty(key))
            {
                item = list[0];
            }
            else
            {
                item = list.Find(a => a.Key.ToLower() == key.ToLower());
            }

            if (item.DesignModel == "")
            {
                item.DesignModel = Config.DbFirst;
            }

            if (item.SqlErrorType == "")
            {
                item.SqlErrorType = SqlErrorType.File;
            }

            if (item.CacheType == "")
            {
                item.CacheType = CacheType.Web;
            }

            item.IsPropertyCache = true;
            item.DbType          = item.DbType.ToLower();

            if (projectName != null)
            {
                item.IsUpdateCache = false;
            }
            return(item);
        }
Пример #2
0
        public static void InstanceMapResource(string dbKey = null, string dbFile = "db.json", string mapFile = "map.json")
        {
            var projectName = Assembly.GetCallingAssembly().GetName().Name;
            var config      = DataConfig.Get(dbKey, projectName, dbFile);
            var db          = new DataContext(dbKey);
            var assembly    = Assembly.Load(projectName);
            var map         = new MapConfigModel();

            using (var resource = assembly.GetManifestResourceStream(string.Format("{0}.{1}", projectName, mapFile)))
            {
                if (resource != null)
                {
                    using (var reader = new StreamReader(resource))
                    {
                        var content = reader.ReadToEnd();
                        map.Path = BaseJson.JsonToModel <List <string> >(BaseJson.JsonToDic(BaseJson.ModelToJson(BaseJson.JsonToDic(content).GetValue(AppSettingKey.Map))).GetValue("Path").ToStr());
                    }
                }
                else
                {
                    map = BaseConfig.GetValue <MapConfigModel>(AppSettingKey.Map, mapFile);
                }
            }

            if (map.Path == null)
            {
                return;
            }

            map.Path.ForEach(a =>
            {
                using (var resource = assembly.GetManifestResourceStream(string.Format("{0}.{1}", projectName, a.Replace("/", "."))))
                {
                    var xml = "";
                    if (resource != null)
                    {
                        using (var reader = new StreamReader(resource))
                        {
                            xml = reader.ReadToEnd();
                        }
                    }
                    var info = new FileInfo(a);
                    var key  = BaseSymmetric.Generate(info.FullName);
                    if (!DbCache.Exists(config.CacheType, key))
                    {
                        var temp       = new MapXmlModel();
                        temp.LastWrite = info.LastWriteTime;
                        temp.FileKey   = MapXml.ReadXml(info.FullName, config, info.Name.ToLower().Replace(".xml", ""), xml);
                        temp.FileName  = info.FullName;
                        if (MapXml.SaveXml(dbKey, key, info, config, db))
                        {
                            DbCache.Set <MapXmlModel>(config.CacheType, key, temp);
                        }
                    }
                    else if ((DbCache.Get <MapXmlModel>(config.CacheType, key).LastWrite - info.LastWriteTime).Milliseconds != 0)
                    {
                        DbCache.Get <MapXmlModel>(config.CacheType, key).FileKey.ForEach(f => { DbCache.Remove(config.CacheType, f); });

                        var model       = new MapXmlModel();
                        model.LastWrite = info.LastWriteTime;
                        model.FileKey   = MapXml.ReadXml(info.FullName, config, info.Name.ToLower().Replace(".xml", ""), xml);
                        model.FileName  = info.FullName;
                        if (MapXml.SaveXml(dbKey, key, info, config, db))
                        {
                            DbCache.Set <MapXmlModel>(config.CacheType, key, model);
                        }
                    }
                }
            });
        }