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

Do Algorithms Make You a Better Developer?

Responding to a question on HashNode, Developers who practise algorithms are better at software development than people who just do development. Is it true? , I wrote the following: My feeling is that algorithms help make one a better programmer, but that is likely true of many coding concepts. I did not have algorithms as an undergraduate, so my knowledge is acquired through reading and practice, but after reading and applying Algorithm's in a Nutshell, I felt the quality of my work improved. That said, my development work increased more after understanding Design Patterns, or after consuming books on database design.  Since many types of knowledge improve developing and architecting abilities, one has to consider how it helps and to what degree. Algorithms are coding-in-the-small, often narrowly focused solutions, but which can have a great impact at scale. For many applications, a focus on algorithms would be overkill as data sets and requirements do not require it. In this ...

VBA versus .NET

I was recently messaged by someone on LinkedIn, and since my response seemed full enough, I thought I'd share. Question I see that you also program in VBA but you have made the jump to .NET. Unfortunately, I have found C#/Excel coding to be quite slow and just wanted to hear about your experiences. Responses Slow? It depends on what you mean. Honest, I have had to make the pitch when building apps that it should be in .NET rather than VBA for speed. One particular app had a form that needed to fill about 20 dropdowns on load, so using async operations was essential. That same app, while executing one SQL statement in the foreground, also executed 2 background statements that filled panels. It wouldn't have performed well if done in VBA. If you mean that it takes longer, then yes, but that is a necessity for good code anyway. If you only need a local operation, non-threaded, that doesn't need to be used across the enterprise, VBA can make sense, but with .NET comes n...

How do you deal with making sure your use of new technology is correct and free from code-smells, security issues, etc.? - Hashnode

Responding to How do you deal with making sure your use of new technology is correct and free from code-smells, security issues, etc.? : Issues can be dealt with in several ways. Understanding what makes high-quality, maintainable code would be first, so knowledge of best practices regarding OOP, SOLID, design patterns, API design, etc. is important. Depending on what you mean by security, best practices in those regarding transfer protocols, coding styles, validation, storage, etc. are equally something one can learn. Planning your work is useful, as a well thought out design is easier to implement, or at least will avoid future problems, than when you are just 'winging it'. Diagramming and project plans can be useful at this stage. Self-management is part of this, so using boards and epic/stories/tasks to track work is important, and there are free tools like Visual Studio Team Services (VSTS) or Trello to help. Requirements gathering will matter so documentation and c...