views:

411

answers:

6

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.

+2  A: 

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

m0rb
I've updated the question, sorry :p
TimothyP
+3  A: 

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.

Kknd
+3  A: 

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.

Tim Matthews
Hey, even though I already knew that, it's a nice response.I think LINQ2SQL and the Entity Framework provide a nice "in-between".I'll look into the new license of db4o though, thnx.(Used it in the past where licenses weren't an issue)
TimothyP
+2  A: 

Hi TimothP,

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 funtions on it, e.g. compression or encryption.

Hope that helps, Prensen

Prensen
It really does, thank you!
TimothyP
+2  A: 

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/

Michiel
+3  A: 

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?

Marc Gravell
Ah indeed, Google Protocol Buffer, forgot about that, thank you!
TimothyP