Click here to Skip to main content
15,881,516 members
Articles / Programming Languages / C#

Achieve Persistence Through Serialization

Rate me:
Please Sign up or sign in to vote.
4.60/5 (7 votes)
6 Jan 2011CPOL5 min read 29.5K   342   15  
This article compares the two common types of serialization in aspects of data access, readability, and runtime cost.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;

namespace Trestan
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (File.Exists("test1.dat"))
                {
                    #region Test Case 15
                    /// Test Case 15: Running this program will generate an output file "test1.dat".
                    /// The consequence execution will come into this block and reconstruct the object from storage.
                    /// Action: Check the value of InstanceCount in the debugger visualizer.
                    /// Test Result: The value of InstanceCount is 0, while it whould be 1 if it got serialized.
                    /// 
                    #endregion
                    BuildinType test3 = (BuildinType)Utility.DeSerializeWithDecrypt("test1.dat");
                }
                BuildinType test1 = new BuildinType();
                test1.Description = "Because binaryformatter can serialize private properties," +
                " we need to assign a value outside the class to proof that the value comes from the persistence storage.";
                Utility.SerializeWithEncrypt(test1, "test1.dat");
                BuildinType test2 = (BuildinType)Utility.DeSerializeWithDecrypt("test1.dat");
                Console.WriteLine("We did not assign a string value to test2.Description:\n" + test2.Description);
            }
            catch (Exception ex) { }

            try
            {
                #region Test Case 12
                /// Test Case 12: DerivedClass does not have a default constructor, which may fail with XmlSerializer as shown in Test Case 8.
                /// Action: Step through until the end of try block.
                /// Test Result: The test2 object is reconstructed successfully.
                /// 
                #endregion
                DerivedClass test1 = new DerivedClass(new Version("3.0.0.0")); 
                test1.Description = "Because binaryformatter can serialize private properties," +
                " we need to assign a value outside the class to proof that the value comes from the persistence storage.";
                test1.TestTime = DateTime.Now.AddHours(1);
                test1.TestFont = new Font("Arial", 12F);

                Utility.SerializeWithEncrypt(test1, "test2.dat");
                DerivedClass test2 = (DerivedClass)Utility.DeSerializeWithDecrypt("test2.dat");
                #region Test Case 11
                /// Test Case 11:Place a break point on the above line. Check the value in debugger visualizer after deserialize.
                /// Test Result: The value of test2.testVision should be "3.0.0.0", not the default value "1.0.0.0".
                /// 
                #endregion
            }
            catch (Exception ex) { }         

            long tick = DateTime.Now.Ticks;
            try
            {
                CollectionTypes test0 = new CollectionTypes();
                Utility.SerializeWithEncrypt(test0, "test3.dat");
                test0.Dispose();
                CollectionTypes test2 = (CollectionTypes)Utility.DeSerializeWithDecrypt("test3.dat");
            }
            catch (Exception ex) { }         

            Console.WriteLine("Time used on a single instance of CollectionTypes (seconds): " + (DateTime.Now.Ticks - tick) / 10000000F +"\n");
            tick = DateTime.Now.Ticks;

            #region Test Case 16
            /// Test Case 16: Run the program and pay attention to the "time used" value in console window. Find the output file test4.dat.
            /// Action: Compare the number with Test Case 9.
            /// Test Result: The time taken is half to one-third of the time used by XmlSerializer.
            /// Also the file size is one-tenth of the one generated by XmlSerializer.
            /// 
            #endregion
            try
            {
                Console.WriteLine("Test of an array containing 10 elements:");
                CollectionTypes[] test1 = new CollectionTypes[10];
                for (int i = 0; i < 10; i++)
                {
                    test1[i] = new CollectionTypes();
                }

                Console.WriteLine("Time used to create instance (seconds): " + (DateTime.Now.Ticks - tick) / 10000000F);
                
                Utility.SerializeWithEncrypt(test1, "test4.dat");
                CollectionTypes[] test2 = (CollectionTypes[])Utility.DeSerializeWithDecrypt("test4.dat");
            }
            catch (Exception ex) { }         

            Console.WriteLine("Time used total (seconds): " + (DateTime.Now.Ticks - tick) / 10000000F);
            Console.Read();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Canada Canada
Looking for something to do in the new year.

Comments and Discussions