Quantcast
Channel: Fraction of the Blogosphere » custom
Viewing all articles
Browse latest Browse all 2

Custom Web.Config Wrapper Class ASP .NET

$
0
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace AIS.Common
{
    public class ConfigValues
    {
        #region appSettings
        public static string SomeStringOne { get { return getval("SomeStringOne "); } }
        public static string SomeStringTwo { get { return getval("SomeStringTwo "); } }
        public static string Env { get { return getval("env"); } } //keep in mind case sensitivity
        public static string LastManualRefresh { get { return getval("date_last_manual_refresh"); } } //useful for manual site refresh/reload
        public static double SomeDouble { get { return Convert.ToDouble(getval("some_static_double")); } }
        #endregion

        #region connectionStrings - update web.config env variable to toggle between dev and prd
        public static string YourDBOneConnectionString { get { return getcstr("win_web_db"); } }

//also read only implementation like above, but illustrates environment variable usage specific in web.config useful if you have many environments
        public static string YourDBTwoConnectionString 
        {
            get
            {
                if (Env.ToLower().ToString() != "filesystem")
                {
                    return getcstr("static_string" + Env.ToLower().ToString());
                }
                return "";
            }
        }
        #endregion

        /// <summary>
        /// Retrieve Connection String for specified provided key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string getcstr(string key)
        {
            try
            {
                return ConfigurationManager.ConnectionStrings[key].ConnectionString;
            }
            catch (Exception ex)
            {
                Shared.HandleError(ex); //TODO: change to throw error event handle instead of direct call for reusability
                return "Error retrieving value";
            }
        }

        /// <summary>
        /// Retrieve appSettings value for provided specified key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static string getval(string key)
        {
            try
            {
                return ConfigurationManager.AppSettings[key];
            }
            catch (Exception ex)
            {
                Shared.HandleError(ex); //TODO: change to throw error event handle instead of direct call for reusability
                return "Error retrieving value";
            }
        }
    }
}


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images