1

I'm trying to create a sort of global settings for a website and store this data on the database, however I keep thinking that may not be very efficient as these settings will have to be read on every request.

The type of settings like 'how many records to show per page', enable/disable things, I plan to store this on the database but don't want the overhead of having to call the database on every request to get the settings, specially when they don't change. Surely this is done all the time on CMS's, how do you think it should be done. I am thinking SqlCacheDependency but never set that up. Is there another way?

Also on the cards is the possibility to store those settings on web.config and create a GUI for it, the problem is that the administration of the site runs on it's own namespace and has it's own web.config, so the question here is if it's possible to manipulate a web.config outside the application namespace.

Thanks guys.

Nelson Pires
  • 321
  • 4
  • 12

2 Answers2

1

I would suggest to you to read the values from the DB in the Application_Start and store these values in the Application object. In this way you will not need to go to the DB to read the values every time. It will only read and store values once when the application starts.

void Application_Start(object sender, EventArgs e)
{
  Application["name"] = ""; Value from DB
  ......................
  ......................
}

Note: It is not recommend to manipulate values in the web.config using UI, because once a user tries to modify any value from the UI, that changes the value in the web.config, all your User session will be terminated.

Another Note: Every time you change the information and update your DB, you will need to update the Application level object as well.

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • If I store something on the Application object on Application_Start and then the database changes, the Application would not know until it's restarted again, is that correct? – Nelson Pires May 30 '11 at 11:13
  • yah, but every time when you change the information and update your DB, then you need to update Application level object. – Muhammad Akhtar May 30 '11 at 11:17
0

First, I wouldn't start worrying about counting database calls until you have an idea that your database calls are actually hurting performance. Modern databases are quick, especially for well-designed queries for scalar values. Lots of popular packages read config on every request and they seem to scale pretty well.

As for updating these values, you can pretty easily update a web.config file from another app presuming you've got the right permissions -- this will definitely require Full Trust which leaves out most hosting scenarios. Thing to remember is that, while VS treats the file special, it is just an XML file so the normal tricks for updating an XML file will apply. Because you are doing this from a different application, it will work. But Muhammad's warning about dumping user sessions would apply to the "victim" of the update. Which might be OK depending on what you are changing.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
  • Thanks. I don't use sessions and wonder if changing the appSettings of web.config will trigger a restart, I think it might but not sure. – Nelson Pires May 30 '11 at 11:16
  • It will restart the app pool. Depending on what is going on there, this could or could not matter for you. Session dumping is the one user-visible angle here. – Wyatt Barnett May 30 '11 at 11:19
  • I can treat the web.config as any XML file, but I was thinking in using proper code like: [code]private bool UpdateAppSettings(string strKey, string strValue) { bool success = false; Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~"); AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings"); if (objAppsettings != null) { objAppsettings.Settings[strKey].Value = strValue; objConfig.Save(); success = true; } return success; }[/code] – Nelson Pires May 30 '11 at 11:21
  • I think you have a point when you say that most popular packages do this per request. I was looking into WordPress the other day and that is full of inline SQL in the PHP and they get away with it pretty well, in my case I don't use inline SQL at all, all carefully written stored procedures that I make sure will compile well, so I guess that's how I will do it. May still look into SqlCacheDependency later as that seems to give out promising results in the sense that it only queries when data has changed on the database, the rest of the time is digs out from cache. – Nelson Pires May 30 '11 at 11:32