Exemple #1
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Saves the settings.
        /// </summary>
        /// <param name="config">
        ///     The config.
        /// </param>
        /// -------------------------------------------------------------------------------------------------

        public void SaveConfig(Config config = null)
        {
            if (config != null)
            {
                this.currentConfig = config;
            }

            using (SqlConfigContext configContext = new SqlConfigContext(this.nameOrConnectionString))
            {
                Dictionary <string, SiteSetting> settings = configContext.SiteSettings.Where(x => x.Name.StartsWith("Config.")).ToDictionary(x => x.Name, x => x);

                foreach (var property in Properties)
                {
                    string[]    propertyAccessors = property.Key.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                    SiteSetting setting;
                    if (!settings.ContainsKey(property.Key))
                    {
                        setting      = new SiteSetting();
                        setting.Name = property.Key;
                        configContext.SiteSettings.Add(setting);
                    }
                    else
                    {
                        setting = settings[property.Key];
                    }

                    string propertyValue = SqlConfigProvider.GetPropertyValue(this.currentConfig, propertyAccessors.Skip(1).ToList());

                    setting.Value = string.IsNullOrWhiteSpace(propertyValue) ? string.Empty : propertyValue;
                }

                configContext.SaveChanges();
            }
        }
Exemple #2
0
        /// -------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Gets the settings.
        /// </summary>
        /// <returns>
        ///     The settings.
        /// </returns>
        /// -------------------------------------------------------------------------------------------------

        public Config GetConfig()
        {
            if (!this.loaded)
            {
                lock (this.syncLock)
                {
                    Dictionary <string, string> settings;
                    using (SqlConfigContext configContext = new SqlConfigContext(this.nameOrConnectionString))
                    {
                        settings = configContext.SiteSettings.Where(x => x.Name.StartsWith("Config.")).ToDictionary(x => x.Name, x => x.Value);
                    }

                    if (settings.Count > 0)
                    {
                        this.currentConfig = new Config();

                        LoadConfig(this.currentConfig, settings);
                    }

                    this.loaded = true;
                }
            }

            return(this.currentConfig);
        }
Exemple #3
0
        private T BuildConfigObject <T>(string key) where T : class, new()
        {
            T configObject = new T();
            Dictionary <string, string> settings;

            using (SqlConfigContext configContext = new SqlConfigContext(this.nameOrConnectionString))
            {
                settings = configContext.SiteSettings.Where(x => x.Name.StartsWith(key + ".")).ToDictionary(x => x.Name, x => x.Value);
            }

            if (settings.Count > 0)
            {
                LoadConfig(configObject, settings);
            }
            return(configObject);
        }
Exemple #4
0
        /// <summary>
        /// Saves the specified configuration object.
        /// </summary>
        /// <typeparam name="T">Type of the object.</typeparam>
        /// <param name="configObject">The configuration object.</param>

        public void Save <T>(T configObject) where T : class, new()
        {
            Type   type       = typeof(T);
            string configName = type.Name;

            using (SqlConfigContext configContext = new SqlConfigContext(this.nameOrConnectionString))
            {
                IList <SiteSetting> siteSettings = configContext.SiteSettings.Where(x => x.Name.StartsWith(configName + ".")).ToList();
                if (configObject == null)
                {
                    foreach (var siteSetting in siteSettings)
                    {
                        configContext.SiteSettings.Remove(siteSetting);
                    }
                }
                else
                {
                    Dictionary <string, SiteSetting> settings = siteSettings.ToDictionary(x => x.Name, x => x);

                    foreach (var property in Build(type, configName))
                    {
                        string[]    propertyAccessors = property.Key.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                        SiteSetting setting;
                        if (!settings.ContainsKey(property.Key))
                        {
                            setting      = new SiteSetting();
                            setting.Name = property.Key;
                            configContext.SiteSettings.Add(setting);
                        }
                        else
                        {
                            setting = settings[property.Key];
                        }

                        string propertyValue = SqlConfigProvider.GetPropertyValue(configObject, propertyAccessors.Skip(1).ToList());

                        setting.Value = string.IsNullOrWhiteSpace(propertyValue) ? string.Empty : propertyValue;
                    }
                }

                configContext.SaveChanges();
            }
            this.extraConfigs.AddOrUpdate(configName, configObject, (k, obj) => configObject);
        }