2017-02-04

Polymorphism with interfaces

Howdy,
I recently ran into a need to perform polymorphism through an interface in C# 4.5.2. The wrinkle to this is that each implementation of the interface must have ta common signature for a constructor. Here's the how:

namespace generictrial01
{
    class Program
    {

        static void Main(string[] args)
        {
            AnyGoopWillDo agwd = new AnyGoopWillDo();
            AnyGoopWillDo agwd_1 = new AnyGoopWillDo(typeof(SpecialGoop));
            AnyGoopWillDo agwd_2 = new AnyGoopWillDo(typeof(NormalGoop));

            Console.WriteLine("default constructor");
            agwd.DoingTheObvious();
            Console.WriteLine("parameterized constructor - special");
            agwd_1.DoingTheObvious();
            Console.WriteLine("parameterized constructor - normal");
            agwd_2.DoingTheObvious();

            int i = 9;  // used as a breakpoint
        }

        /*
            Expected output:
                default constructor
                NormalGoop says "wow"
                parameterized constructor - special
                SpecialGoop says "yuck"
                parameterized constructor - normal
                NormalGoop says "wow"
        */
    }

    public interface IGoop
    {
        void PerformMiracle();
    }

    public class NormalGoop : IGoop
    {
        private string GoopString { get; set; }

        public NormalGoop()
        {
            GoopString = "wow";
        }

        public void PerformMiracle()
        {
            Console.WriteLine("NormalGoop says \"{0}\"", GoopString);
        }
    }

    public class SpecialGoop : IGoop
    {
        public void PerformMiracle()
        {
            Console.WriteLine("SpecialGoop says \"yuck\"");
        }
    }

    public class AnyGoopWillDo
    {
        private readonly Type _goopish;

        public AnyGoopWillDo()
        {
            _goopish = typeof(NormalGoop);
        }

        public AnyGoopWillDo(Type initType)
        {
            _goopish = initType;
        }

        public void DoingTheObvious()
        {
            UsingGoop test = new UsingGoop(_goopish);
            test.UseThisGoop();
        }
    }

    public class UsingGoop
    {
        private Type TheType { get; set; }

        public UsingGoop(Type initType)
        {
            TheType = initType;
        }
        public void UseThisGoop()
        {
            // this will determine if initType implements IGoop
            // see http://stackoverflow.com/questions/15138924/c-sharp-how-to-determine-if-a-type-implements-a-given-interface
            if (typeof (IGoop).IsAssignableFrom(TheType.UnderlyingSystemType))
            {
                IGoop iGoop = (IGoop) Activator.CreateInstance(TheType);
                iGoop.PerformMiracle();
            }
        }
    }
}

2015-03-11

Http time consumption

I did this as a refresher for raw sockets, and the TCP/IP stack. Written in C#, the following class can be used to tally the amount of time spent waiting for a site. It measures the time between the request, an HTTP GET, and the end of the response, the TCP FIN.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;           // for IPEndPoint
using System.Net.Sockets;   // for socket
using System.IO;

namespace User.Network
{
    class SocketMonitor
    {
        // members
        private bool _bIsRunning;
        private DateTime _dtLastReset;
        private DateTime _dtTimeStart;
        private DateTime _dtTimeEnd;
        private TimeSpan _tsCumulative;
        private TimeSpan _tsMax;
        private TimeSpan _tsMin;
        private TimeSpan _tsLast;

        // properties
        public IPAddress ipaTarget { get; private set; }
        public int iPort { get; private set; }

        public DateTime dtLastReset
        {
            get
            {
                return _dtLastReset;
            }
            private set
            {
                _dtLastReset = value;
            }
        }

        public TimeSpan tsCumulative
        {
            get
            {
                return _tsCumulative;
            }
            private set
            {
                _tsCumulative = value;
            }
        }

        public TimeSpan tsMax
        {
            get
            {
                return _tsMax;
            }
            private set
            {
                _tsMax = value;
            }
        }

        public TimeSpan tsMin
        {
            get
            {
                return _tsMin;
            }
            private set
            {
                _tsMin = value;
            }
        }

        public TimeSpan tsLast
        {
            get
            {
                return _tsLast;
            }
            private set
            {
                _tsLast = value;
            }
        }

        // constructor
        public SocketMonitor(IPAddress target, int port)
        {
            ipaTarget = target;
            iPort = port;
            _bIsRunning = false;
            _dtTimeStart = DateTime.MinValue;
            _dtTimeEnd = DateTime.MinValue;
            Reset();
        }

