Explicit interface implementation

As they say: you live and learn. Some time ago I wrote about why I don’t like VB.NET, where I was complaining about strange VB.NET behaviour with implementing interfaces. And recently I found out that in C# there is also something called Explicit Interface Implementation.

So if we take example from my previous post:

interface IDoNothing
    {
        void Method();
    }

    class FirstClass : IDoNothing
    {
        public void Method()
        {
            Console.WriteLine("FirstClass.Method");
        }

        void IDoNothing.Method()
        {
            Console.WriteLine("FirstClass.IDoNothing.Method");
        }
    }
    class PrivateClass : IDoNothing
    {
        public void Method()
        {
            Console.WriteLine("PrivateClassClass.Method");
        }
    }

And then we can call it:

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("As Interface:");
            IDoNothing asInterface = new FirstClass();
            //Outputs: FirstClass.IDoNothing.Method
            asInterface.Method();

            Console.WriteLine("As Class:");
            FirstClass asClass = new FirstClass();
            //Outputs: FirstClass.Method
            asClass.Method();
            //Outputs: FirstClass.ThisIsMethod
            ((IDoNothing)asClass).Method();

            Console.WriteLine("As Private Class:");
            IDoNothing asPrivateClass = new PrivateClass();
            //Outputs: PrivatClass.Method
            asPrivateClass.Method();
        }
    }

It is quite similar to VB.NET, but for me it much more clearer and there are less possibilities for create obfuscated code (like defining private method which is actually public).
Czytaj dalej Explicit interface implementation

Dump data from SQL to CSV (or other format) with F#

Recently I started playing with F#. And when I looked up for a way of accessing data, I found something called TypeProvider. And I have to say it is amazing tool. Then I realized that I was looking for some simple way to dump part of my database to CSV file, so I can use this data in automated testing (I might write something about that in near future). And I could use F# to just do it!
The result of my work was code that could dump selected tables (or part of selected tables) into separate CSV files (or in any other format, if you change function that dumps data). It will automatically get all columns in your database, so you have to only list tables that you want to dump (or write some simple F# query to get only data you want – useful if you have tables with millions of records, and don’t want to dump all of them).

Here is description of how it is done: (or you can just look on full source code).
Czytaj dalej Dump data from SQL to CSV (or other format) with F#