0

I need to store a Dictionary in my asp .net application. This dictionary is basically a straight mapping of keys (strings) to values (strings). During the life of the Application the dictionary will not change and will contain about 10 elements.

I will be iterating over possibly thousands of rows returned from a stored procedure and obtaining corresponding values from this dictionary.

What would be the best approach to tackle this? Here are the 2 ideas I have on implementing it:

  1. Instantiate and initialize the dictionary during Application_Start and storing it in the ApplicationState object.

Or ...

  1. Do what they propose here: How do I store a dictionary object in my web.config file?

I like option 2 since adding a new value in the future is just a matter of adding an entry in Web.Config without any code changes whatsoever but I'm concerned about performance since I will have to obtain a value for every single row returned from the stored procedure.

I wonder if these lookups in web.config are somehow optimized/cached?

divibisan
  • 8,631
  • 11
  • 31
  • 46
Icarus
  • 60,193
  • 14
  • 91
  • 110
  • 1
    The web.config is loaded into memory so basically both options oppose the same performance – sternr Aug 11 '11 at 15:23

2 Answers2

2

If you're using ConfigurationManager to access AppSettings values, these are cached after the 1st time they're accessed. So the first time you access ConfigurationManager.AppSettings["MyValue"], it's read from disk, after that it's read from cache.

Joel C
  • 5,427
  • 1
  • 18
  • 30
0

Why not mix it up a bit. On Application_Start load your web.config values into a dictionary. Then you can still configure the list without code changes, and you code can easily reference the dictionary

EDIT: Of course, the disadvantage here is that the list will only refresh when you restart you website (i.e. cause Application_Start to fire again) where as a straight config file will allow more instant results when you modify the web.config file. Perhaps that is a reason to go for the web.config option

musefan
  • 45,726
  • 20
  • 123
  • 171