Beispiel #1
0
        /// <summary>
        ///     Gets the setting with the provided name. If the setting does not exist yet, it is created.
        /// </summary>
        /// <typeparam name="T">The type of the setting, can be any serializable type</typeparam>
        /// <param name="name">The name of the setting</param>
        /// <param name="defaultValue">The default value to use if the setting does not exist yet</param>
        /// <returns></returns>
        public PluginSetting <T> GetSetting <T>(string name, T defaultValue = default)
        {
            lock (_settingEntities)
            {
                // Return cached value if available
                if (_settingEntities.ContainsKey(name))
                {
                    return((PluginSetting <T>)_settingEntities[name]);
                }
                // Try to find in database
                var settingEntity = _pluginSettingRepository.GetByNameAndPluginGuid(name, _pluginInfo.Guid);
                // If not found, create a new one
                if (settingEntity == null)
                {
                    settingEntity = new PluginSettingEntity {
                        Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)
                    };
                    _pluginSettingRepository.Add(settingEntity);
                }

                var pluginSetting = new PluginSetting <T>(_pluginInfo, _pluginSettingRepository, settingEntity);
                _settingEntities.Add(name, pluginSetting);
                return(pluginSetting);
            }
        }
Beispiel #2
0
        public PluginSetting <T> GetSetting <T>(string name, T defaultValue = default(T))
        {
            if (_settingEntities.ContainsKey(name))
            {
                return(new PluginSetting <T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]));
            }

            var settingEntity = new PluginSettingEntity {
                Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)
            };

            _pluginSettingRepository.Add(settingEntity);
            _pluginSettingRepository.Save();

            _settingEntities.Add(name, settingEntity);
            return(new PluginSetting <T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]));
        }