Skip to main content

Posts

Showing posts from August, 2012

VBA versus .NET

Abstract Cat Factory (Amusement)

A friend amusingly posted on Facebook a retro photograph of four (4) 'designers' around a cat, with the caption alluding to designing hats for cats, and so I decided to make, somewhat incongruously, an abstract cat factory, which varies by the location of the cat..   Salient Characteristic(s) Classes derived from abstract types Class creates derived (concrete) classes based on type required Code namespace DesignPatterns {     public enum Location     {         Kitchen,         Bedroom,         LivingRoom     }     public enum Amusements     {         String,         Mouse,         Food,         Sleep     }          public abstract class Feline     {         public abstract Feline HereKittyKitty();     }     public class Cat : Feline     {         private Location _Location;         private Feline _Cat = null;         private Amusements _Amusement;         public Cat(Location location)         {             _Location = location;         }

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     {         //signatu

Facade

This is derived from actual code of mine, and elements of this sample combine the Facade pattern with the Publisher (Observer) pattern.  The primary class, based on the ISubscriber interface, is fairly complicated to use, requiring delegates, threading, and asynchronous callbacks.  The facade, based on ISubscriberFacade, encapsulates all the methods required to work with the Windows Communication Foundation (WCF) service , handling threading, delegate creation, and asynchronous callbacks internally, so that that the clients only need to create the object.  I wrote the encapsulating client to ease the adoption of the WCF service for legacy clients, seeing that the code complexity was likely a hurdle. Salient Characteristic(s) Reduces or hides complexities to other clients or systems Simplified interaction between systems and/or types Note DispatchingObservableCollection is based on ObservableCollection commonly used in Windows Presentation Foundation (WPF) applications and