C# 应用程序 - 将首选项存储在数据库或配置文件中?

发布于 2024-10-01 05:36:44 字数 355 浏览 2 评论 0原文

我有一个使用 SQLite 的应用程序,它非常轻量且快速。我有一些首选项不一定需要在启动时加载,但可能需要根据用户所在的位置在不同时间使用。话虽这么说,我无法决定在哪里存储这些信息。

Q1:我应该直接将其存储在数据库中吗?我应该将其存储在配置文件中吗?

问题 2:我是否应该在启动时加载并存储首选项和其他数据,即使它们不一定立即使用?或者我应该在需要时查询数据库?

示例:我的应用程序可以存储正在使用该软件的公司的公司信息。公司名称、公司电话等。这些信息仅在软件自动打印信件或用户在程序中编辑公司信息时使用。

编辑:我意识到这取决于应用程序设置与用户设置。我的程序的每个软件副本没有多个用户。话虽这么说,我想这些将是应用程序设置。

I have an application that uses SQLite, which is extremely light weight and quick. I have some preferences that don't necessarily need to be loaded on startup, but might need to be used at various times depending on where the user goes. That being said, 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 config file?

Q2: Should I load and store the preferences and other data at startup even if they're not necessarily being used right away? Or should I just query the database when I need them?

Example: My application can store the company information for the company that is using the software. Company name, company phone, etc. The only time this information is used is when the software auto-prints a letter, or the user goes to edit their company information in the program.

EDIT: I've realized that this comes down to application settings vs user settings. My program does not have multiple users per copy of the software. That being said, I would suppose these would be application settings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由范儿 2024-10-08 05:36:44

您可能想要做的是编写一个封装设置的类,然后将它们读入 Hashtable。

您可以使用基本的 GetSetting 方法来根据名称查找设置。如果该设置位于Hashtable中,则返回该值,否则前往DB查找该设置,然后将其存储在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
}

What you may want to do is write a class that encapsulates the settings and then reads them into Hashtable.

You could have a basic GetSetting method that looks up a setting based on a name. If the setting is located in the Hashtable, return the value, otherwise go to the DB to find the setting and then store it in the Hashtable. You can then write separate properties for each setting you want, each calling the GetSetting/SetSetting methods.

This allows you to store the settings in the DB easily, and caches the reads to avoid constantly reading the DB.

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
    }
}

Then just create a class with a bunch of properties like this:

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

Now you can do this in your code:

if (Setting.BooleanFeature) {
    // Run certain code
else {
    // Run other code
}
两仪 2024-10-08 05:36:44

您想要保存多少设置?使用内置设置功能非常轻松。

http://msdn.microsoft.com/en-us/library/aa730869.aspx

How many settings are you looking to save? Using the built-in settings feature is pretty painless.

http://msdn.microsoft.com/en-us/library/aa730869.aspx

无畏 2024-10-08 05:36:44

将配置数据存储在文件中对于很少更改的轻量级设置很有好处。通常,您会针对开发和生产之间不同的设置执行此操作,并用于启动和运行应用程序。

之后,其他所有内容最好存储在数据库中。这使您可以选择良好的用户界面来修改设置,在需要时加载它们,在系统升级期间保存它们,如果您使用多个前端则可用(而不是将配置保存在文件中并确保所有前端- 两端具有相同的最新文件。)

Storing configuration data in a file is good for light-weight settings that rarely change. Usually you'd do this for settings that are different between development and production and are used to get your application up and running.

After that, everything else is best stored in a database. This gives you the option for good user interfaces to modify the settings, load them when needed, save them during upgrades to your system, be available if you're using multiple front-ends (versus saving the configuration in a file and ensuring all front-ends have the same up-to-date files.)

野心澎湃 2024-10-08 05:36:44

除了 JTA 的答案之外,我还想补充一点,我使用了 3 种方法,它们都有其优点和缺点。

  1. 将它们存储在内置的
    实际上将它们锁定到
    运行用户。例如如果
    有多个用户使用您的应用程序
    将是独立设置
    每个用户。如果这就是你想要的,
    选择这个。
  2. 将它们存储在数据库中是
    如果你不希望它有用的话
    绑定到用户而不是
    数据库。虽然你无法改变
    这些设置来自应用程序外部。

  3. 我使用了一个配置类
    如果我需要编辑,则使用 XML 进行序列化
    使用 xml 编辑器。例如
    如果您是这样的话,这非常有用
    运行服务。

In addition to JTAs answer I would like to add that I've used 3 methods and they all have their up and down sides.

  1. Storing them in the built-in one
    does actually lock them to the
    running user. So for instance if
    multiple users use your app, there
    will be independent settings for
    each user. If this is what you want,
    pick this one.
  2. Storing them in the database is
    useful if you do not want it to be
    bound to a user but rather to the
    database. Though you cannot change
    these settings from outside the app.

  3. I've used a config-class that I
    serialize with XML if I need to edit
    it with an xml-editor. For instance
    this is very useful if you are
    running a service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文