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.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)

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.150224.1 | Last Updated 6 Jan 2011
Article Copyright 2011 by trestan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid