Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store list of static data in C#?

I am working on a website and I want a drop-down to display the list of cities. Where should I store the list of cities for faster access? I do not want to store this data in DB.

Is it a good idea to store it in XML file?

like image 882
Neha Avatar asked Dec 29 '25 20:12

Neha


2 Answers

I would store it in Cache, possibly with a Sql Dependency or File Dependency.

public DataTable GetCities(bool BypassCache)
{
   string cacheKey = "CitiesDataTable";
   object cacheItem = Cache[cacheKey] as DataTable;
   if((BypassCache) || (cacheItem == null))
   {
      cacheItem = GetCitiesFromDataSource();
      Cache.Insert(cacheKey, cacheItem, null,
      DateTime.Now.AddSeconds(GetCacheSecondsFromConfig(cacheKey), 
      TimeSpan.Zero);
   }
   return (DataTable)cacheItem;
}

If you want to store it in XML, that's fine too but you'll have to publish the file to all servers in the farm each time there is a change.

like image 55
Chris Gessler Avatar answered Dec 31 '25 09:12

Chris Gessler


Store it in a text file. This avoids the overhead of XML parsing. Load using File.ReadAllLines().

like image 20
cdiggins Avatar answered Dec 31 '25 09:12

cdiggins