开发者

How should I store a lookup collection?

开发者 https://www.devze.com 2022-12-30 06:03 出处:网络
I am debugging an application that uses an XML file to store data as key/value pairs. Every pageload the XML file is parsed to populate a Dictionary collection with these key/value pairs. The Diction

I am debugging an application that uses an XML file to store data as key/value pairs.

Every pageload the XML file is parsed to populate a Dictionary collection with these key/value pairs. The Dictionary is then used to look up values based on their keys.

Is this method of loading data from an XML file on every page load a good practice?

Here are some thoughts:

  • Should the XML data be stored in a database table instead?
  • Should 开发者_如何学JAVAI work with a collection or hit the database for every lookup request?
  • Could the collection be loaded on application start and set as a global/application property?

What do you guys think?


I would suggest loading the results of the XML Parsing into the ASP.NET Cache:

Dictionary<string, object> values = new Dictionary<string, object>();

if(Cache["ConfigDictionary"] != null)
{
    values = (Dictionary<string, object>)Cache["ConfigDictionary"];
}
else
{
    // Load data into the dictionary here

    // This dependency will ensure that you don't keep stale data in the cache
    CacheDependency dependency = 
        new CacheDependency(Server.MapPath("yourFileHere.xml"));

    // Inser the Dictionary into the cache
    Cache.Insert("ConfigDictionary", values, dependency);
}


I agree with @Justin, keeping the collection into cache is nice. Also, the question about storing data in a database, depends mostly on what content the app is loading. If there is config settings, or another data that has low probability to change, I think is more efficient keeping it in a XML file. Why?

  • Easier to maintain;
  • You dont need to care about connections, queries, parameters;
  • Easier to view data content;
  • Non database-skilled people (support for app), can change settings.


I would store it in the database instead of XML file. It is easier to query (if you have to run some reports for example) and also backup is sort of more controllable.

Of course, I assume you are using database for your web application in general.

You may try to use Viewstate to decrease number of database runs if your are in the context of the same web page.

0

精彩评论

暂无评论...
验证码 换一张
取 消