views:

912

answers:

5

I have two questions:

1) I have a few global variables for my website declared in my global.asax file. They are simple, small key/value pairs and there are only a few of them. Is this a good practice for values that are small and need to be accessed by almost every page on my website? Storing them in the database and requiring a db lookup seems as thought it would waste resources for values that don't change rapidly.

2) If one of the values changes once per week, is it possible to allow a user to edit the global variable with a form or some other means?

Example:

<script runat="server">

  Overloads Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

      Application("default_scoring_id") = 3
      Application("week_current") = 3

  End Sub

</script>

In the above example, the system needs to know which week (out of 15 of them) that the current date is within. So, once every Monday morning, the "week_current" value needs to change.

I can easily do this, but is there a way to give a user access to changing this value without touching the code?

+1  A: 

The typical practice is to put these into the web.config, which can be edited. These <appSettings> values will then be available on every page, yet the file can be edited.

John Saunders
+1  A: 

I would consider using the built in Cache (System.Web.Caching.Cache)

That way you can store them where ever you want (say in a database), change them from within the app easily, and have quick and cheap retrieval.

From within a class which inherits from BasePage use the given Cache object (eg Cache.Add(..)) and from elsewhere use HttpContext.Current.Cache (eg. HttpContext.Current.Cache.Remove(Key))

Tetraneutron
+1  A: 

1) Good practice is usually to store those sort of values in the web.config. See here and here for some guides.

2) a) If you store this in the web.config it will be easily updatedable without the need for recompiling and the web application should immediatly pick up the changes.

b) Does updating something like the 'week number' really need to be a manual process? It sounds a bit error prone and i would suggest automating this if at all possible by calculating it based on the current date.

mundeep
It would seem like you could base the week number on the date, but this is problematic when certain work is delayed, thus preventing a routine change-of-week. I need to let the user decide the exact time to change the week, unfortunately.
wrburgess
A: 

The other answers suggest the different ways this can be done, and must be done. But, even then if you want to allow the user to edit the global variable, you'll have to take a lock or Mutex on a shared object, change the value of your global variable, and release the lock or Mutex.

lock(globalsharedobject) //This is C# code.
{
    Application("default_scoring_id") = 3;
}
Kirtan
A: 

Web.config is the .NET way, or ASP.NET way, it's not always the most efficent or most suitable.

You're Global.asax file is much more than some events, you can put static data in any class that subclass System.Web.HttpApplication and inherit that in your Global.asax file.

The HttpSessionState and HttpApplicationState are relics, from the classic ASP time and you would do well to avoid them, becuase the serve no real purpose.

Depending on the type (System.Type) of your objects you can design your own strongly typed objects that store information about your application and session, for application level data a bunch of static fields would be enough.

They have to be static becuase each HttpModule as well as HttpApplication instance are pooled object, so to avoid that confusion, make sure your persistent data is stored in a static or several static dicionaries. But be aware of concurrency issues when modyfying these collections. A good strategy is to lock the object, only for the duration you're modifying it and make sure you don't call any other code while modyfiny the collection, a simple swap idiom, might be helpful here, it's fast and it's a non-deadlock guarntee.

John Leidegren