Ejemplo n.º 1
0
        public static TValueType GetValue <TValueType>(Expression <Func <TValueType> > expression)
        {
            if (expression == null)
            {
                return(default(TValueType));
            }

            string assembly = ((MemberExpression)expression.Body)?.Member?.ReflectedType?.Assembly?.GetName()?.Name;

            if (string.IsNullOrWhiteSpace(assembly))
            {
                return(default(TValueType));
            }

            string propertyName = ((MemberExpression)expression.Body).Member.Name;

            if (string.IsNullOrWhiteSpace(propertyName))
            {
                return(default(TValueType));
            }

            string     fullSettingName = $"{assembly}.{propertyName}";
            TValueType defaultValue    = expression.Compile()();

            DbAppSettingDto placeHolderDto = new DbAppSettingDto
            {
                Key   = fullSettingName,
                Type  = typeof(TValueType).ToString(),
                Value = InternalDbAppSettingBase.ConvertValueToString(typeof(TValueType).ToString(), defaultValue),
            };

            TValueType value = SettingCache.GetDbAppSettingValue <TValueType>(placeHolderDto);

            return(value);
        }
Ejemplo n.º 2
0
        public void ConvertJsonToStringCollection()
        {
            Random randNum = new Random();

            int[] randomInts = Enumerable
                               .Repeat(0, 1000)
                               .Select(i => randNum.Next(0, int.MaxValue))
                               .ToArray();

            StringCollection inputStringCollection = new StringCollection();

            inputStringCollection.AddRange(randomInts.Select(t => t.ToString()).ToArray());
            Assert.IsTrue(inputStringCollection.Count == randomInts.Length);

            string result = InternalDbAppSettingBase.ConvertStringCollectionToJson(inputStringCollection);

            Assert.IsNotNull(result);

            StringCollection resultStringCollection = InternalDbAppSettingBase.ConvertJsonToStringCollection(result);

            Assert.IsNotNull(resultStringCollection);

            List <string> inputToList  = inputStringCollection.Cast <string>().ToList();
            List <string> resultToList = resultStringCollection.Cast <string>().ToList();

            Assert.IsTrue(inputToList.SequenceEqual(resultToList));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Check the if the cache contains the value from the dictionary and return the up to date value if so
        /// </summary>
        /// <param name="dbAppSetting"></param>
        internal void HydrateSettingFromDto(InternalDbAppSettingBase dbAppSetting)
        {
            //Check if setting exists without locking
            DbAppSettingDto outDto;

            if (SettingDtosByKey.TryGetValue(dbAppSetting.FullSettingName, out outDto))
            {
                dbAppSetting.From(outDto);
                return;
            }

            //Check if setting exists with locking
            lock (Lock)
            {
                if (SettingDtosByKey.ContainsKey(dbAppSetting.FullSettingName))
                {
                    DbAppSettingDto settingDto = SettingDtosByKey[dbAppSetting.FullSettingName];
                    dbAppSetting.From(settingDto);
                    return;
                }

                //If the setting was not found in the cache, we need to save it to the data access layer
                DbAppSettingDto dbAppSettingDto = dbAppSetting.ToDto();
                SaveNewSettingIfNotExists(dbAppSettingDto);
                dbAppSetting.From(dbAppSettingDto);
            }
        }
Ejemplo n.º 4
0
        public void DbAppSetting_StringCollection()
        {
            List <string> initialStringList = new List <string> {
                "One", "Two", "Three"
            };
            StringCollectionSetting setting = new StringCollectionSetting();

            List <string> initialValueList = setting.InitialValue.Cast <string>().ToList();

            Assert.IsTrue(initialStringList.SequenceEqual(initialValueList));
            List <string> internalValueList = setting.InternalValue.Cast <string>().ToList();

            Assert.IsTrue(initialStringList.SequenceEqual(internalValueList));

            StringCollection newCollection = new StringCollection {
                "t", "3", "2"
            };
            List <string> newStringList = new List <string> {
                "t", "3", "2"
            };
            string jsonNewCollection = InternalDbAppSettingBase.ConvertStringCollectionToJson(newCollection);

            DbAppSettingDto settingDto = new DbAppSettingDto()
            {
                Key = setting.FullSettingName, Value = jsonNewCollection, Type = setting.TypeString
            };

            setting.From(settingDto);

            initialValueList = setting.InitialValue.Cast <string>().ToList();
            Assert.IsTrue(initialStringList.SequenceEqual(initialValueList));
            internalValueList = setting.InternalValue.Cast <string>().ToList();
            Assert.IsTrue(newStringList.SequenceEqual(internalValueList));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the cached value for the setting key regardless of the concrete class
        /// </summary>
        /// <typeparam name="TValueType"></typeparam>
        /// <param name="dbAppSettingDto"></param>
        /// <returns></returns>
        public TValueType GetDbAppSettingValue <TValueType>(DbAppSettingDto dbAppSettingDto)
        {
            if (dbAppSettingDto == null)
            {
                return(default(TValueType));
            }

            dbAppSettingDto = GetValueFromCache(dbAppSettingDto);

            return(dbAppSettingDto != null
                ? InternalDbAppSettingBase.GetValueFromString <TValueType>(dbAppSettingDto.Type, dbAppSettingDto.Value)
                : default(TValueType));
        }