Search code examples
c#asp.netperformanceweb-configapplication-state

Store Dictionary object in ApplicationState Vs putting it in Web.config


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?


Solution

  • 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.