Click here to Skip to main content
15,892,059 members
Articles / Desktop Programming / WPF

DataObjectBase

Rate me:
Please Sign up or sign in to vote.
4.66/5 (20 votes)
18 May 2010CPOL8 min read 37.8K   572   59  
DataObjectBase -> the new Object class for data objects!
using System;
using System.Runtime.Serialization;
using CatenaLogic.Data;

namespace CatenaLogic.Examples.Data.OldNamespace
{
    /// <summary>
    /// Person class in an "old" (standard .NET) style.
    /// </summary>
    /// <remarks>
    /// All base functionality, such as the INotifyPropertyChanged, are not implemented in this class. The
    /// goal of this "old" style class is to show that objects that were previously binary serialized in a custom
    /// way, can easily be transformed into the DataObjectBase class.
    /// </remarks>
    [Serializable]
    public class Person : ISerializable
    {
        #region Variables
        #endregion

        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="Person"/> class.
        /// </summary>
        public Person()
        {
            // Set default values
            FirstName = "Geert";
            LastName = "van Horrik";
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Person"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="context">The context.</param>
        /// <remarks>
        /// Constructor required for serialization.
        /// </remarks>
        public Person(SerializationInfo info, StreamingContext context)
        {
            // Read all data from the info
            FirstName = (string)info.GetValue("FirstName", typeof (string));
            LastName = (string)info.GetValue("LastName", typeof(string));
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>The first name.</value>
        public string FirstName { get; set; }

        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>The last name.</value>
        public string LastName { get; set; }
        #endregion

        #region Methods
        #endregion

        #region ISerializable Members
        /// <summary>
        /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
        /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for this serialization.</param>
        /// <exception cref="T:System.Security.SecurityException">
        /// The caller does not have the required permission.
        /// </exception>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Write object data to info
            info.AddValue("FirstName", FirstName);
            info.AddValue("LastName", LastName);
        }
        #endregion
    }
}

namespace CatenaLogic.Examples.Data.NewNamespace
{
    /// <summary>
    /// Person Data object class which fully supports serialization, property changed notifications,
    /// backwards compatibility and error checking.
    /// </summary>
    /// <remarks>
    /// As you can see, the RedirectType attribute is used to redirect an old type to a complete new
    /// assembly, namespace or type name.
    /// </remarks>
    [Serializable]
    [RedirectType("CatenaLogic.Examples", "CatenaLogic.Examples.Data.OldNamespace.Person")]
    public class Person : DataObjectBase<Person>
    {
        #region Constructor & destructor
        /// <summary>
        /// Initializes a new object from scratch.
        /// </summary>
        public Person()
            : base() { }

        /// <summary>
        /// Initializes a new object based on <see cref="SerializationInfo"/>.
        /// </summary>
        /// <param name="info"><see cref="SerializationInfo"/> that contains the information.</param>
        /// <param name="context"><see cref="StreamingContext"/>.</param>
        public Person(SerializationInfo info, StreamingContext context)
            : base(info, context) { }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        public string FirstName
        {
            get { return GetValue<string>(FirstNameProperty); }
            set { SetValue(FirstNameProperty, value); }
        }

        /// <summary>
        /// Register the property so it is known in the class.
        /// </summary>
        public readonly PropertyData FirstNameProperty = RegisterProperty("FirstName", typeof(string), string.Empty);

        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        public string LastName
        {
            get { return GetValue<string>(LastNameProperty); }
            set { SetValue(LastNameProperty, value); }
        }

        /// <summary>
        /// Register the property so it is known in the class.
        /// </summary>
        public readonly PropertyData LastNameProperty = RegisterProperty("LastName", typeof(string), string.Empty);
        #endregion

        #region Serialization
        /// <summary>
        /// Retrieves the actual data from the serialization info.
        /// </summary>
        /// <param name="info"><see cref="SerializationInfo"/>.</param>
        /// <remarks>
        /// This method should only be implemented if backwards compatibility should be implemented for
        /// a class that did not previously implement the DataObjectBase class.
        /// </remarks>
        protected override void GetDataFromSerializationInfo(SerializationInfo info)
        {
            // Check if deserialization succeeded
            if (DeserializationSucceeded) return;

            // Deserialization did not succeed for any reason, so retrieve the values manually
            // Luckily there is a helper class (SerializationHelper) that eases the deserialization of "old" style objects
            FirstName = SerializationHelper.GetString(info, "FirstName", FirstNameProperty.GetDefaultValue<string>());
            LastName = SerializationHelper.GetString(info, "LastName", LastNameProperty.GetDefaultValue<string>());
        }
        #endregion
    }
}

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
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions