예제 #1
0
        public static T GetOrUpdateSetting(string site)
        {
            if (string.IsNullOrEmpty(site))
            {
                site = SiteConst.NNN;
            }

            string cacheType = GetCacheType();
            string cacheKey  = GetCacheKey(site);

            bool isExist = _SettingCache.TryGetValue(cacheKey, out T setting);

            if (!isExist)
            {
                setting = new T();
                _SettingCache.GetOrAdd(cacheKey, setting);
            }

            IReadConfig readConfig = null;

            if (_ReadConfigCache.ContainsKey(cacheType))
            {
                readConfig = _ReadConfigCache[cacheType];
            }

            Invoke(site, setting, readConfig);

            return(_SettingCache[cacheKey]);
        }
예제 #2
0
        /// <summary>
        /// 新增或更新(T)ReadConfig缓存
        /// </summary>
        /// <param name="readConfig">ReadConfig实例</param>
        /// <returns>结果</returns>
        public static bool UpdateOrAddReadConfig(IReadConfig readConfig)
        {
            string cacheType = GetCacheType();

            _ReadConfigCache[cacheType] = readConfig;
            return(true);
        }
예제 #3
0
        /// <summary>
        /// 获取实例(如果不存在,则调用T默认的Invoke初始化)
        /// <typeparamref name="T"/>
        /// 需要先对_ReadConfig赋值
        /// </summary>
        /// <param name="site">站点Site</param>
        /// <returns>获取setting实例</returns>
        public static T GetSetting(string site)
        {
            if (string.IsNullOrEmpty(site))
            {
                site = SiteConst.NNN;
            }

            string cacheType = GetCacheType();
            string cacheKey  = GetCacheKey(site);

            if (!_SettingCache.ContainsKey(cacheKey))
            {
                IReadConfig readConfig = null;
                if (_ReadConfigCache.ContainsKey(cacheType))
                {
                    readConfig = _ReadConfigCache[cacheType];
                }

                T setting = new T();
                Invoke(site, setting, readConfig);
                _SettingCache.GetOrAdd(cacheKey, setting);
            }

            return(_SettingCache[cacheKey]);
        }
예제 #4
0
 public TrolleyCalculatorRepository(IHttpClientDecorator httpClient, ILogger logger, IReadConfig readConfig, ISerializer serializer)
 {
     _client     = httpClient;
     _logger     = logger;
     _readConfig = readConfig;
     _serializer = serializer;
 }
예제 #5
0
 public ProductRepository(IHttpClientDecorator httpClient, ILogger logger, IReadConfig readConfig, IDeserializer deserializer)
 {
     _client       = httpClient;
     _logger       = logger;
     _readConfig   = readConfig;
     _deserializer = deserializer;
 }
예제 #6
0
        public static string GetConfigValue(this string key)
        {
            string res = null;

            try
            {
                IReadConfig irf = CreateReadObj.getReadWay();
                res = irf.getConfigValue(key);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return(res);
        }
예제 #7
0
 public static bool UpdateOrAddReadConfig(IReadConfig readConfig)
 {
     return(SettingFac <DataSetting> .UpdateOrAddReadConfig(readConfig));
 }
예제 #8
0
 public SettingFacTest()
 {
     readConfig = new ReadConfig();
 }
예제 #9
0
        private static T Invoke(string site, T setting, IReadConfig readConfig)
        {
            PropertyInfo[] properties = setting.GetType().GetProperties();
            var            regex      = new Regex(DataConst.SiteParam, RegexOptions.IgnoreCase);

            foreach (PropertyInfo property in properties)
            {
                // Sites标记开关列表,bool类型
                var sitesAttr = property.GetAttribute <SitesAttribute>(false);
                if (sitesAttr != null)
                {
                    if (property.PropertyType == typeof(bool))
                    {
                        var propertyValue = sitesAttr.Sites?.Contains(site);
                        property.SetValue(setting, propertyValue);
                    }

                    continue;
                }

                // 读取外部config配置中的值
                var configAttr = property.GetAttribute <ConfigAttribute>(false);
                if (configAttr != null)
                {
                    if (readConfig != null)
                    {
                        var configPath    = regex.Replace(configAttr.Path ?? string.Empty, site); // 替换Site
                        var propertyValue = readConfig.Get(configPath, property.PropertyType);
                        property.SetValue(setting, propertyValue);
                    }

                    continue;
                }

                // 开关
                var switchAttr = property.GetAttribute <SwitchAttribute>(false);
                if (switchAttr != null)
                {
                    if (readConfig != null)
                    {
                        var configPath    = regex.Replace(switchAttr.Path ?? string.Empty, site); // 替换Site
                        var configValue   = (string)readConfig.Get(configPath, typeof(string));
                        var switchValues  = configValue?.Split(switchAttr.SplitChar.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
                        var propertyValue = switchValues != null?switchValues.Contains(site) : false;

                        property.SetValue(setting, propertyValue);
                    }

                    continue;
                }

                var descAttr = property.GetAttribute <DescAttribute>(false);
                if (descAttr != null)
                {
                    if (property.PropertyType == typeof(string))
                    {
                        var propertyValue = regex.Replace(descAttr.Desc ?? string.Empty, site); // 替换Site
                        property.SetValue(setting, propertyValue);
                    }

                    continue;
                }

                // 都没有的执行默认的私有Set方法
                if (property.SetMethod != null)
                {
                    int      parametersNumber = property.SetMethod.GetParameters().Count();
                    object[] args             = new object[parametersNumber];
                    for (int i = 0; i < parametersNumber; i++)
                    {
                        args[i] = null;
                    }

                    property.SetMethod.Invoke(setting, args);
                }
            }

            return(setting);
        }
예제 #10
0
 public void SetUp()
 {
     _config = new ReadConfig();
 }