Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Tagged as

Achieve Persistence Through Serialization

, 6 Jan 2011 CPOL
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.Runtime.Serialization;

namespace Trestan
{
    // All the buildin types are supported.
    [Serializable]
    public class BuildinType///: ISerializable
    {
        static int instanceCount = 0;

        #region Test Case 16
        ///Test Case 16: Uncomment the constructor and GetObjectData(). Delete "Test1.dat". Repeat Test Case 15.
        ///Test Result: This time the value instanceCount should be 1.
        ///public BuildinType(SerializationInfo info, StreamingContext context)
        ///{
        ///    BuildinType.instanceCount = info.GetInt32("instanceCount");
        ///    instanceID = info.GetInt32("instanceID");
        ///    Number = (float)info.GetValue("Number", typeof(float));
        ///    Description = info.GetString("Description");
        ///}
        ///
        ///public void GetObjectData(SerializationInfo info, StreamingContext context)
        ///{
        ///    info.AddValue("instanceCount", instanceCount, typeof(int));
        ///    info.AddValue("instanceID", instanceID, typeof(int));
        ///    info.AddValue("Number", Number, typeof(float));
        ///    info.AddValue("Description", Description, typeof(string));
        ///}
        ///
        #endregion

        public BuildinType() { }

        int instanceID = instanceCount++;
        public int InstanceID
        {
            get { return instanceID; }
            set { instanceID = value; }
        }

        float number = 100.3F * instanceCount;
        public float Number
        {
            get { return number; }
            set { number = value; }
        }

        string description = "This is a test.";
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
    }

    [Serializable]
    public class DerivedClass : BuildinType
    {
        public enum STATE { INIT = 0, DONE = 1 };

        private STATE testState = STATE.DONE;
        public STATE TestState
        {
            get { return testState; }
            set { testState = value; }
        }

        public DerivedClass( Version theVersion)
        {
            testVersion = theVersion;
        }

        DateTime testTime = DateTime.Now;

        public DateTime TestTime
        {
            get { return testTime; }
            set { testTime = value; }
        }

        Font testFont = new Font("Times New Roman", 10F);
        public Font TestFont
        {
            get { return testFont; }
            set { testFont = value; }
        }

        //Does not need the conversion any more.
        //public Font ThisFont
        //{
        //    get { return thisFont; }
        //    set { thisFont = value; }
        //}

        //Private fields get serialized with BinaryFormatter.
        Version testVersion = Assembly.GetExecutingAssembly().GetName().Version;

        /// Test Case 14:Uncomment the following codes.Repeat Test Case 12. 
        /// Test Result: There should be an exception thrown. 
        /// Contrary to Test Case 6, this time you need to try the build-in types one by one to find out which are not supported:)
        ///Brush solidBeigeBrush = new SolidBrush(Color.Beige);
    }

    [Serializable]
    public class CollectionTypes
    {
        BuildinType test = new BuildinType();
        public BuildinType Test
        {
            get { return test; }
            set { test = value; }
        }

        BuildinType[] testArray = new BuildinType[10000];
        public BuildinType[] TestArray
        {
            get { return testArray; }
            set { testArray = value; }
        }

        List<BuildinType> testList = new List<BuildinType>();
        public List<BuildinType> TestList
        {
            get { return testList; }
            set { testList = value; }
        }

        // serializable with BinaryFormatter.
        Queue<BuildinType> testQueue = new Queue<BuildinType>();

        #region Test Case 17
        /// Test Case 17: Uncomment the following line of code. Run the program.
        /// Action: Notice the "time used" in console window, and find the output file "test4.dat".
        /// Test Result: Both the execution time and the file size are doubled.
        /// Dictionary<int, BuildinType> theDictionaryTest = new Dictionary<int, BuildinType>();        //uncomment to test Dictionary type.
        /// 
        #endregion

        //No longer needed.
        //public List<BuildinType> ListQueue
        //{
        //    get { return testQueue.ToList<BuildinType>(); }
        //    set
        //    {
        //        foreach (BuildinType one in value)
        //        {
        //            testQueue.Enqueue(one);
        //        }
        //    }
        //}

        public CollectionTypes()
        {
            for (int i = 0; i < 10000; i++)
            {
                BuildinType temp = new BuildinType();
                testArray[i] = temp;
                temp = new BuildinType();
                testQueue.Enqueue(temp);
                BuildinType test1 = new BuildinType();

                /// Test Case 17: Uncomment the following line of code.
                /// theDictionaryTest.Add(i, test1);  //uncomment to test it.
            }

            testList.AddRange(testArray);
            testList.AddRange(testArray);
        }

        public void Dispose()
        {
            testList.Clear();
            testQueue.Clear();
        }
    }
}

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)

Share

About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 6 Jan 2011
Article Copyright 2011 by trestan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid