/// <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); } }
public ActionResult SaveSetting(DbAppSettingModel model) { if (model == null) { throw new ValidationException("model cannot be null"); } DbAppSettingDto toSave = model.ToDto(); bool isValid = _dbAppSettingMaintenanceService.ValidateValueForType(model.Value, model.Type); if (!isValid) { return new JsonResult() { Data = false } } ; _dbAppSettingMaintenanceService.SaveDbAppSetting(HttpContext.Session.SessionID, toSave); return(new JsonResult() { Data = true }); }
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)); }
public void DbAppSetting_customObect() { MyTestClassSetting setting = new MyTestClassSetting(); Assert.IsTrue(setting.InitialValue.SomeProperty == 1); Assert.IsTrue(setting.InitialValue.SomeOtherProperty == "Test"); Assert.IsTrue(setting.InternalValue.SomeProperty == 1); Assert.IsTrue(setting.InternalValue.SomeOtherProperty == "Test"); MyTestClass testClass = new MyTestClass(); testClass.SomeProperty = 2; testClass.SomeOtherProperty = "Test2"; string jsonTestClass = new JavaScriptSerializer().Serialize(testClass); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = setting.FullSettingName, Value = jsonTestClass, Type = setting.TypeString }; setting.From(settingDto); Assert.IsTrue(setting.InitialValue.SomeProperty == 1); Assert.IsTrue(setting.InitialValue.SomeOtherProperty == "Test"); Assert.IsTrue(setting.InternalValue.SomeProperty == 2); Assert.IsTrue(setting.InternalValue.SomeOtherProperty == "Test2"); DbAppSettingDto toDto = setting.ToDto(); Assert.IsTrue(toDto.ApplicationKey == settingDto.ApplicationKey); Assert.IsTrue(toDto.Key == settingDto.Key); Assert.IsTrue(toDto.Type == settingDto.Type); Assert.IsTrue(toDto.Value.Equals(settingDto.Value, StringComparison.InvariantCultureIgnoreCase)); }
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); }
public DbAppSettingDto GetDbAppSetting(DbAppSettingDto dbAppSettingDto) { if (CachedKeys.ContainsKey(dbAppSettingDto.Key)) { return(CachedKeys[dbAppSettingDto.Key]); } return(null); }
public DbAppSettingDto GetDbAppSetting(DbAppSettingDto dbAppSettingDto) { GetDbAppSettingHitCount++; DbAppSettingDto dto = new DbAppSettingTestSetting().ToDto(); dto.Value = "100"; return(dto); }
public void DeleteDbAppSetting(string sessionId, DbAppSettingDto dto) { Dictionary <string, DbAppSettingDto> settingsByKey = GetOrCreateSessionSettings(sessionId); if (settingsByKey.ContainsKey(dto.Key)) { settingsByKey.Remove(dto.Key); } }
public void Test() { DbAppSettingDto dto = new DbAppSettingDto() { Key = "MyAssembly.Key" }; Assert.IsTrue(dto.Key == "MyAssembly.Key"); Assert.IsTrue(dto.Assembly == "MyAssembly"); }
public void GetDbAppSetting() { DefaultLazyLoadSettingDao dao = new DefaultLazyLoadSettingDao(); Assert.IsNotNull(dao); DbAppSettingDto results = dao.GetDbAppSetting(new DbAppSettingDto()); Assert.IsNull(results); }
public static DbAppSettingModel FromDto(DbAppSettingDto dto) { return(new DbAppSettingModel { Application = dto.ApplicationKey, Assembly = dto.Assembly, Key = dto.Key, Type = dto.Type, Value = dto.Value, }); }
internal static TValueType GetDbAppSettingValue <TValueType>(DbAppSettingDto dbAppSettingDto) { if (dbAppSettingDto == null) { return(default(TValueType)); } Instance.SettingCacheProvider.IntializationCheck(); return(Instance.SettingCacheProvider.GetDbAppSettingValue <TValueType>(dbAppSettingDto)); }
public void SaveNewSettingIfNotExists(DbAppSettingDto dbAppSettingDto) { if (MySettingClassLazyLoadSettingDao.CachedKeys.ContainsKey(dbAppSettingDto.Key)) { MySettingClassLazyLoadSettingDao.CachedKeys[dbAppSettingDto.Key] = dbAppSettingDto; } else { MySettingClassLazyLoadSettingDao.CachedKeys.Add(dbAppSettingDto.Key, dbAppSettingDto); } }
private Dictionary <string, DbAppSettingDto> CreateSettings() { Dictionary <string, DbAppSettingDto> settingsByKey = new Dictionary <string, DbAppSettingDto>(); DbAppSettingDto dto1 = new DbAppSettingDto() { Key = new DemoDbAppSettings.DemoDbAppSettingBool().FullSettingName, Value = "2", Type = typeof(int).FullName, ApplicationKey = "DbAppSettingApp" }; DbAppSettingDto dto2 = new DbAppSettingDto() { Key = $"AnotherAssembly.{new DemoDbAppSettings.DemoDbAppSettingInt().SettingName}", Value = "NEW TEST", Type = typeof(string).FullName, ApplicationKey = "DbAppSettingApp" }; DbAppSettingDto dto3 = new DbAppSettingDto() { Key = $"AnotherAssembly.{new DemoDbAppSettings.DemoDbAppSettingString().SettingName}", Value = "true", Type = typeof(bool).FullName, ApplicationKey = "DbAppSettingApp" }; settingsByKey.Add(dto1.Key, dto1); settingsByKey.Add(dto2.Key, dto2); settingsByKey.Add(dto3.Key, dto3); DbAppSettingDto dto4 = new DbAppSettingDto() { Key = new DemoDbAppSettings.DemoSecondAppDbAppSettingBool().FullSettingName, Value = "2", Type = typeof(int).FullName, ApplicationKey = "SecondApp" }; DbAppSettingDto dto5 = new DbAppSettingDto() { Key = new DemoDbAppSettings.DemoSecondDbAppSettingInt().FullSettingName, Value = "NEW TEST", Type = typeof(string).FullName, ApplicationKey = "SecondApp" }; DbAppSettingDto dto6 = new DbAppSettingDto() { Key = new DemoDbAppSettings.DemoSecondDbAppSettingString().FullSettingName, Value = "true", Type = typeof(bool).FullName, ApplicationKey = "SecondApp" }; settingsByKey.Add(dto4.Key, dto4); settingsByKey.Add(dto5.Key, dto5); settingsByKey.Add(dto6.Key, dto6); List <DbAppSettingDto> allTypeDtos = new List <DbAppSettingDto> { new DemoDbAppSettings.DemoAnotherAppDbAppSettingBool().ToDto(), new DemoDbAppSettings.DemoAnotherAppDbAppSettingByte().ToDto(), new DemoDbAppSettings.DemoAnotherAppDbAppSettingChar().ToDto(), new DemoDbAppSettings.DemoAnotherAppDbAppSettingDecimal().ToDto() }; foreach (DbAppSettingDto dto in allTypeDtos) { dto.ApplicationKey = "Another App"; settingsByKey.Add(dto.Key, dto); } return(settingsByKey); }
/// <summary> /// Internal method used to convert the domain into the dto representation /// </summary> /// <returns></returns> internal override DbAppSettingDto ToDto() { DbAppSettingDto dpAppSettingDto = new DbAppSettingDto { ApplicationKey = ApplicationKey, Key = FullSettingName, Type = TypeString, Value = ConvertValueToString(TypeString, InternalValue), }; return(dpAppSettingDto); }
/// <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)); }
public void SaveDbAppSetting(string sessionId, DbAppSettingDto dto) { Dictionary <string, DbAppSettingDto> settingsByKey = GetOrCreateSessionSettings(sessionId); if (settingsByKey.ContainsKey(dto.Key)) { settingsByKey[dto.Key] = dto; } else { settingsByKey.Add(dto.Key, dto); } }
/// <summary> /// Specific method that hydrates a setting from the dto provided by the data access layer /// </summary> /// <param name="dto"></param> internal override void From(DbAppSettingDto dto) { //Application key ApplicationKey = dto.ApplicationKey; //Type string _typeString = dto.Type; //String value string value = dto.Value; //Get the internal value InternalValue = GetValueFromString <TValueType>(TypeString, value); //Let the class know that it was hydrated from the data access layer HydratedFromDataAccess = true; }
public ActionResult RemoveSetting(DbAppSettingModel model) { if (model == null) { throw new ValidationException("model cannot be null"); } DbAppSettingDto toRemove = model.ToDto(); //TODO: Validate _dbAppSettingMaintenanceService.DeleteDbAppSetting(HttpContext.Session.SessionID, toRemove); return(new JsonResult() { Data = true }); }
public void SaveNewSettingIfNotExists() { DummySaveNewSettingDao dao = new DummySaveNewSettingDao(); DummySettingCacheProvider4 provider = new DummySettingCacheProvider4(new DummyCacheManagerArguments() { SaveNewSettingDao = dao, CacheRefreshTimeout = () => TimeSpan.FromMilliseconds(0) }); Assert.IsNull(SettingCacheProviderBase.LastRefreshedTime); DbAppSettingTestSetting domain = new DbAppSettingTestSetting(); DbAppSettingDto dto = domain.ToDto(); provider.SaveNewSettingIfNotExists(dto); Assert.IsTrue(dao.SaveNewSettingIfNotExistsHitCount == 1); Assert.IsTrue(SettingCacheProviderBase.SettingDtosByKey.Count == 1); }
/// <summary> /// Save the settign to the data access layer /// </summary> /// <param name="dbAppSettingDto"></param> internal virtual void SaveNewSettingIfNotExists(DbAppSettingDto dbAppSettingDto) { lock (Lock) { try { //If the setting is no found in the cache, save the setting ManagerArguments.SaveNewSettingDao.SaveNewSettingIfNotExists(dbAppSettingDto); //If the setting was just added to the data access layer, add it to the dto cache as well SettingDtosByKey.AddOrUpdate(dbAppSettingDto.Key, dbAppSettingDto, (key, oldValue) => dbAppSettingDto); } catch (Exception e) { //TODO: Log manager //cacheManager.NotifyOfException(e); } } }
public void HydrateSettingFromDto() { DummySettingCacheProvider3 provider = new DummySettingCacheProvider3(new DummyCacheManagerArguments() { CacheRefreshTimeout = () => TimeSpan.FromMilliseconds(0) }); Assert.IsNull(SettingCacheProviderBase.LastRefreshedTime); DbAppSettingTestSetting domain = new DbAppSettingTestSetting(); DbAppSettingDto dto = domain.ToDto(); provider.SetSettingValues(new List <DbAppSettingDto> { dto }); Assert.IsTrue(SettingCacheProviderBase.SettingDtosByKey.Count == 1); provider.HydrateSettingFromDto(domain); Assert.IsTrue(domain.HydratedFromDataAccess); }
public override DbAppSetting <T, TValueType> GetDbAppSetting <T, TValueType>() { IntializationCheck(); T newSetting = new T(); //Check if setting exists without locking DbAppSettingDto outDto; if (SettingDtosByKey.TryGetValue(newSetting.FullSettingName, out outDto)) { newSetting.From(outDto); return(newSetting); } lock (Lock) { //Check if setting exists with locking if (SettingDtosByKey.ContainsKey(newSetting.FullSettingName)) { DbAppSettingDto settingDto = SettingDtosByKey[newSetting.FullSettingName]; newSetting.From(settingDto); return(newSetting); } //If setting does not exist yet, go get it from the database DbAppSettingDto updatedSettingDto = _managerArguments.LazyLoadSettingDao.GetDbAppSetting(newSetting.ToDto()); if (updatedSettingDto != null) { SetSettingValues(new List <DbAppSettingDto> { updatedSettingDto }); } HydrateSettingFromDto(newSetting); return(newSetting); } }
public void DbAppSetting_From() { DbAppSettingTestSetting setting = GetSetting(); Assert.IsNotNull(setting.InitialValue); Assert.IsTrue(setting.InitialValue == 1); Assert.IsNotNull(setting.InternalValue); Assert.IsTrue(setting.InternalValue == 1); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = "DbAppSettingTest1", Value = "2", Type = typeof(int).FullName }; setting.From(settingDto); Assert.IsNotNull(setting.InitialValue); Assert.IsTrue(setting.InitialValue == 1); Assert.IsNotNull(setting.InternalValue); Assert.IsTrue(setting.InternalValue == 2); }
/// <summary> /// Check the if the cache contains the value from the dictionary and return the up to date value if so /// </summary> /// <param name="placeHolderDbAppSettingDto"></param> internal DbAppSettingDto GetValueFromCache(DbAppSettingDto placeHolderDbAppSettingDto) { //Check if setting exists without locking DbAppSettingDto outDto; if (SettingDtosByKey.TryGetValue(placeHolderDbAppSettingDto.Key, out outDto)) { return(outDto); } //Check if setting exists with locking lock (Lock) { if (SettingDtosByKey.ContainsKey(placeHolderDbAppSettingDto.Key)) { return(SettingDtosByKey[placeHolderDbAppSettingDto.Key]); } //If the setting was not found in the cache, we need to save it to the data access layer SaveNewSettingIfNotExists(placeHolderDbAppSettingDto); return(placeHolderDbAppSettingDto); } }
public void DbAppSetting_Guid() { GuidSetting setting = new GuidSetting(); Assert.IsTrue(setting.InitialValue == new Guid("9245fe4a-d402-451c-b9ed-9c1a04247482")); Assert.IsTrue(setting.InternalValue == new Guid("9245fe4a-d402-451c-b9ed-9c1a04247482")); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = setting.FullSettingName, Value = new Guid("1245fe4a-d402-451c-b9ed-9c1a04247482").ToString(), Type = setting.TypeString }; setting.From(settingDto); Assert.IsTrue(setting.InitialValue == new Guid("9245fe4a-d402-451c-b9ed-9c1a04247482")); Assert.IsTrue(setting.InternalValue == new Guid("1245fe4a-d402-451c-b9ed-9c1a04247482")); DbAppSettingDto toDto = setting.ToDto(); Assert.IsTrue(toDto.ApplicationKey == settingDto.ApplicationKey); Assert.IsTrue(toDto.Key == settingDto.Key); Assert.IsTrue(toDto.Type == settingDto.Type); Assert.IsTrue(toDto.Value.Equals(settingDto.Value, StringComparison.InvariantCultureIgnoreCase)); }
public void DbAppSetting_byte() { byteSetting setting = new byteSetting(); Assert.IsTrue(setting.InitialValue == Encoding.ASCII.GetBytes("test")[0]); Assert.IsTrue(setting.InternalValue == Encoding.ASCII.GetBytes("test")[0]); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = setting.FullSettingName, Value = Encoding.ASCII.GetBytes("test")[1].ToString(), Type = setting.TypeString }; setting.From(settingDto); Assert.IsTrue(setting.InitialValue == Encoding.ASCII.GetBytes("test")[0]); Assert.IsTrue(setting.InternalValue == Encoding.ASCII.GetBytes("test")[1]); DbAppSettingDto toDto = setting.ToDto(); Assert.IsTrue(toDto.ApplicationKey == settingDto.ApplicationKey); Assert.IsTrue(toDto.Key == settingDto.Key); Assert.IsTrue(toDto.Type == settingDto.Type); Assert.IsTrue(toDto.Value.Equals(settingDto.Value, StringComparison.InvariantCultureIgnoreCase)); }
public void DbAppSetting_ulong() { ulongSetting setting = new ulongSetting(); Assert.IsTrue(setting.InitialValue == 18446744073709551615); Assert.IsTrue(setting.InternalValue == 18446744073709551615); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = setting.FullSettingName, Value = 18446744073709551614.ToString(), Type = setting.TypeString }; setting.From(settingDto); Assert.IsTrue(setting.InitialValue == 18446744073709551615); Assert.IsTrue(setting.InternalValue == 18446744073709551614); DbAppSettingDto toDto = setting.ToDto(); Assert.IsTrue(toDto.ApplicationKey == settingDto.ApplicationKey); Assert.IsTrue(toDto.Key == settingDto.Key); Assert.IsTrue(toDto.Type == settingDto.Type); Assert.IsTrue(toDto.Value.Equals(settingDto.Value, StringComparison.InvariantCultureIgnoreCase)); }
public void DbAppSetting_char() { charSetting setting = new charSetting(); Assert.IsTrue(setting.InitialValue == 'a'); Assert.IsTrue(setting.InternalValue == 'a'); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = setting.FullSettingName, Value = 'b'.ToString(), Type = setting.TypeString }; setting.From(settingDto); Assert.IsTrue(setting.InitialValue == 'a'); Assert.IsTrue(setting.InternalValue == 'b'); DbAppSettingDto toDto = setting.ToDto(); Assert.IsTrue(toDto.ApplicationKey == settingDto.ApplicationKey); Assert.IsTrue(toDto.Key == settingDto.Key); Assert.IsTrue(toDto.Type == settingDto.Type); Assert.IsTrue(toDto.Value.Equals(settingDto.Value, StringComparison.InvariantCultureIgnoreCase)); }
public void DbAppSetting_decimal() { decimalSetting setting = new decimalSetting(); Assert.IsTrue(setting.InitialValue == 1.25M); Assert.IsTrue(setting.InternalValue == 1.25M); DbAppSettingDto settingDto = new DbAppSettingDto() { Key = setting.FullSettingName, Value = 5.10M.ToString(), Type = setting.TypeString }; setting.From(settingDto); Assert.IsTrue(setting.InitialValue == 1.25M); Assert.IsTrue(setting.InternalValue == 5.10M); DbAppSettingDto toDto = setting.ToDto(); Assert.IsTrue(toDto.ApplicationKey == settingDto.ApplicationKey); Assert.IsTrue(toDto.Key == settingDto.Key); Assert.IsTrue(toDto.Type == settingDto.Type); Assert.IsTrue(toDto.Value.Equals(settingDto.Value, StringComparison.InvariantCultureIgnoreCase)); }