将类实例存储到文件/数据库的最佳方法

发布于 2024-07-10 05:57:01 字数 1013 浏览 10 评论 0原文

将类的实例存储到文件/数据库的最佳方法是什么?

我们有一个名为 Command 的基类和大量派生类。 用户通过向图形设计器添加命令来创建这些类的实例 他们可以在哪里配置它们。 (设置属性)。

然后我们需要一种方法将这些“命令”存储到文件中而不丢失 任何信息。

一种想法是使用 db4o,但该项目不接受 GPL 许可证。

有什么建议或代码示例吗?

更新:(

为了“消除”我的问题的模糊性:p)

生成的代码可能看起来像这样:

    command[i++] = new DelayInSecondsCommand(2);
    command[i++] = new DaliRequestCommand(1, false, 254);
    command[i++] = new DaliRequestCommand(2, false, 254);
    command[i++] = new DaliRequestCommand(3, false, 254);
    command[i++] = new WaitInSecondsCommand(2);             
    command[i++] = new DaliRequestCommand(1, false, 0);
    command[i++] = new DaliRequestCommand(2, false, 0);
    command[i++] = new DaliRequestCommand(3, false, 0);
    command[i++] = new JumpCommand(0);

但随后有大量不同的命令。

我知道 .NET 序列化是可能的,尽管我以前从未使用过它, 但我想知道是否有更好的选择,就像我说的 db4o 看起来不错,但许可证不适合该项目。

更新2:

谢谢您的回复。 我现在可能会采用序列化解决方案, 但我也会研究其他选择。 仅供参考数据存储在 SQL Compact 数据库中。

What is the best way to store instances of a class to file/database?

We have a base class called Command and loads of derived classes.
Users create instances of these classes by adding commands to a graphical designer
where they can configure them. (Set the properties).

We then need a way to store these "commands" to a file without losing
any information.

One idea was to use db4o, but the GPL license is not acceptable for this project.

Any suggestions or code samples?

Update:

(In order to "de-blurryfie" my question :p)

The generated code might look something like:

    command[i++] = new DelayInSecondsCommand(2);
    command[i++] = new DaliRequestCommand(1, false, 254);
    command[i++] = new DaliRequestCommand(2, false, 254);
    command[i++] = new DaliRequestCommand(3, false, 254);
    command[i++] = new WaitInSecondsCommand(2);             
    command[i++] = new DaliRequestCommand(1, false, 0);
    command[i++] = new DaliRequestCommand(2, false, 0);
    command[i++] = new DaliRequestCommand(3, false, 0);
    command[i++] = new JumpCommand(0);

But then with loads of different commands.

I know it's possible with .NET serialization, altough I've never used it before,
but I was wondering if there are better alternatives, like I said db4o seems nice but the license doesn't fit the project.

Update 2:

Thank you for the replies. I'll probably go with the serialization solution now,
but I'll look into the other options as well. F.Y.I. data is stored in a SQL Compact database.

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

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

发布评论

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

评论(6

遮了一弯 2024-07-17 05:57:01

您是否想将数据保存在表中? 或者作为 blob/clob 数据? 既然你提到了文件,我假设后者:任何标准的 .NET 序列化器都应该没问题 - 它们都支持继承等。我会考虑使用 DataContractSerializer,因为它结合了字段级支持(像 BinaryFormatter),以及 XmlSerializer 的程序集独立性。

您还可以考虑更深奥的东西,例如 protobuf-net

那么:您需要做什么在标准序列化器下不起作用?

Are you trying to save the data in tables? or as blob/clob data? Since you mention files, I assume the latter: any of the standard .NET serializers should be fine - they all support inheritance etc. I'd consider for DataContractSerializer, as this combines the field-level support (like BinaryFormatter), and the assembly-independence of XmlSerializer.

You could also consider more esoteric things like protobuf-net.

So: what is it you need to do that won't work under the standard serializers?

伴随着你 2024-07-17 05:57:01

序列化就可以了! 序列化无非是将对象或对象的连接图转换为字节流(以便持久保存对象的当前状态)。 这可以是二进制流、XML 或其他任何形式。 您不必自己进行此转换,因为 .Net 对序列化有很好的支持。 序列化对象后,您可以随意将此数据存储到文件或数据库中。 同样,表示序列化对象的字节流可以反序列化为与原始对象具有相同状态的对象。

顺便说一句:一旦有了序列化的字节流,您就可以对其应用更多功能,例如压缩或加密。

serialization does the trick! Serialization is nothing more than converting an object or a connected graph of objects into a stream of bytes (in order to persist the current state of the object). This can be a binary stream, XML or whatever. You don't have to do this conversion by your own since .Net has great support for serialization. Once you serialized an object, you are free to store this data to a file or database. Likewise, a stream of bytes representing a serialized object can be deserialized into an object which will have the same state as the original one.

Btw: Once you have a serialized stream of bytes, you can apply some more functions on it, e.g. compression or encryption.

踏雪无痕 2024-07-17 05:57:01

非常模糊的问题,为什么不直接使用 .NET 的内置序列化功能(例如 XmlSerializer)。

Pretty blurry question, why don't you just use .NET's built-in serialization possibilities (e.g. XmlSerializer).

差↓一点笑了 2024-07-17 05:57:01

db40 还提供商业许可证,但它最近被Versant 购买了,所以也许您可能想看看。 这种数据库称为面向对象数据库,是一种创建类的持久实例的方法,这与使用表工作的关系数据库非常不同。

(wikpedia.org) 是一本关于面向对象数据库的好读物,(也是维基百科)是一些可用选项的列表。

在我看来,对象数据库要好得多。 比关系数据库更强大,如果我确实需要的话,我只会使用像 mysql 这样的关系数据库(不是很经常)。

我建议您观看这些视频并下载试用版。

db40 also provides a commerical license but it has been recently bought by versant so maybe you may want to look at that. This sort of database is known as object orientated database and is a way of creating persistant instances of classes which is very different to relational databases that work using tables.

This (wikpedia.org) is a good read on object orienated databases and this (also wikipedia) is a list of some of the available options.

In my opinion object databases are much better & more powerfull than relational and I will only use relational databases like mysql if I really have to (not very often).

I would recommomend you watch these videos and download the trial.

携君以终年 2024-07-17 05:57:01

http://www.neodatis.org。 它是 LGPL,但我使用的时候只有 Java 的实现。 现在,C# 有一个“测试版”版本,但我没有测试。

There is http://www.neodatis.org. Its LGPL, but the time I used there was only a implementation for Java. Now, there's a "beta" release for C#, but I didn't tested.

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