Skip to main content

VBA versus .NET

Prototype

Whereas ICloneable will be used to return a shallow copy, the data object's attribute Serializable enables creation of a deep copy of the data object, instead of a reference:

Salient Chacteristic(s)
  • Shallow copies use the native IConeable Interface
  • Deep copies requires use of creation of new objects, not references
  • For this, use of Serializable attribute to create a new copies
Code

using System;
using System.IO;
using Serialization = System.Runtime.Serialization;

namespace DesignPatterns
{
    /// <summary>
    /// A serializable data object, the attribute
    ///  necessary for simple form of deep copy
    /// </summary>
    [Serializable]
    public class SerializableDataObject 
    {
        public SerializableDataObject(int objectId)
        {
        }

        private int _Id;
        public int Id
        {
            get
            {
                return _Id;
            }
            set
            {
                _Id = value;
            }
        }
    }

    public static class CommonMethod
    {
        /// <summary>
        /// Serialize and desserializes the DataObject
        ///  creating a new DataObject with the same values as the source
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T DeepClone<T>(this T obj)
        {
            using (var ms = new MemoryStream())
            {
                var bf = new Serialization.Formatters.Binary.BinaryFormatter();
                bf.Serialize(ms, obj);
                ms.Position = 0;
                return (T)bf.Deserialize(ms);
            }
        }
    }

    public class BasePrototype : ICloneable
    {
        /// <summary>
        /// Example object, for either shallow or deep copies
        /// </summary>
        private SerializableDataObject _DataObject;
        public SerializableDataObject DataObject
        {
            get 
            {
                return _DataObject;
            }
            set
            {
                _DataObject = value;
            }
        }
        
        /// <summary>
        /// Creates a DataObject when constructed
        ///  Classes inheriting this can do Cones, as shallow copies
        ///  or override Clone to create deep copy
        /// </summary>
        public BasePrototype()
        {
            _DataObject = new SerializableDataObject(1);
        }

        public virtual object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    /// <summary>
    /// Returns a shallow copy via the inherited Clone operation,
    ///  in that the clone operation has a reference to the base class DataObject
    /// </summary>
    public class ShallowPrototype: BasePrototype
    {
        public ShallowPrototype()
        {
        }

        public override object Clone()
        {
            return this.MemberwiseClone();
        }
    }


    /// <summary>
    /// Returns a deep copy, in that the clone operation of the DataObject
    ///  is new copy of the base DataObject
    /// </summary>
    public class DeepPrototype : BasePrototype
    {
        public DeepPrototype()
        {
        }

        /// <summary>
        /// Overrides base Clone operation to create a memberwise clone
        ///  but with a deep copy of the DataObject
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            //clones this, but changes data object to true copy
            // not just reference, of data object
            DeepPrototype tempThis = (DeepPrototype)this.MemberwiseClone();
            tempThis.DataObject.Id += 1;
            tempThis.DataObject = CommonMethod.DeepClone(tempThis.DataObject);
            return tempThis;
        }
    }
}

Comments

Popular posts from this blog

Decorator Pattern

This is an example of the Decorator pattern, in this case a decorator for ObservableCollection.  Working with WPF and ObservableCollection using threads, one will run into the problem whereby the ObservableCollection cannot be updated from outside the owning thread; Delegates and Invoke will not work. A solution is to contain and expand the class, as is done in this example on michIG's Blog . This linked file contains the original code in C#, as well as the same code converted to VB.NET. Salient Characteristic(s) Sets an internal pointer to the decorated object, sending method calls and property actions to the internal object Extends the object by wrapping it and adding some aspect handled by the decorator Code using System; using System.Collections.ObjectModel; using System.Windows.Threading; using System.Collections.Specialized; using System.ComponentModel; namespace DesignPatterns {     /// <summary>       /// This class is an...

James Igoe's Reviews > Thinking Architecturally

Thinking Architecturally by Nathaniel Schutta My rating: 4 of 5 stars An overview of architectural decisions, the politics and persuasion involved, and the needs to balance competing measures and attributes. A fairly easy read, but full of great suggestions, and, for many, reminders of how to handle being a senior developer or architect. View all my reviews

Migrating and Design Planning

I have been toying with the idea of migrating one of my sites to a better host - it was supported by Yahoo and now AAbaco - and implementing some newer technologies. Among products I have used at work or are working with peripherally, I am considering using ASP.NET MVC, Entity Framework, ReSTful API's, NoSQL, and Azure-hosted databases - it is currently a mixture of very low-end PHP, HTML5/CSS3, light Javascript, and MySQL - so I decided to write up an architectural diagram - it looks like any standard architecture, with maybe a few additional elements - to help with the planning: