95 lines
3.4 KiB
C#
95 lines
3.4 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
|
|||
|
|
namespace DatabaseImport
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// handles storing/retrieving settings from a db
|
|||
|
|
/// assumes DB connection is already established or connection information has already been set for db
|
|||
|
|
/// </summary>
|
|||
|
|
public class SettingsDB
|
|||
|
|
{
|
|||
|
|
private static SettingsDB _instance;
|
|||
|
|
private static readonly object LOCK_OBJECT = new object();
|
|||
|
|
|
|||
|
|
private static SettingsDB Instance
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
lock (LOCK_OBJECT)
|
|||
|
|
{
|
|||
|
|
return _instance ?? (_instance = new SettingsDB());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private readonly Dictionary<string, Setting> _settingsLookup = new Dictionary<string, Setting>();
|
|||
|
|
/// <summary>
|
|||
|
|
/// retrieves a global value, creates using default value if property doesn't exist yet
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <param name="defaultValue"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static string GetGlobalValue(string id, string defaultValue)
|
|||
|
|
{
|
|||
|
|
lock (LOCK_OBJECT)
|
|||
|
|
{
|
|||
|
|
if (!Instance._settingsLookup.ContainsKey(id))
|
|||
|
|
{
|
|||
|
|
Instance._settingsLookup[id] = new GlobalSetting(id, defaultValue);
|
|||
|
|
}
|
|||
|
|
return Instance._settingsLookup[id].PropertyValue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// returns the global value for given property, or the default value if no value currently exists
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <param name="defaultValue"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool GetGlobalValueBool(string id, bool defaultValue)
|
|||
|
|
{
|
|||
|
|
var sValue = GetGlobalValue(id, defaultValue.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|||
|
|
var b = defaultValue;
|
|||
|
|
return !bool.TryParse(sValue, out b) ? b : b;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// stores an application global property in the database
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
public static void SetGlobalValue(string id, string value)
|
|||
|
|
{
|
|||
|
|
lock (LOCK_OBJECT)
|
|||
|
|
{
|
|||
|
|
if (!Instance._settingsLookup.ContainsKey(id))
|
|||
|
|
{
|
|||
|
|
Instance._settingsLookup[id] = new GlobalSetting(id, value);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Instance._settingsLookup[id].SetValue(value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// stores an application global property value in the database
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="id"></param>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
public static void SetGlobalValueBoolean(string id, bool value)
|
|||
|
|
{
|
|||
|
|
lock (LOCK_OBJECT)
|
|||
|
|
{
|
|||
|
|
if (!Instance._settingsLookup.ContainsKey(id))
|
|||
|
|
{
|
|||
|
|
Instance._settingsLookup[id] = new GlobalSetting(id, value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Instance._settingsLookup[id].SetValue(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|