Where to store application settings?
Asked Answered
W

9

8

Recently, I discovered that the "Web.Config" file contains an <appSettings> section which seemed good for storing one's Application Settings. Heck, it even has a programmatic way to access the file thru a standard System library. So being all clever, I wrote an Interface to access it and then a Concrete implementation of the interface, shown here:

public interface IAppSettings
{
    IEnumerable<string> GetValues(string componentName, string settingName);
    IEnumerable<KeyValuePair<string, string>> GetValuePairs(string componentName, string settingName);
    void SetValues(string componentName, string settingName, IEnumerable<string> valueList, bool append);
    void SetValuePairs(string componentName, string settingName, IEnumerable<KeyValuePair<string, string>> pairList, bool append);
}

Then I came to discover that saving settings back to "web.config" while the application is running causes the entire application to re-start. This seems completely unreasonable to me because if I'm writing back to web.config a lot and the application is restarting each time, then things like HttpRuntime.Cache get completely emptied effectively making my Cache useless because it's constantly emptying and repopulating.

So I'm wondering: Where should I store my Application Settings?

Is there a good solution out there for this so that I don't have to roll my own?

EDIT:

Okay, thanks to everyone who suggested using a DB and a potential table schema. I think I'm going to go with the following schema:

settings:
    index NUMBER NOT NULL AUTO_INCREMENT   <== Primary Key
    component NVARCHAR(255) NOT NULL
    setting NVARCHAR(255) NOT NULL
    key   NVARCHAR(255)
    value NVARCHAR(255) NOT NULL

Though I don't think I'll make the "setting" the P-Key, but use an Auto-Incr Index instead. This way if I have an application that needs to mail something to multiple managers, I can store many:

index     component       setting        value
1         RequestModule   ManagerEmail   manager1@someplace
2         RequestModule   ManagerEmail   manager2@someplace

And then I can use:

IEnumerable<string> GetValues(string componentName, string settingName);

And it will return a list of email addresses, rather than just a single value.

Does this make sense?

Woodrow answered 20/10, 2010 at 14:23 Comment(2)
Using DB to stores applciation settings is OK but please store the settings in Cache. Settings in web config are fast to read since they are in server memery. If you constantly read settings in evvery page consider adding caching to your settings and also remove them form cache when the settins are updated.Massasauga
following your edit, I edited my answer. Note that when you have two questions, ask them separately, instead of adding the second question to the previous one.Riti
R
14

web.config is generally used for read-only settings, ie. the settings set during deployment of the application by the system administrator.

If you want to read and write the settings, the most obvious way is to use the database. By the way, this has an advantage: an application can be hosted on several servers and will still read and write the settings correctly,

You can also implement your custom storage for the settings, but it will be probably more difficult to implement and not much faster.


To answer your second question, the structure of your database depends on the type of settings you want to store.

If you need to store heterogeneous unique entries like this:

  • Mail address of an administrator,
  • Maximum number of entries to display on home page of the website,
  • Text to display on "About us" page,
  • Boolean value indicating whether public comments are enabled or not,

then you have to use varchars or other more or less friendly types as keys to identify the entries (rather than to refer to them by their index).

On the other hand, if your purpose is to store the mail addresses of several managers, you should create a Manager table containing their mail addresses, names, datetime of their last connection, etc.

You really shouldn't mix both. In theory, you can refer to the entry in settings by component/setting pair. In practice, it makes things harder and creates a bunch of problems:

  • What if, further, you will need, for every manager, to store a boolean value indicating whether she/he wants to receive alerts from you? With your current structure, this will be impossible.
  • Since the same setting can have multiple values, how do you intend to handle the settings which must be unique? For example, there must be only a single value of text to display on "About us" page. What if there are two values stored in database?
Riti answered 20/10, 2010 at 14:27 Comment(2)
Interesting, I hadn't considered an application possibly running on multiple servers, but it makes sense. In my case, it likely will never happen because it's for an internal company project, but this is good to keep in mind.Woodrow
Interesting. I was just searching for how to store a single value since it felt wrong to fire up a database table that would only ever contain one record, one integer. But you saying that the "web.config is generally used for read-only settings" makes sense too. As a result of your (excellent) answer I am confused as to where the best place to store an application wide read/write integer in a web service is.Publias
P
6

Storing settings in web.config is useful because it makes it easy to have different settings in different environments. However, as you say, this is no use if you are likely to want to change the settings in a live environment.

A simple database table is the most useful way to do this if you need to change the values.

eg.

create table Settings
(Name varchar(50) primary key,
Value varchar(50))

If you're using SQL Server, you can set to the Value column to be sql_variant which will allow you to store a variety of datatypes.

Pentomic answered 20/10, 2010 at 14:29 Comment(1)
+1 for sql_variant. nvarchar(max) is not a good idea when storing data of any type (not to mention varchar(100)).Riti
A
5

It is meant for application settings, but not settings that are meant to be dynamically changed during runtime. Rather, it is for settings that change only occasionally, and where you would expect (and even desire) an app restart when they change.

For more ephemeral settings, you may want to just use a simple database system - even a flat file/XML in the App_Data dir can work, if your app doesn't use a database otherwise.

Asphyxiate answered 20/10, 2010 at 14:27 Comment(0)
G
5

Yes.Changing the web.config will reset the application.I usually maintain a settings table to store key-value pairs for the settings and access it from there.

SETTINGS

SETTING_NAME   VARCHAR(100)PRIMARY KEY
SETTING_VALUE  VARCHAR(100)

Then write a class which can Insert,Delete,Update values to this table.

Ex Data for the SETTINGS table

    SETTING_NAME         SETTING_VALUE

    AdminEmail            [email protected]
    ErrorTrackingEmail    [email protected]
Gussiegussman answered 20/10, 2010 at 14:28 Comment(1)
name should be primary key. Separate columns makes no senseAbdication
R
3

I usually add a "type" field to my settings table so that I only retrieve the group of settings needed this works for me as a way of grouping like settings and retrieving them at once.

Rake answered 20/10, 2010 at 14:45 Comment(2)
In what scenario would you need to fetch a group of settings? Is it for performance reasons mainly?Pilau
Some settings are grouped such as DirectoryPath, if I am looking for DirectoryPath I can just pull all the settings where type = DirectoryPath and then just iterate through them.Rake
A
1

Create a key-valued table like this:

settings:
name NVARCHAR(255) PRIMARY KEY
value NVARCHAR(255) NOT NULL
Abdication answered 20/10, 2010 at 14:29 Comment(0)
H
1

First of all, it's not unreasonable at all when you consider what information is supposed to be stored in the web.config file. When you change things like assembly information, connection strings, etc., your application needs to stop and reload the values to run with those settings.

If you're storing application wide settings, then you can create a Settings table in your database or even use a separate text file to store the settings.

If you're talking about storing per-user settings, you shoul check out ASP.NET Profile Properties.

Huberthuberto answered 20/10, 2010 at 14:30 Comment(1)
By "unreasonable", I meant the constant "restarting" of the application was a side-effect that would have performance issues that I wouldn't be able to tolerate. (not that the reason behind it restarting was unreasonable...) -- Thanks for pointing out the ASP.NET Profile properties. It's not exactly what I'm looking for, but I think it will help me out with another problem I'm having... :-)Woodrow
C
1

Application-wide settings should certainly be stored in this section of the web.config file, as it prevents hard-coding values which may change over time. There's no need to read them out using your own code, as there's a built-in method: use the System.Configuration.ConfigurationManager.AppSettings array to retrieve them (you'll need to add a reference to the System.Configuration assembly in your project). You can edit AppSettings through the ASP.NET Web Site Administration Tool, too (Project menu -> ASP.NET Configuration).

For values which you envisage changing more often and while the site is up and running, it's reasonable to use an XML file, lightweight database (such as SQLite or SQL Server Compact) or even a text file for these settings.

Charron answered 20/10, 2010 at 14:43 Comment(0)
T
0

If you need to save settings, you can always save them in a custom configuration file.

I did just that a while back, and I have the code available to do it here.

Timothytimour answered 20/10, 2010 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.