tags:

views:

1117

answers:

6

Hi!

I have a program in which the user adds multiple objects to a scene. These objects are all represented as a classes. Now I want the user to be able to save the scenario, so it can be loaded again.

Is there some genereic save method I can use, or do I have to write my own that saves the values of all properties and such and then create new objects and insert the saved values into them when the user loads the scenario again.

/P

Edit: Thanks for the quick replys guys. I'm actually storing default values for the objects in a SQL compact 3.5 database right now. So if anyone knows how to serialize to SQL, that would be great info.

Edit 2: Thanks again! I'm going to serialize to xml, then store it as a string to the SQL database.

+2  A: 

Usually one would use a database to do this kind of thing. That's probably the recommended practise.

However if you want a quick and dirty method you can serialise to Xml or Binary with relatively little code. Check out the System.Runtime.Serialization stuff. The trade-off of Xml or Binary is that versioning sucks big time.

Consider a collection of xml files containing objects from your 1.0 app. If you've added fields to those objects since 1.0 then they wont be compatible with any new 1.1 objects. To get around this you have to write a manual update sequence from type 1.0 -> 1.1. Even worse if you use autodiscovery code (which automatically serialises) such as they xml or binary serializer you may even have to name your objects different names Product1 / Product1.1 / etc, it's either that or write your own serialisation code.

If your app starts approaching any level of seriousness I'd say you'll probably want to use a database. SqlLite is very nice and simple for 1 user apps, anything bigger and you'll want a proper RDBMS.

Quibblesome
The poster does not state if the application does not already use a database. If not, I would not recommend bringing one in. Serialise to file as XML. Just like MSWord.
Anthony
Yes.. but the poster seems to be en-route to making mistakes I made many years ago. So i'm just warning them of the trade-offs of storing objects in blobs.
Quibblesome
Also I very much doubt MSWord does a xmlSerialiser.Serialize(wordDocument). It probably rolls it's own custom serialisation.
Quibblesome
+5  A: 

Assuming your objects have all values available as public properties, you can use an XMLSerializer to convert the object to an XML string, then use a XMLDeserializer to re-create the object.

There is an extension method to accomplish this here.

If you have private variables, you can still write your own custom serialize/deserialize methods.

Edit in response to original question edit:

You can use any of these serialization techniques to store the string into a database. Another option would be to use an ORM (Object Relational Mapper) to bridge the gap between your objects and the databases.

Jason Z
A: 

XML Serialization Tutorial might be a good starting point.

+1  A: 

You'll need to look at .NET Serialization, which is a big topic. Here's an introductory tutorial.

Stewart Johnson
+1  A: 

You could also take a look at db4o, which is a simple object database.

mmiika
A: 

why not use memento pattern ..