        // member methods
        // timer control
        public void Reset()
        {
            tsCumulative = TimeSpan.Zero;
            tsMax = TimeSpan.Zero;
            tsMin = TimeSpan.MaxValue;
            tsLast = TimeSpan.Zero;
            dtLastReset = DateTime.Now;
        }// of method Reset()

        private void Start()
        {
            if (!_bIsRunning)
            {
                _bIsRunning = true;
                _dtTimeStart = DateTime.Now;
            }
        }// of method Start()

        private void Stop()
        {
            if(_bIsRunning)
            {
                _bIsRunning = false;
                _dtTimeEnd = DateTime.Now;
                TimeSpan duration = _dtTimeEnd - _dtTimeStart;
                if (duration > TimeSpan.Zero)
                {
                    tsLast = duration;
                    tsCumulative += duration;
                    if (duration > tsMax) tsMax = duration;
                    if (duration < tsMin) tsMin = duration;
                    Console.WriteLine("{0} Total: {1}, Max: {2}, Min: {3}, Last: {4}",
                        DateTime.Now.ToShortTimeString(), tsCumulative, tsMax, tsMin, tsLast);
                }
            }
        }// of method Stop()

        // executable
        // much of this taken from here: https://social.msdn.microsoft.com/Forums/en-US/6d19f326-0d8f-4672-90f9-29d3497fc803/raw-sockets-promiscuous-mode-only-sees-datagrams-tofrom-single-machine?forum=ncl
        public void Run()
        {
            int len_receive_buf = 4096;
            byte[] receive_buf = new byte[len_receive_buf];
            int cout_receive_bytes;
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
            socket.Blocking = false;
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true);
            socket.ReceiveBufferSize = 65535;
            // the following linq frag was found here: http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c
            IPAddress thisMachine = Dns.GetHostEntry(Dns.GetHostName()).AddressList.
                Where(o => o.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).First();
            IPEndPoint ipEP = new IPEndPoint(thisMachine, 0); //(IPAddress.Any, 0);
            socket.Bind(ipEP);
            // found this here: http://www.progamercity.net/code-tut/5370-c-creating-simple-network-sniffer.html
            byte[] byTrue = new byte[4] { 1, 0, 0, 0 };
            byte[] byOut = new byte[4];
            socket.IOControl(IOControlCode.ReceiveAll, byTrue, byOut);
            while (true)
            {
                IAsyncResult ar =
                    socket.BeginReceive(receive_buf, 0, len_receive_buf, SocketFlags.None, null, this);
                cout_receive_bytes = socket.EndReceive(ar);
                Receive(receive_buf, cout_receive_bytes);
            }
        }// of method Run()

        private void Receive(byte[] buf, int len)
        {
            if ((len >= 20) && ((buf[0] >> 4) == 4))
            {
                //check IPv4 only; header info
                int IHL = buf[0] & 0xf; // number of words in header
                int byIHL = IHL * 4; // number of bytes in header
                IPAddress ipSender = new IPAddress((long)BitConverter.ToUInt32(buf, 12));
                IPAddress ipReceiver = new IPAddress((long)BitConverter.ToUInt32(buf, 16));
                // here are the protocol numbers: http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
                if (buf[9] == 6)
                {
                    // tcp = 6; header info
                    int sourcePort = buf[byIHL] << 8 | buf[byIHL + 1];
                    int destPort = buf[byIHL + 2] << 8 | buf[byIHL + 3];
                    int dataOffset = buf[byIHL + 12] >> 4; // number of words in TCP header
                    int flags = (buf[byIHL + 12] & 1) << 8 | buf[byIHL + 13];
                    int byDataOffset = (dataOffset * 4) + byIHL; // offset past the IP & tcp headers
                    if ((ipReceiver.ToString() == ipaTarget.ToString()) && (destPort == iPort))
                    {
                        // from us -- looking for the GET in the payload
                        if (len > byDataOffset)
                        {
                            // a payload exists
                            byte[] payload = new byte[len - byDataOffset];
                            for (int index = 0; index < len - byDataOffset; index++)
                            {
                                payload[index] = buf[index + byDataOffset];
                            }
                            string outline1 = Encoding.ASCII.GetString(payload);

                            if (outline1.TrimStart().ToUpper().StartsWith("GET"))
                            {
                                // found the get... start the timer
                                Start();
                            }
                        }
                    }

                    if ((ipSender.ToString() == ipaTarget.ToString()) && (sourcePort == iPort))
                    {
                        // to us -- looking for a FIN flag (flags, bit 0)
                        if ((flags & 1) == 1)
                        {
                            // found the fin... stop the timer & save
                            Stop();
                        }
                    }
                }
            }
        }// of method Receive(byte[], int)

    }
}
Have fun!
Steve



