Recipe5.14.Persisting a Collection Between Application Sessions


Recipe 5.14. Persisting a Collection Between Application Sessions

Problem

You have a collection such as an ArrayList, List<T>, Hashtable, or Dictionary<T,U> in which you are storing application information. You can use this information to tailor the application's environment to the last known settings (e.g., window size, window placement, currently displayed toolbars). You can also use it to allow the user to start the application at the same point where it was last shut down. In other words, if the user is editing an invoice and needs to shut down the computer for the night, the application will know exactly which invoice to initially display when the application is started next time.

Solution

Serialize the object(s) to and from a file:

 public static void SaveObj<T>(T obj, string dataFile) {     FileStream FS = File.Create(dataFile);     BinaryFormatter binSerializer = new BinaryFormatter( );     binSerializer.Serialize(FS, obj);     FS.Close( ); } public static T RestoreObj<T>(string dataFile) {     FileStream FS = File.OpenRead(dataFile);     BinaryFormatter binSerializer = new BinaryFormatter( );     T obj = (T)binSerializer.Deserialize(FS);     FS.Close( );     return (obj); } 

Discussion

The dataFile parameter accepts a string value to use as a filename. The SaveObj<T> method accepts an object and attempts to serialize it to a file. Conversely, the RestoreObj<T> method removes the serialized object from the file created in the SaveObj<T> method.

The TestSerialization utility shown in Example 5-5 demonstrates how to use these methods to serialize a Hashtable object and a List<int> object (note that this will work for any type that is marked with the SerializableAttribute).

Example 5-5. Persisting a collection between application sessions

 public static void TestSerialization( ) {     // Create a Hashtable object to save/restore to/from a file.     Hashtable HT = new Hashtable( );     HT.Add(0, "Zero");     HT.Add(1, "One");     HT.Add(2, "Two");     // Display this object's contents and save it to a file.     foreach (DictionaryEntry DE in HT)         Console.WriteLine(DE.Key + " : " + DE.Value);     SaveObj(HT);     // Restore this object from the same file and display its contents.     Hashtable HTNew = new Hashtable( );     HTNew = (Hashtable)RestoreObj( );     foreach (DictionaryEntry DE in HTNew)         Console.WriteLine(DE.Key + " : " + DE.Value);     // Create a List<int> object to save/restore to/from a file.     Console.WriteLine( );     List<int> test = new List<int>( );     test.Add(1);     test.Add(2);     // Display this object's contents and save it to a file.     foreach (int i in test)         Console.WriteLine(i.ToString( ));     SaveObj<List<int>>(test, "TEST.DATA");     // Restore this object from the same file and display its contents.     List<int> testNew = new List<int>( );     testNew = RestoreObj<List<int>>("TEST.DATA");     foreach (int i in testNew)         Console.WriteLine(i.ToString( )); } 

If you serialize your objects to disk at specific points in your application, you can then deserialize them and return to a known state, for instance, in the event of an unintended shutdown.

If you rely on serialized objects to store persistent information, you need to figure out what you are going to do when you deploy a new version of the application. You should plan ahead with either a strategy for making sure the types you serialize don't get changed or a technique for dealing with changes. Otherwise you are going to have big problems when you deploy an update.


See Also

See the "ArrayList Class," "Hashtable Class," "List<T> Class," "Dictionary<T,U> Class," "File Class," and "BinaryFormatter Class" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net