Ejemplo n.º 1
0
        public void AddOrUpdateValue <T>(string key, T value, SettingStrategy strategy = SettingStrategy.Local, string containerName = "")
        {
            _loggingService.WriteLine($"Adding setting {key} to {strategy.ToString().ToLower()} container {containerName} with value '{value}'.");

            var type = typeof(T);

            if (value != null)
            {
                type = value.GetType();
            }

            var container = new ApplicationDataCompositeValue();
            var converted = JsonConvert.SerializeObject(value, Formatting.None);

            if (converted != null)
            {
                container["Value"] = converted;
            }

            if ((type != typeof(string) && !type.GetTypeInfo().IsValueType) || (type != typeof(T)))
            {
                container["Type"] = type.AssemblyQualifiedName;
            }

            GetContainerValuesForStrategyAndContainerName(strategy, containerName)[key] = container;
        }
Ejemplo n.º 2
0
        public T GetValueOrDefault <T>(string key, T defaultValue = default(T), SettingStrategy strategy = SettingStrategy.Local, string containerName = "")
        {
            _loggingService.WriteLine($"Trying to fetch setting {key} from {strategy.ToString().ToLower()} container {containerName}.");

            try
            {
                var values = GetContainerValuesForStrategyAndContainerName(strategy, containerName);

                if (values.ContainsKey(key))
                {
                    var container = values[key] as ApplicationDataCompositeValue;

                    var type = typeof(T);
                    if (container.ContainsKey("Type"))
                    {
                        type = Type.GetType((string)container["Type"]);
                    }

                    string value = null;
                    if (container.ContainsKey("Value"))
                    {
                        value = container["Value"] as string;
                    }

                    var converted = (T)JsonConvert.DeserializeObject(value, type);

                    _loggingService.WriteLine($"Returning value: {converted}");

                    return(converted);
                }

                _loggingService.WriteLine($"Returning default value: {defaultValue}");
                return(defaultValue);
            }
            catch
            {
                _loggingService.WriteLine($"Returning default value: {defaultValue}");
                return(defaultValue);
            }
        }