Breakpoints

Getting breakpoints to work with Classic ASP and ASP.NET 2.0, with IIS running locally.
  1. Ensure web.config has debug="True"
  2. Ensure that you are attaching to the correct w3wp.exe process
  3. Ensure that the App Pool configuration that is associated with this site in IIS has Maximum Worker Processes set to 1 (found in the Process Model section).
  4. Ensure that the ASP configuration for the site in IIS allows Client and Server debugging (found in the Debugging Properties section).
  5. Ensure that machine.config does not have retail="True" in the deployment tag. The default for this attribute is false, which will permit debugging.

2013-08-01

C# Tree

This is the construction of the Tree class presented at this URL:
http://dvanderboom.wordpress.com/2008/03/15/treet-implementing-a-non-binary-tree-in-c/ 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace UserDefined.Structures
{
    // basic multinode tree template
 
    public enum TreeTraversalType
    {
        TopDown,
        BottomUp
    }// of enum TreeTraversalType
 
    public class TreeBasic001 <T> : TreeBasic001Node <T>
    {
        public TreeBasic001() {}// Default constructor
 
        public TreeBasic001(T rootNode)
        {
            Value = rootNode;
        }// of constructor TreeBasic001(T)
 
        // since this is derived from TreeBasic001Node, it shares the copy constructor of that class
    }// of class TreeBasic
 
    public class TreeBasic001NodeList<T> : List<TreeBasic001Node<T>>
    {
        public TreeBasic001Node<T> Parent;
 
        public TreeBasic001NodeList(TreeBasic001Node<T> Parent)
        {
            this.Parent = Parent;
        }// of constructor TreeBasic001NodeList(TreeBasic001Node<T>)
 
        public TreeBasic001NodeList(TreeBasic001Node<T> newParent, TreeBasic001NodeList<T> otherList)
        {
            this.Parent = newParent;
            foreach (TreeBasic001Node<T> otherChild in otherList)
            {
                this.Add(new TreeBasic001Node<T>(otherChild));
            }
        }// of copy constructor TreeBasic001NodeList(TreeBasic001NodeList<T>)
 
        public new TreeBasic001Node<T> Add(TreeBasic001Node<T> Node)
        {
            base.Add(Node);
            Node.Parent = Parent;
            return Node;
        }// of method Add(TreeBasic001Node<T>)
 
        public TreeBasic001Node<T> Add(T Value)
        {
            return Add(new TreeBasic001Node<T>(Value));
        }// of method Add(T)
 
        public override string ToString()
        {
            return "Count=" + Count.ToString();
        }// of method ToString()
    }// of class TreeBasic001NodeList<T>
 
    public class TreeBasic001Node<T> : IDisposable
    {
 
        private TreeBasic001Node<T> _Parent;
        public TreeBasic001Node<T> Parent
        {
            get { return _Parent; }
            set
            {
                if (value == _Parent)
                {
                    return;
                }
                
                if (_Parent != null)
                {
                    _Parent.Children.Remove(this);
                }
 
                if (value != null && !value.Children.Contains(this))
                {
                    value.Children.Add(this);
                }
 
                _Parent = value;
            }
        }// of Property TreeBasic001Node<T> Parent
 
        public TreeBasic001Node<T> Root
        {
            get
            {
                //return (Parent == null) ? this : Parent.Root;
                TreeBasic001Node<T> node = this;
 
                while (node.Parent != null)
                {
                    node = node.Parent;
                }
    
                return node;
            }
        }// of Property TreeBasic001Node<T> Root
          
        private TreeBasic001NodeList<T> _Children;
 
        public TreeBasic001NodeList<T> Children
        {
            get { return _Children; }
            private set { _Children = value; }
        }// of Property TreeBasic001NodeList<T> Children
 
        private T _Value;
 
        public T Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                /* more advanced implementation
                if (_Value != null && _Value is ITreeNodeAware<T>)
                {
                    (_Value as ITreeNodeAware<T>).Node = this;
                }*/
            }
        }// of Property Value
 
        public int Depth
        {
            get
            {
                //return (Parent == null ? -1 : Parent.Depth) + 1;
                int depth = 0;
                TreeBasic001Node<T> node = this;
                while (node.Parent != null)
                {
                    node = node.Parent;
                    depth++;
                }
 
                return depth;
            }
        }// of Property Depth
 
        public TreeBasic001Node()
        {
            this.Value = default(T);
            Parent = null;
            Children = new TreeBasic001NodeList<T>(this);
        }// of constructor TreeBasic001Node()
 
        public TreeBasic001Node(T Value)
        {
            this.Value = Value;
            Parent = null;
            Children = new TreeBasic001NodeList<T>(this);
        }// of constructor TreeBasic001Node(T)
 
        public TreeBasic001Node(T Value, TreeBasic001Node<T> Parent)
        {
            this.Value = Value;
            this.Parent = Parent;
            Children = new TreeBasic001NodeList<T>(this);
        }// of constructor TreeBasic001Node(T, TreeBasic001Node<T>)
 
        public TreeBasic001Node(TreeBasic001Node<T> otherNode)
        {
            this.Value = otherNode.Value;
            this.Parent = otherNode.Parent;
            this.Children = new TreeBasic001NodeList<T>(this, otherNode.Children);
        }// of copy constructor TreeBasic001Node(TreeBasic001Node<T>)
 
        public override string ToString()
        {
            string Description = string.Empty;
            if (Value != null)
            {
                Description = "[" + Value.ToString() + "] ";
            }
 
            return Description + "Depth=" + Depth.ToString() + ", Children="
              + Children.Count.ToString();
        }// of method ToString()
 
        private TreeTraversalType _DisposeTraversal = TreeTraversalType.BottomUp;
        public TreeTraversalType DisposeTraversal
        {
            get { return _DisposeTraversal; }
            set { _DisposeTraversal = value; }
        }// of Property DisposeTraversal
 
        private bool _IsDisposed;
 
        public bool IsDisposed
        {
            get { return _IsDisposed; }
        }// of Property IsDisposed
 
        public void Dispose()
        {
            CheckDisposed();
            OnDisposing();
 
            // clean up contained objects (in Value property)
            if (Value is IDisposable)
            {
                if (DisposeTraversal == TreeTraversalType.BottomUp)
                {
                    foreach (TreeBasic001Node<T> node in Children)
                    {
                        node.Dispose();
                    }
                }
 
                (Value as IDisposable).Dispose();
 
                if (DisposeTraversal == TreeTraversalType.TopDown)
                {
                    foreach (TreeBasic001Node<T> node in Children)
                    {
                        node.Dispose();
                    }
                }
            }// of if T implements IDisposable
 
            _IsDisposed = true;
        }// of method Dispose()
 
        public event EventHandler Disposing;
 
        protected void OnDisposing()
        {
            if (Disposing != null)
            {
                Disposing(thisEventArgs.Empty);
            }
        }// of event signal OnDisposing()
 
        public void CheckDisposed()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }
        }// of method CheckDisposed()
 
    }// of class TreeBasic001Node<T>
 
 
}// of namespace UserDefined.Structures
 

