Adapter or Wrapper

This is code possible through .NET, in that it bridges across to COM for automation in VBA.  This concept can be extended to work for many other COM-based applications.  This code allows the add-in to expose internal .NET-coded procedures to Excel COM, extending the use of the .NET code.

Salient Characteristic(s)

  • Handle interface between different, incompatible systems
  • The interface is required because COM is interface-based
  • Other code elements are required for this to work properly
    • Registering the DLL
    • Creating the AdapterForVBA class on startup
Code

using System;
using System.Runtime.InteropServices;

namespace DesignPatterns
{
    /// <summary>
    /// Interace to expose VSTO/COM obects for COM and Excel
    /// Used by class below
    /// </summary>
    [ComVisible(true)]
    [Guid("B523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IAdapterForVba
    {
        //signatures for methods acessible to VBA go here
        //exists here and in class
    }

    /// <summary>
    /// Class to expose items to VBA and other COM application
    /// </summary>
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class AdapterForVba : IAdapterForVba
    {
        public AdapterForVba()
        {
            //methods to expose go here
            //exists here and in interface
        }
    }
}

Popular posts from this blog

Multiton

Decorator Pattern