Skip to main content

VBA versus .NET

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 available from one of my other sites.
  • Some classes are not available in this example, i.e., TrackedConnections, CubeInfo, and ConfigurationList, since they are part of the larger project, and would detract his example
Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.ServiceModel;
using System.Threading;

namespace DesignPatterns
{
    public interface ISubscriber
    {
        string UserId { get; set; }
        string Workstation { get; set; }
        string Title { get; set; }
        string Host { get; set; }
        string Port { get; set; }

        bool IsConnected { get; }
        TrackedConnections Tracker { get; set; }

        [OperationContract(IsOneWay = false)]
        void Attach(string userId, string workstation, string application);

        # region Methods used for Excel clients, used with delegate for status updates

        [OperationContract(IsOneWay = true)]
        void Update(IList<CubeInfo> message);

        [OperationContract(IsOneWay = false)]
        void Run();

        [OperationContract(IsOneWay = false)]
        void SendWorkbokForTracking(TrackedConnection item);

        UserConfigurationList Configuration { get; set; }

        # endregion

        # region Methods for legacy clients - single execution with return

        [OperationContract(IsOneWay = false)]
        IList<CubeInfo> GetAvailableCubeStatus();

        [OperationContract(IsOneWay = false)]
        CubeInfo GetActiveCube();

        # endregion

        # region Load Status methods - returns slices to load information

        [OperationContract(IsOneWay = false)]
        DataSet GetLoadStatusByDate(string viewDate);

        [OperationContract(IsOneWay = false)]
        DataSet GetLoadStatusBySource(string viewDate);

        [OperationContract(IsOneWay = false)]
        DataSet GetLoadStatusByRegion(string viewDate);

        # endregion
    }

    interface ISubscriberFacade
    {
        event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        DispatchingObservableCollection<CubeInfo> ActiveCubes { get; }
        DispatchingObservableCollection<CubeInfo> CubeList { get; }
        CubeInfo ActiveCube { get; }

        void Connect();
        void Detach();
        void Dispose();

        string Host { get; }
        string Port { get; }

        bool Status { get; }
        DateTime TimeStamp { get; }
        string AsString { get; }
        string ToString();
        bool UseWindowsAuthentication { get; }
    }
}

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

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...