AppSettings values Usage through Static Class – C#

Standard

AppSettings stores strings or other values. It is found in a section of Web.config in an ASP.NET website project. Keeping constants in non-code files allows changes to be made easier. We use this feature in the Web.config file.

It is possible to cache the results from appSettings. This makes the code 30 times faster and allows better code separation and clarity. We will use ASP.NET global variables. Here are the settings in Web.config.

Static classes hold static methods and enforce code correctness. There is a static constructor that sets the properties. When the ASP.NET AppDomain for this site is initialized the properties will be accessed and cached.

public class AppConfig
{
private static string _moduleTitle;
private static bool? _bundlingActive;
 
public static string ModuleTitle
{
    get
    {
        if (string.IsNullOrEmpty(_moduleTitle))
            _moduleTitle = ConfigurationManager.AppSettings["ModuleTitle"];
        return _moduleTitle;
    }
}
 
public static bool BundlingActive
{
    get
    {
        if (!_bundlingActive.HasValue)
            _bundlingActive = ConfigurationManager.AppSettings["BundlingActive"] == "1";
        return _bundlingActive.Value;
    }
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s