patterncsharpMinor
Possible solutions to remove the use of a Hashtable
Viewed 0 times
thehashtableremovepossiblesolutionsuse
Problem
While this is working very well for storing setting variables for my applications, I've been using it for a number of years and really feel there is something better. I'm perhaps just looking to use some newer functions from the framework.
The settings are stored in the database and created at install time/modified as needed. At most I have 20 to
public string Value { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
///
/// Gets the selected setting from cache if it exists,
/// else returns the supplied default value
///
///
///
///
public static string GetCacheSetting(string settingName, string defaultValue)
{
return LoadSettings().Contains(settingName) ? Convert.ToString(LoadSettings()[settingName]) : defaultValue;
}
///
/// Returns the selected setting value from cache.
/// If cache is null then rebuild the cache.
///
///
public static Hashtable LoadSettings()
{
Hashtable h;
h = (Hashtable)HttpRuntime.Cache["SiteSettings"];
if (h == null)
{
h = new Hashtable();
DataAccess da = new DataAccess(); //My DAL
foreach (DataRow row in da.GetSiteSettings().Tables[0].Rows)
{
Settings oSetting = new Settings();
oSetting.SettingsInitFromRow(row);
h.Add(oSetting.Name, oSetting.Value);
}
HttpRuntime.Cache.Insert("SiteSettings",
h,
null,
DateTime.Now.AddMinutes(120),
Cache.NoSlidingExpiration);
}
return h;
}
///
/// Sets the values for the cache items
///
///
private void SettingsInitFromRow(DataRow row)
{
this.Name = (string)row["Name"];
this.Value = (string)row["Value"];
}The settings are stored in the database and created at install time/modified as needed. At most I have 20 to
Solution
It look spretty good to me, but there are a couple of things I would likely change, none of which have to do with the use (or non-use) of a hashtable data structure as it is perfectly suited for this sort of thing.
I would probably change the
I would probably change the
LoadSettings method a bit as it doesn't actually load settings each time it is called, and then definitely use the generic Dictionary instead of the antiquated Hashtable class, thereby removing the need for all that nasty casting. You could also avoid two lookups when attempting to retrieve a value from the cache by using TryGetValue instead of Contains and then performing another lookup, something like:public static string GetCacheSetting(string settingName, string defaultValue)
{
string ret;
Settings.TryGetValue( settingName, out ret );
return ret ?? defaultValue;
}
// you may even want to make this private as a caller could
// edit the contents of the cache directly. You could leave
// GetCacheSetting as the only access point, adding a
// SetCacheSetting method if needed.
private static Dictionary _settings;
public static Dictionary Settings
{
get
{
if( _settings == null )
{
_settings = LoadSettings();
}
return _settings;
}
}
private static Dictionary LoadSettings()
{
HttpRuntime.Cache.Remove( _settings );
_settings = new Dictionary();
DataAccess da = new DataAccess(); //My DAL
foreach (DataRow row in da.GetSiteSettings().Tables[0].Rows)
{
Settings oSetting = new Settings();
oSetting.SettingsInitFromRow(row);
_settings.Add(oSetting.Name, oSetting.Value);
}
HttpRuntime.Cache.Insert("SiteSettings",
_settings,
null,
DateTime.Now.AddMinutes(120),
Cache.NoSlidingExpiration);
return _settings;
}Code Snippets
public static string GetCacheSetting(string settingName, string defaultValue)
{
string ret;
Settings.TryGetValue( settingName, out ret );
return ret ?? defaultValue;
}
// you may even want to make this private as a caller could
// edit the contents of the cache directly. You could leave
// GetCacheSetting as the only access point, adding a
// SetCacheSetting method if needed.
private static Dictionary<string, string> _settings;
public static Dictionary<string, string> Settings
{
get
{
if( _settings == null )
{
_settings = LoadSettings();
}
return _settings;
}
}
private static Dictionary<string, string> LoadSettings()
{
HttpRuntime.Cache.Remove( _settings );
_settings = new Dictionary<string,string>();
DataAccess da = new DataAccess(); //My DAL
foreach (DataRow row in da.GetSiteSettings().Tables[0].Rows)
{
Settings oSetting = new Settings();
oSetting.SettingsInitFromRow(row);
_settings.Add(oSetting.Name, oSetting.Value);
}
HttpRuntime.Cache.Insert("SiteSettings",
_settings,
null,
DateTime.Now.AddMinutes(120),
Cache.NoSlidingExpiration);
return _settings;
}Context
StackExchange Code Review Q#3904, answer score: 4
Revisions (0)
No revisions yet.