Click here to Skip to main content
15,867,453 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.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace Trestan
{
    [Serializable]
    public sealed class Configuration
    {
        private static Configuration instance = null;

        private Configuration()
        {
        }

        public static Configuration Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = (Configuration)Utility.TDeSerialize("test.dat");
                }
                if (instance == null)
                {
                    instance = new Configuration();
                }
                return instance;
            }
        }

        int serverPort = 8000;
        public int ServerPort
        {
            get { return serverPort; }
            set { serverPort = value; }
        }

        int chatPort = 6055;
        public int ChatPort
        {
            get { return chatPort; }
            set { chatPort = value; }
        }

        string serverIP = "localhost";
        public string ServerIP
        {
            get { return serverIP; }
            set { serverIP = value; }
        }

        UserInfo currentUser = new UserInfo();

        public UserInfo CurrentUser
        {
            get { return currentUser; }
            set { currentUser = value; }
        }

        public void Save()
        {
            Utility.TSerialize(this, "test.dat");
        }
    }

    [Serializable]
    [DataContract]
    public class UserInfo
    {
        protected int userID = 0;

        [DataMember()]
        public int UserID
        {
            get { return userID; }
            set { userID = value; }
        }

        protected string userName;

        [DataMember()]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        protected bool moderator = false;

        [DataMember()]
        public bool Moderator
        {
            get { return moderator; }
            set { moderator = value; }
        }

        protected bool pending = false;

        [DataMember()]
        public bool Pending
        {
            get { return pending; }
            set { pending = value; }
        }
    }

}

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