Question

I'm currently having some configuration variables in my Web.config, eg:

<appSettings>
 <add key="RecipientEmailForNotifications" value="asdf@sdf.com" />
 <add key="NotifyNewEntries" value="true" />
 <!-- etc... -->
</appSettings>

I have a view where admin users can change this values online, eg.

/Admin/Configuration/


Recipient email for notifications: [___________]

Notify new entries: [x] Yes

[SAVE]

But there are two problems with this approach:

  1. Each time I deploy the web.config, I'm overriding those values that were saved online
  2. Each time the web application restarts (because web applications are naturally stopped when there is no activity), these values that were saved online are reset to the originals of the last deployment

So the question is: What is the best way of achieving this goal of having configuration variables that have to be persisted?

I think that using a table isn't very natural, but I might be wrong (please explain why).

Also, using app settings is very convenient when reading the values:

ConfigurationManager.AppSettings["RecipientEmailForNotifications"]

As opposed of using the database,

db.SomeConfigurationTable.SingleOrDefault(.....)
Was it helpful?

Solution

I'd use a combination of database store and caching.

If you are on a single web server you could store the settings in proc cache. If you are in a web farm you could look at something like couchbase (free memcached) or azure.

It might make sense to warm your cache on applicaiton start. That is read the settings from your database and populate your cache.

Another popular approach is to have a Get call that check the cache first and populate cache if cache is empty, e.g.

public string Get(string key, Func<string> getItemCallback) 
        {
            var item = Cache.Get(key) as string;
            if (item == null)
            {
                item = db.SomeConfigurationTable.SingleOrDefault(key);
                Cache.Store(key, item);
            }
            return item;
        }

Not forgetting you will need to burst your cache when updating settings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top