Friday, April 25, 2014

Why don't more people use the .Net Settings as opposed to Config settings.

I see a lot of people putting key-value pairs in web.config/app.config files and then using them in their applications via
var someConfigKey = ConfigurationManager.AppSettings["someConfigKey"];
int count;
if (int.TryParse(someConfigKey, out count))
{
    //// Do Something
}

However I rarely see people using application settings. I won't go into depth on what application settings are as the Microsoft website has a pretty good explanation of them here however Settings are strongly typed and so the above lines of code can all be replaced with:
var count = Properties.Settings.Default.Count;

I might be missing something here but it seems like the latter is much more convenient to use if you are simply using the appSettings section of the .config file. Of course it is a different story if you are using .config sections which are serialized into objects but for the most part what I can figure is that many people don't know about .net Settings.

Take this or this stackoverflow post which add to the confusion and were found on the first page of googling for the difference between settings and config files.

As a side note, if you do choose to use .Net Settings instead of a .Config file and you wanted to list your settings on some admin section of a webpage you could do something like so:
public Dictionary GetSettings()
{
  return Properties.Settings.Default.Properties.Cast<SettingsProperty>()
    .ToDictionary(property => property.Name, property => Properties.Settings.Default[property.Name].ToString());
}

No comments:

Post a Comment