.NET Configuration (app.config/web.config/settings.settings)

后端 未结 13 1316
广开言路
广开言路 2020-11-29 14:41

I have a .NET application which has different configuration files for Debug and Release builds. E.g. the debug app.config file points to a development SQL Server which has d

13条回答
  •  广开言路
    2020-11-29 15:36

    It says asp.net above, so why not save your settings in the database and use a custom-cache to retrieve them?

    The reason we did it because it's easier (for us) to update the continuously database than it is to get permission to continuously update production files.

    Example of a Custom Cache:

    public enum ConfigurationSection
    {
        AppSettings
    }
    
    public static class Utility
    {
        #region "Common.Configuration.Configurations"
    
        private static Cache cache = System.Web.HttpRuntime.Cache;
    
        public static String GetAppSetting(String key)
        {
            return GetConfigurationValue(ConfigurationSection.AppSettings, key);
        }
    
        public static String GetConfigurationValue(ConfigurationSection section, String key)
        {
            Configurations config = null;
    
            if (!cache.TryGetItemFromCache(out config))
            {
                config = new Configurations();
                config.List(SNCLavalin.US.Common.Enumerations.ConfigurationSection.AppSettings);
                cache.AddToCache(config, DateTime.Now.AddMinutes(15));
            }
    
            var result = (from record in config
                          where record.Key == key
                          select record).FirstOrDefault();
    
            return (result == null) ? null : result.Value;
        }
    
        #endregion
    }
    
    namespace Common.Configuration
    {
        public class Configurations : List
        {
            #region CONSTRUCTORS
    
            public Configurations() : base()
            {
                initialize();
            }
            public Configurations(int capacity) : base(capacity)
            {
                initialize();
            }
            public Configurations(IEnumerable collection) : base(collection)
            {
                initialize();
            }
    
            #endregion
    
            #region PROPERTIES & FIELDS
    
            private Crud _crud; // Db-Access layer
    
            #endregion
    
            #region EVENTS
            #endregion
    
            #region METHODS
    
            private void initialize()
            {
                _crud = new Crud(Utility.ConnectionName);
            }
    
            /// 
            /// Lists one-to-many records.
            /// 
            public Configurations List(ConfigurationSection section)
            {
                using (DbCommand dbCommand = _crud.Db.GetStoredProcCommand("spa_LIST_MyConfiguration"))
                {
                    _crud.Db.AddInParameter(dbCommand, "@Section", DbType.String, section.ToString());
    
                    _crud.List(dbCommand, PopulateFrom);
                }
    
                return this;
            }
    
            public void PopulateFrom(DataTable table)
            {
                this.Clear();
    
                foreach (DataRow row in table.Rows)
                {
                    Configuration instance = new Configuration();
                    instance.PopulateFrom(row);
                    this.Add(instance);
                }
            }
    
            #endregion
        }
    
        public class Configuration
        {
            #region CONSTRUCTORS
    
            public Configuration()
            {
                initialize();
            }
    
            #endregion
    
            #region PROPERTIES & FIELDS
    
            private Crud _crud;
    
            public string Section { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
    
            #endregion
    
            #region EVENTS
            #endregion
    
            #region METHODS
    
            private void initialize()
            {
                _crud = new Crud(Utility.ConnectionName);
                Clear();
            }
    
            public void Clear()
            {
                this.Section = "";
                this.Key = "";
                this.Value = "";
            }
            public void PopulateFrom(DataRow row)
            {
                Clear();
    
                this.Section = row["Section"].ToString();
                this.Key = row["Key"].ToString();
                this.Value = row["Value"].ToString();
            }
    
            #endregion
        }
    }
    

提交回复
热议问题