存储和加载程序数据,C# WPF
我正在 WPF 中用 C# 编写一个实用程序,允许用户创建角色扮演场景,包括怪物、物品、角色等。
用户将创建或导入元素(怪物等),然后使用导入的元素来创建角色扮演场景。创建场景。程序使用的所有内容都是在程序内创建的,因此我没有要访问的任何预定义数据。
这是我的问题 - 存储和加载数据的最佳方法是什么?
目前,我正在使用 XML 序列化将对象序列化为 XML 文件并稍后重新加载它们。这有点笨重,我想知道数据库是否会更有效 - 数据肯定是相关的(怪物有物品,地图有怪物等),并且可能有数十或数百个条目。
我不需要实际使用的代码行或方法,只需了解在这种情况下(在 .NET 中)通常使用哪种类型的文件存储/检索。
谢谢!
I'm writing a utility program with C# in WPF that allows users to create role-playing scenarios, including monsters, items, characters, etc.
The user will create or import the elements (monsters, etc) and then use the imported elements to create scenarios. Everything used by the program is created within the program, so I don't have any pre-defined data I'll be accessing.
Here's my question - what's the best way to store and load the data?
Currently, I'm using XML serialization to serialize the objects to XML files and reload them later. This is kind of clunky, and I'm wondering if a database would be more effective - the data is definitely relational (monsters have items, maps have monsters, etc), and there could be dozens or hundreds of entries.
I don't need actual lines of code or methods to use, just an idea of what kind of file storage/retrieval would usually be used in this situation (in .NET).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您自己所说:数据是相关的,因此关系数据库可能会有所帮助。使用 Sql Server Compact,您可以拥有简单的文件,可以将其命名为任何您想要的名称,并在打开时将其加载到 Sql Server 中。这样您就不必管理传统的数据库服务器,并且用户甚至不知道涉及到数据库。
为了访问数据,我个人非常喜欢 Linq-to-Sql,它直接在 C# 中提供类型安全的查询。
As you said yourself: The data is relational so a relational database will probably help. Using Sql Server Compact you can have simple files, which are named whatever you want, that you load into Sql Server when opening. That way you won't have to administer a traditional database server and the user won't even know there is a database involved.
To access the data I'm personally very fond of Linq-to-Sql, which gives type-safe querying directly in C#.
数据库无疑是一条出路。使用对象关系映射器与数据库对话,这将可能一开始就能满足你99%的需求。
对于需要不同进程相互通信的场景,我更喜欢保留 XML 序列化。
Database is the way to go, definetely. Use an object-relational mapper to talk to a database, this will probably cover 99% of your needs at the beginning.
I prefer to keep the XML-serialization for the scenarios requiring different process intercommunication.
这实际上取决于您需要实现什么目标。数据库占有一席之地,但平面文件也非常适合数据(通过序列化)。
所以; xml给你带来了什么问题?如果您能回答这个问题,那么您就会知道您想要解决的痛点是什么。您提到“游戏”,实际上平面文件往往更合适(假设您想要最小的开销等),但通常都可以。二进制序列化可能在 CPU 和磁盘方面更有效(但我不推荐 BinaryFormatter - 当您更改类型时它会咬您)。
我不是反-数据库(远非如此)-我只是想提出一个平衡的观点;-p
It really depends on what you need to achieve. Databases have a place, but flat files are also perfectly fine for data (via serialization).
So; what problems is the xml giving you? If you can answer that, then you'll know what the pain points are that you want to address. You mention "game", and indeed flat files tend to be more suitable (assuming you want minimum overhead etc), but either would normally do fine. Binary serialization might be more efficient in terms of CPU and disk (but I don't recommend
BinaryFormatter
- it will bite you when you change the types).I'm not anti-database (far from it) - I just wanted to present a balanced viewpoint ;-p
您可以使用对象数据库(例如 db4o)。好处包括:类型安全、无 ORM、索引信息……
You could use an object database (such as db4o). The benefits include: type safety, no ORM, indexed information...