Translate

Tuesday, May 29, 2012

Programming to an interface in C# (Composition in C#)

Once you start reading up on the basics of Software design patterns you would come across the concept of "Programming to an interface". What programming to an interface does is that, it lets your code change its behavior with least amount of re write. To demonstrate this, we will follow the evolution of code from beginner level code with no code reuse to the point where we program to an interface.

Code with no reuse

Consider the code below.


using System;


class EntryCLass
{
    static void Main(string[] args)
    {

        MotorCycle mcycle = new MotorCycle();
        mcycle.Move();//Displays Turn Wheels
    }

}

public abstract class Vehicles
{

   
}

public class Cars : Vehicles
{
    public  void Move()
    {
        Console.WriteLine("Turn Wheels");
    }
}

public class MotorCycle : Vehicles
{
    public  void Move()
    {
        Console.WriteLine("Turn Wheels");
    }
}


You would notice that the code in Method Move() is repeated. Duplicate code is a big no no!


Reusing Code by applying Inheritance

using System;


class EntryCLass
{
    static void Main(string[] args)
    {

        MotorCycle mcycle = new MotorCycle();
        mcycle.Move();//Displays Turn Wheels
    }

}

public abstract class Vehicles
{

    public void Move()
    {
        Console.WriteLine("Turn Wheels");
    }
}

public class Cars : Vehicles
{
    
}

public class MotorCycle : Vehicles
{
    
}

In the above code we have reused code by placing it in the parent class. Now what if we have to add a Boat class and a Ship class to this and both of them inherit from the class Vehicles? 

using System;

class EntryCLass
{
    static void Main(string[] args)
    {

        MotorCycle mcycle = new MotorCycle();
        mcycle.Move();//Displays Turn Wheels

        Boat myBoat=new Boat();
        myBoat.Move();//Displays Turn Wheels

        Ship myShip = new Ship();
        myShip.Move();//Displays Turn Wheels
    }

}

public abstract class Vehicles
{

    public void Move()
    {
        Console.WriteLine("Turn Wheels");
    }
}

public class Cars : Vehicles
{

}

public class MotorCycle : Vehicles
{

}


public class Boat : Vehicles
{
    
}



public class Ship : Vehicles
{

}


But a Boat doesn't have wheels! Instead it needs Turn Rotor in its Move method. Do you see how hard it is to extend the code when we apply inheritance? If we don't apply inheritance at all,  we can have extensible code, but then we would also end up with repeating code. 

So how do we re use code and make the code extensible at the same time? The answer is programming to an interface.

Program to an Interface


using System;

class EntryCLass
{
    static void Main(string[] args)
    {
        IMove ImoveWheels = new MoveByTurningWheels();
        IMove ImoveRotor = new MoveByTurningRotor();


        MotorCycle mcycle = new MotorCycle(ImoveWheels);
        mcycle.Move();//Displays Turn Wheels

        Boat myBoat = new Boat(ImoveRotor);
        myBoat.Move();//Displays Turn Rotor

        Ship myShip = new Ship(ImoveRotor);
        myShip.Move();//Displays Turn Rotor
    }

}

public interface IMove
{
    void Move();
}


public class MoveByTurningWheels : IMove
{
    public void Move()
    {
        Console.WriteLine("Turn Wheels");
    }
}

public class MoveByTurningRotor : IMove
{
    public void Move()
    {
        Console.WriteLine("Turn Rotor");
    }
}

public abstract class Vehicles
{

    public abstract void Move();
}

public class Car : Vehicles
{
    IMove ImoveInternal;

    public Car(IMove Im)
    {
        ImoveInternal = Im;
    }

    public override  void  Move()
    {
        ImoveInternal.Move();
    }
}

public class MotorCycle : Vehicles
{
    IMove ImoveInternal;

    public MotorCycle(IMove Im)
    {
        ImoveInternal = Im;
    }

    public override  void  Move()
    {
        ImoveInternal.Move();
    }
}



public class Boat : Vehicles
{
    IMove ImoveInternal;

    public Boat(IMove Im)
    {
        ImoveInternal = Im;
    }

    public override  void  Move()
    {
        ImoveInternal.Move();
    }
}

public class Ship : Vehicles
{
    IMove ImoveInternal;

    public Ship(IMove Im)
    {
        ImoveInternal = Im;
    }

    public override  void  Move()
    {
        ImoveInternal.Move();
    }
}



Programming to an interface is a way to achieve composition. Its called composition because a class is composed of other classes.

Thursday, May 24, 2012

Create a custom event log in Windows 7 using C# (Log error in event log c#)

What I am going to show you in this article is how to create a custom log for your windows based application.





















The console application C# code below was used to achieve the above


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace CreateEventLog
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "mySource";
            string logName = "myLog";
            CreateCustomEvent(source, logName);
            LogAnEventInTheCustomEvent(source);
            // DeleteCustomEvent(source, logName);//Use this if you want to delete this custom log
        }


        static void CreateCustomEvent(string source, string logName)
        {

            if (!EventLog.SourceExists(source))
            {
                EventLog.CreateEventSource(source, logName);

            }

        }

        static void LogAnEventInTheCustomEvent(string source)
        {
            // Create an EventLog instance and assign its source.
            EventLog myLog = new EventLog();
            myLog.Source = source;

            // Write an informational entry to the event log.    
            myLog.WriteEntry("myInformation", EventLogEntryType.Information);

            // Write an error entry to the event log.    
            myLog.WriteEntry("myError", EventLogEntryType.Error);

        }

        static void DeleteCustomEvent(string source, string logName)
        {
            if (EventLog.SourceExists(source))
            {
                EventLog.DeleteEventSource(source);
                EventLog.Delete(logName);
            }

        }
    }
}



Another way to delete the custom event log is to delete the
[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\myLog]
log folder in the registry



Note that by default the maximum size allotted to an event log is 512 KB. Once this limit is reached the older entries start getting over written by the newer entries.

To increase the maximum space allotted to a windows event log use the code below


EventLog myLog = new EventLog();
myLog.Source = "mySource";
myLog.MaximumKilobytes = 10048;


This code will increase the maximum size allotted to the event with the source mySource to 10,048 KB (ie 10 MB). After that limit is crossed, old events would start getting overwritten.

Further reading
http://msdn.microsoft.com/en-us/library/2awhba7a.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.modifyoverflowpolicy(v=vs.110).aspx

Tuesday, May 15, 2012

Web Servers used by the top websites

As of today's date these are the web servers used by the top ten sites

Rank in May 2012
Site
Web Server



1
Google.com
GSE
2
Facebook.com
Hiphop
3
Youtube.com
Apache
4
Yahoo.com
YTS/1.20.10
5
Baidu.com
BWS/1.0
6
Apache
7
Live.com
IIS 7.5
8
Twitter.com
tfe
9
squid/3.1.18
10
Amazon.com
? (Linux based Webserver)






Web Servers of some other popular websites

Hotmail.com                      IIS 7.5
myspace.com                     IIS 7.5
cnn.com                             nginx
foxnews.com                     Apache
Reuters.com                      Apache
nytimes.com                      Sun-ONE-Web-Server/6.1
Hulu.com                          nginx
Netflix.com                       Apache-Coyote/1.1
Craigslist                          Apache
indeed.com                       Apache
dice.com                           Apache
ebay.com                          Apache-Coyote/1.1
mint.com                           Apache
bankofamerica.com           Apache
blogspot.com                    sffe