/// <summary> /// used to update existing values if they need to change during runtime via funct call (value returned) /// </summary> /// <param name="requestedConfigurationName"></param> /// <param name="valueFactory"></param> public dynamic SetValue <T>(string requestedConfigurationName, Func <T> valueFactory, bool isUpdateDatabase = false, long?applicationId = null) { applicationId ??= _currentApplicationId; VerifyConfigDataLoaded()[applicationId.Value].AddOrUpdate(requestedConfigurationName, valueFactory.Invoke(), (Func <string, dynamic, dynamic>)((key, existingValue) => valueFactory.Invoke())); if (isUpdateDatabase == true) { OperationalConfiguration currentWorkingObject = _repositoryOperationalConfiguration.GetAll().ItemCollection.Where(singleConfig => singleConfig.PropertyName == requestedConfigurationName).FirstOrDefault(); if (currentWorkingObject == null) { currentWorkingObject = new OperationalConfiguration(); currentWorkingObject.PropertyName = requestedConfigurationName; currentWorkingObject.Description = requestedConfigurationName; currentWorkingObject.IsEnabled = true; currentWorkingObject.IsConnectionString = false; _repositoryOperationalConfiguration.Insert(currentWorkingObject); } AssignValueToCorrectProperty(GetValue(requestedConfigurationName), currentWorkingObject); _repositoryOperationalConfiguration.Update(currentWorkingObject); } return(VerifyConfigDataLoaded()[applicationId.Value][requestedConfigurationName]); }
/// <summary> /// deletes the requested setting value /// </summary> /// <param name="requestedConfigurationName"></param> /// <returns></returns> public void DeleteValue(string requestedConfigurationName, bool isUpdateDatabase = false, long?applicationId = null) { applicationId ??= _currentApplicationId; if (VerifyConfigDataLoaded()[applicationId.Value].ContainsKey(requestedConfigurationName)) { VerifyConfigDataLoaded()[applicationId.Value].TryRemove(requestedConfigurationName, out _); } if (isUpdateDatabase == true) { OperationalConfiguration currentWorkingObject = _repositoryOperationalConfiguration.GetAll().ItemCollection.Where(singleConfig => singleConfig.PropertyName == requestedConfigurationName).FirstOrDefault(); if (currentWorkingObject != null) { _repositoryOperationalConfiguration.Delete(currentWorkingObject); } } }
/// <summary> /// used to update existing values if they need to change during runtime /// </summary> /// <param name="requestedConfigurationName"></param> /// <param name="newAssignmentValue"></param> public void SetConnectionString(string requestedConfigurationName, string newAssignmentValue, bool isUpdateDatabase = false, long?applicationId = null) { applicationId ??= _currentApplicationId; VerifyConfigDataLoaded()[applicationId.Value]["ConnectionStrings"].AddOrUpdate(requestedConfigurationName, newAssignmentValue, (Func <string, dynamic, dynamic>)((key, existingValue) => newAssignmentValue)); if (isUpdateDatabase == true) { OperationalConfiguration currentWorkingObject = _repositoryOperationalConfiguration.GetAll().ItemCollection.Where(singleConfig => singleConfig.PropertyName == requestedConfigurationName).FirstOrDefault(); if (currentWorkingObject == null) { currentWorkingObject = new OperationalConfiguration(); currentWorkingObject.PropertyName = requestedConfigurationName; currentWorkingObject.Description = requestedConfigurationName; currentWorkingObject.IsEnabled = true; currentWorkingObject.IsConnectionString = true; _repositoryOperationalConfiguration.Insert(currentWorkingObject); } currentWorkingObject.StringValue = newAssignmentValue; _repositoryOperationalConfiguration.Update(currentWorkingObject); } }
/// <summary> /// assign the value to the correct property based on its data type /// </summary> /// <param name="newAssignmentValue"></param> /// <param name="currentWorkingObject"></param> private void AssignValueToCorrectProperty(dynamic newAssignmentValue, OperationalConfiguration currentWorkingObject) { currentWorkingObject.BoolValue = null; currentWorkingObject.DateValue = null; currentWorkingObject.DecimalValue = null; currentWorkingObject.GuidValue = null; currentWorkingObject.IntegerValue = null; currentWorkingObject.LongValue = null; currentWorkingObject.StringValue = null; switch (Type.GetTypeCode(newAssignmentValue.GetType())) { case TypeCode.Boolean: { currentWorkingObject.BoolValue = Convert.ChangeType(newAssignmentValue, typeof(bool)); break; } case TypeCode.DateTime: { currentWorkingObject.DateValue = Convert.ChangeType(newAssignmentValue, typeof(DateTime)); break; } case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: { currentWorkingObject.DecimalValue = Convert.ChangeType(newAssignmentValue, typeof(decimal)); break; } case TypeCode.Object: { if (newAssignmentValue.GetType() == typeof(Guid)) { currentWorkingObject.GuidValue = Convert.ChangeType(newAssignmentValue, typeof(Guid)); } break; } case TypeCode.Byte: case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.UInt16: case TypeCode.UInt32: { currentWorkingObject.IntegerValue = Convert.ChangeType(newAssignmentValue, typeof(int)); break; } case TypeCode.Int64: case TypeCode.UInt64: { currentWorkingObject.LongValue = Convert.ChangeType(newAssignmentValue, typeof(long)); break; } case TypeCode.String: case TypeCode.Char: default: { currentWorkingObject.StringValue = Convert.ChangeType(newAssignmentValue, typeof(string), new CultureInfo("en-US")); break; } } }