The best way to store class instances to a file/database

落花浮王杯 提交于 2021-02-10 07:34:57

问题


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.


回答1:


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.




回答2:


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?




回答3:


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




回答4:


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.




回答5:


Serialization is a great way to store this type of data. See http://blog.paranoidferret.com/index.php/2008/06/20/csharp-tutorial-xml-serialization/




回答6:


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.



来源:https://stackoverflow.com/questions/355730/the-best-way-to-store-class-instances-to-a-file-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!