C# 应用程序 - 将首选项存储在数据库或配置文件中?
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想要做的是编写一个封装设置的类,然后将它们读入 Hashtable。
您可以使用基本的 GetSetting 方法来根据名称查找设置。如果该设置位于Hashtable中,则返回该值,否则前往DB查找该设置,然后将其存储在Hashtable中。然后,您可以为所需的每个设置编写单独的属性,每个属性都调用 GetSetting/SetSetting 方法。
这使您可以轻松地将设置存储在数据库中,并缓存读取以避免不断读取数据库。
然后只需创建一个具有一堆属性的类,如下所示:
现在您可以在代码中执行此操作:
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.
Then just create a class with a bunch of properties like this:
Now you can do this in your code:
您想要保存多少设置?使用内置设置功能非常轻松。
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
将配置数据存储在文件中对于很少更改的轻量级设置很有好处。通常,您会针对开发和生产之间不同的设置执行此操作,并用于启动和运行应用程序。
之后,其他所有内容最好存储在数据库中。这使您可以选择良好的用户界面来修改设置,在需要时加载它们,在系统升级期间保存它们,如果您使用多个前端则可用(而不是将配置保存在文件中并确保所有前端- 两端具有相同的最新文件。)
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.)
除了 JTA 的答案之外,我还想补充一点,我使用了 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.
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.
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.
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.