2013-01-27

TSQL Comma Separated List

Howdy! I ran across this TSQL snippet that renders a column as a comma delimited list (see this):

DECLARE @TeamInfo varchar(max)

SELECT @TeamInfo = COALESCE(@TeamInfo + ', ', '') + FirstName
FROM TeamInfo
WHERE TeamID = @TeamID


I've run across jQuery components that prefer these lists, and this is a great way to build them.


Steve

2012-06-23

Entity Framework v. Data Access Layer

A recent undertaking of mine involves a paradigm move into a different framework. My traditional approached, in the IIS/ASP environment, has been to use ASP.NET rendering with the traditional HTML/C#.NET or VB.NET code-behind. Instead, I have a project that is based on MVC, or more specifically, Microsoft's MVC framework in ASP.NET.
MVC is an acronym for Model-View-Controller. This paradigm is a class-centric view of the typical client-server model. For instance, in ASP.NET, the markup and code-behind draw a division that is hardware-centric. The concept of MVC is scalable - it can be applied to an individual control as well as an entire interface. In this overall concept, the Controller receives the requests from external actors and interprets those requests as modifications to the data (Model) or the View or both. Applying this to a single textbox control gives the focus/blur and keyboard handler to the controller; the text buffer is the model, with the size and other rendering delegated to the view.
Microsoft's MVC is not so conceptual. It is a specific implementation of the MVC concept to web renderings at the client-server level. In this case, a web site may contain a series of models. The input marshalling to these controllers is controlled through a router, which is largely hidden. The router operates on a subscription basis, which is in contrast to the discovery protocols used with the network devices that bear the same name. The subscription is caused by the signature of methods within the controller.
Though the View can be rendered using ASP.NET controls, MVC also has a new rendering engine, called Razor. This engine allows the user to more fluidly mix markup, Javascript, and code-behind. There is a price for this: Microsoft gets to guess what it's currently parsing. In my short stint using Razor, I've come across two issues with rendering abnormalities. The first was parentheses pairs in the data were being interpreted as Javascript function calls. Replacing the parens with brackets fixed that. The other involves Razor's string sterilization. By default, Razor will strip all potential cross site scripting (XSS) threats from strings. This can be specifically overriden for a given control, but there is no partial whitelist/blacklist for it. Instead of the familiar asp: controls, Razor has a parade of helper methods for control rendering. It also makes substantial use of jQuery. Despite some quirky issues with bad guesses, Razor seems to allow a code structure that is more closely related to the Document Object Model (DOM).
This brings us to the Model. In its simplest form, the Model is just a class with properties and no methods - a struct. From this, you could construct a data access layer in the controller to manipulate the Model properties directly through a familiar SQLClient class. However, my particular project did not afford such familiarity; the original designers decided (did I mention that I inherited this project - well, I did) to use the acronym coupling of EF (Entity Framework) on ADO (ActiveX Data Object) using Linq for Entities, and this is the crux of this post. Instead of a very simple set of static methods in a class of their own to use as an API, I have spaghetti. Nicely frameworked spaghetti, but spaghetti none-the-less. The primary issue is the degree of overlap between the Entity Framework and the actual database. Changes must be accounted for on both sides, instead of just at the SQLCommand/Stored Procedure interface. Linq for Entities brings its own problems to the party, as well. It is possible, between MVC's use of lambda equations and Linq for Entities' obfuscation, to generate a SQL call that has no direct reference in the code.
Before this becomes a rant - avoid EF and Linq for Entities. They bring nothing new, other than obfuscation headaches. As for the rest of Microsoft's MVC on ASP.NET implementation - the jury is out. I'll see how this project progresses.

