.Net 中可用于本地状态持久化的选项有哪些
我正在寻找保存应用程序本地状态的好选择(在 .Net / C# 中创建)。
我考虑过推出自己的解决方案,或者使用简单的本地数据库(例如 Sqllite),但是我想我应该问那么社区有哪些可用选项。
我正在开发的应用程序存在于信息亭上,我需要维护本地状态,以防意外关闭和其他类似的异常。
你有什么建议吗?
更新
好吧,经过进一步审查,我决定将 SQLite 与 DbLinq 一起使用,主要的替代竞争者是 SQL Server Compact 版本,但有几个我发现很重要的因素,这些因素使 SQLite 高于 SQLCE。
尤其。
- SQLCE (IMO) 提供的 API 不如 SQLite.net 或 DbLinq 提供的丰富。
- SQLCE 运行时性能不如 SQLite。
- SQLite 开源是一个有用的因素。
为什么我没有使用 Properties.Settings 或 app.config?
对于我的用例,我需要类似(轻松)可查询的事件日志之类的东西(我需要允许对信用交易进行审计。)我在这里解决的主要状态值是信息亭上的可用信用。因此,要在应用程序启动时恢复状态变量,我可以查询日志中的最后一个条目并从那里获取所需的值。
当您只需要存储一个值并检索它时,Properties.Settings 或 app.config 是完全适合执行此操作的位置。
I'm looking for good options for persisting local state of an application (created in .Net / C#.)
I've considered rolling my own solution, or using a simple local database such as Sqllite, however I thought I'd ask the SO community what options are available.
The application I'm working on exists on a Kiosk, and I will need to maintain local state in case of accidental shutdown and other similar exceptions.
What do you recommend?
Update
Well, after further review I decided to use SQLite with DbLinq, the main alternative contender was SQL Server Compact edition, but there are several factors which I found important, which placed SQLite above SQLCE.
Notably.
- The API provided by SQLCE (IMO) wasn't as rich as the ones provided by both SQLite.net or DbLinq.
- SQLCE runtime performance wasn't as good as SQLite.
- SQLite being open-source is a useful factor.
Why I didn't use Properties.Settings or app.config?
For my use case I needed something like a (easily) query-able event log (I need to allow auditing of credit transactions.) The primary state value I was addressing here was available credit on a kiosk. So to restore my state variables at application start up, I can query the log for the last entry and get the required values from there.
When you only need to store a value and retrieve it Properties.Settings or app.config are perfectly adequate places to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会推荐
XmlSerializer
或BinaryFormatter
,你可以在 SO 或 google 上查看它们。您的选择将取决于一些偏好,例如数据集的大小或快速序列化数据的能力。
如果您需要一次性保存整个应用程序状态,这两种方法都非常快捷。
但是,如果您需要在发生更改时部分保存应用程序状态,那么最好使用某种数据库,
http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm
http://www.jpreece.com/csharp/serialization-tutorial/
I would recommend
XmlSerializer
orBinaryFormatter
, you can look them both on SO or google.Your choice will depend on few preferences such as size of dataset or ability to serialize it quickly.
And both are really quick way IF YOU NEED whole app state saved in one go.
However, if you need to save app state part by part, as changes occur, you'll be better of with some kind of a database, http://www.sqlite.org/ for example.
http://www.jonasjohn.de/snippets/csharp/xmlserializer-example.htm
http://www.jpreece.com/csharp/serialization-tutorial/
SQLite 绝对是一个不错的选择,它比完整的数据库更轻,而且它符合 ACID,以防出现严重错误。
这里唯一的要求是数据库有一个并发用户。
SQLite is definitely a good option, it's lighter than a full blown database, and it's ACID compliant in case something goes terribly wrong.
The only requirement here is that you have one concurrent user for the database.
您可以使用 app.config 或
Properties.Settings
吗?Can you use the app.config or
Properties.Settings
?您可以使用VS 中的设置。对于您的情况,您需要存储用户首选项的用户范围设置。
You can use Settings in VS. For your case, you need user-scope settings that store user preferences.
您可以使用 SQL Server CE 4.0 代替 SQLite。将数据存储在一个文件中,您可以使用实体框架代码优先。
You can use SQL Server CE 4.0 instead of SQLite. Store data in one file and you can use the Entity Framework Code-First.