I’m considering creating a static class to handle all of my Web.config appSettings access. For example, it would look like this:
public static class ConfigManager
{
public static string Timeout = ConfigurationManager.AppSettings["Timeout"];
public static string Version = ConfigurationManager.AppSettings["Version"];
}
I believe that this would give me a central location to change keys in the app settings if I wanted to change one in the future and would give me intellisense for all the configuration settings in my application.
My question is how this would operate as I’m not sure how static works under the hood. My hope is that the first time I accessed one of the properties, all the properties would be read from the config and placed in memory and that all subsequent hits would then just go to memory instead of looking at the config. Unfortunately, this would mean that runtime changes to the config wouldn’t take effect. I also thought it may be possible that only the property I’m looking at would be loaded, or that they would all be loaded every time I access any property.
Does anyone know how the combo of having a static property reading from the configs would behave under the hood?
Static means that there will only be one instance of that class or variable in memory.
Since you chose a static class the values will be set once by your assignment when the static constructor is called. This will happen the first time the class is used.
Anytime you access the variable after that it will be pulling the value from memory.
If you are concerned about being able to change values at runtime, you could use a property instead and then implement a caching strategy that would refresh that property at a given time interval.