-- Steve

2012-03-24

Code Patterns: IndexOf()

The IndexOf() String method in C# and VB.NET should be accompanied with an "if" statement as a matter of practice.  There are some exceptions, and the absence of the conditional should be explained in the comments around that code section.  Here are two examples demonstrating a code section using IndexOf() in C#:

    // IndexOf() with trinary conditional
    String strTest = "123456, 7890";
    int intCommaPosition = strTest.IndexOf(',');
    if (intCommaPosition > 0)
    {
        // stuff to do if the comma is found
    }
    else if (intCommaPosition == 0)
    {
        // stuff to do if the comma is the first char
        //  typically, this is an unusual condition
    }
    else
    {
        // stuff to do if the comma is not found
    }// of trinary if-else if on IndexOf() return value

    // IndexOf() with binary conditional
    intCommaPosition = strTest.TrimStart(',')

                          .TrimStart().IndexOf(',');
    if (intCommaPosition > -1)
    {
        // stuff to do if the comma is found
    }
    else
    {
        // stuff to do if the comma is not found
    }// of binary if on IndexOf() return value


The trinary can intercept the condition where the character leads the string.  The binary removes leading characters like the one we are looking for, then any leading whitespace, and finally it looks for the character of interest.  This way, the binary conditional's intCommaPosition calculation removes the unlikely instance that the string strTest starts with a comma.

This example uses C#, though VB.NET is similar.  Please remember to force the constants to characters if using Visual Basic.  Here's an example:

    Dim strTest As String = "123456, 7890"
    Dim intCommaPosition As Integer = 0

    ' uses variant 4 of 9 - IndexOf(String)
    intCommaPosition = strTest.IndexOf(",")
    ' uses variant 1 of 9 - IndexOf(Char) - faster
    intCommaPosition = strTest.IndexOf(","c)
    ' ... if statements! ...


As a programmer, I discovered this back in 1971, using InStr$() functions with Basic.  Even though those days had me coding in a high school closet using a 75 baud ball-and-hammer teletype through a 300 baud acoustic coupler into a telephone line that had a good deal of crosstalk as it traversed the city to the University of Pittsburgh, with persistent storage on paper-clipped paper tapes fashionably tucked into my dress shirt pocket, the code pattern remains: do not assume that the character will be found!

Sorry for the geek visual; it was a fun time in those days, and it still is fun now.
-- Steve