C # application - saving preferences in a database or configuration file?

I have an application that uses SQLite, which is very light and fast. I have some settings that do not have to be loaded at startup, but may need to be used at different times depending on where the user is going. However, I can’t decide where to store this information.

Q1: Should I just go ahead and store it in the database? Should I store it in a configuration file?

Q2: Do I have to load and save settings and other data at startup, even if they are not necessarily used immediately? Or should I just query the database when I need it?

Example. My application can store company information for a software company. Company name, company phone number, etc. The only time this information is used is when the software automatically prints a letter, or the user proceeds to edit his company information in the program.

EDIT: I realized that it comes down to application settings and user settings. My program does not have multiple users per copy of the software. In this case, I would suggest that these will be application settings.

+5
source share
4 answers

, , , Hashtable.

GetSetting, . Hashtable, , , , Hashtable. , GetSetting/SetSetting.

, .

public class Settings {
    private object SyncRoot = new object();
    private System.Collections.Hashtable _cache = new System.Collections.Hashtable();

    public T GetSetting<T>(string xPath, T defaultValue)
    {
        lock (SyncRoot)
        {
            if (!_cache.ContainsKey(xPath))
            {
                T val = GetSettingFromDB<T>(xPath, defaultValue);
                _cache[xPath] = val;
                return val;
            }
            return (T)_cache[xPath];
        }
    }

    public T GetSettingFromDB<T>(string xPath, T defaultValue)
    {
         // Read from DB
    }

    public void SaveSetting<T>(string xPath, T value)
    {
        lock (SyncRoot)
        {
            if (_cache.ContainsKey(xPath))
                _cache[xPath] = value;
        }

        SaveSettingToDB<T>(xPath, value);
    }

    public T SaveSettingToDB<T>(string xPath, T defaultValue)
    {
         // Read from DB
    }
}

, :

    public static bool BooleanFeature
    {
        get { return Settings.GetSetting<bool>("BooleanFeature", true); }
        set { Settings.SaveSetting<bool>("BooleanFeature", value); }
    }

:

if (Setting.BooleanFeature) {
    // Run certain code
else {
    // Run other code
}
+3

, . , .

. , , , , ( - .)

+1

JTA , 3 , .

  • . , , , . , , .
  • , , , . .

  • , XML, xml-. , .

0

All Articles