Extension Methods for Easy Data Extraction

Today I am continuing the theme of extension methods by talking about a few I primarily use for aiding in testing scenarios. Have you ever been in the situation where you are stepping through your code and get a set of data back from a SQL call and wish there was an easy way to save it so you could use it as test data? I have a few extension methods that make this situation much easier and all deal with a DataTable.

DataTable Extension Methods

public static void ToCSVFile(this DataTable dt, string fileName, char splitCharacter)
{
    File.WriteAllText(fileName, dt.ToCSVString(splitCharacter));
}
 
public static string ToCSVString(this DataTable dt, char splitCharacter)
{
    StringBuilder sb = new StringBuilder();
    string[] columnNames = dt.Columns.Cast<DataColumn>().
                                        Select(column => column.ColumnName).
                                        ToArray();
    sb.AppendLine(string.Join(",", columnNames));
    foreach (DataRow row in dt.Rows)
    {
        string[] fields = row.ItemArray.Select(field => field.ToString()).ToArray();
        sb.AppendLine(string.Join(",", fields));
    }
    return sb.ToString();
}
 
public static void FromCSVFile(this DataTable dt, string fileName, char splitCharacter)
{
    StreamReader sr = new StreamReader(fileName);
    dt.FromCSVString(sr.ReadToEnd(), splitCharacter);
}
 
public static void FromCSVString(this DataTable dt, string csvData, char splitCharacter)
{
    StringReader sr = new StringReader(csvData);
    string[] columns;
    string[] rows;
    string myStringRow = sr.ReadLine();
    columns = myStringRow.Split(splitCharacter);
 
    foreach (string column in columns)
    {
        dt.Columns.Add(column);
    }
    myStringRow = sr.ReadLine();
    while (myStringRow != null)
    {
        rows = myStringRow.Split(splitCharacter);
        dt.Rows.Add(rows);
        myStringRow = sr.ReadLine();
    }
}

Here I have four extension methods that make saving and loading a DataTable extremely easy. You can save to and read from a file or string that contains data separated by the delimiter of your choosing.

Usage

There are two ways to use these methods to save data off quickly, of course these methods can also be used for normal coding as well and not limited to the below scenarios.

  • Drop in a line of code right after you get the data back into your DataTable.
myTable.ToCSVFile(@"C:\Source\CSVTest.csv"',');
        // OR
string s = myTable.ToCSVString(',');
  • While debugging use the Immediate Window and write the same line after a break point has been hit, within scope of the DataTable, after it has been filled.

Using the Immediate Window to quickly save off data from a DataTable while stopped at a break point via Extension Methods.

Using ToCSVString and ToCSVFile from the Immediate Window is extremely useful because it does not require you to edit your code and can be used as an after though while debugging, Just In Time data extraction ;).

How is this helpful?

Once you have the data as a string or file you can use it in lots of different scenarios.

  • While stepping through code the data can be used to overwrite what is in a DataTable to try out different scenarios that may be hard to replicate.
  • You can use the string value in a unit test as part of your test setup data.
  • When using a test harness or mock you can load this data into the return variable.
  • During the exploratory phase of development simply add a line of code to load the data into a DataTable to see if what you are writing even works.

Summary

Having these extension methods handy has proven to be extremely useful in debugging, testing, and general coding. They help make life easier and development faster by saving time trying to create data sets from live scenarios and figuring out how to reuse that test data. These same ideas could easily apply to other collection types such as Lists, Arrays, and many more objects. The ability to quickly serialize data for later use is a need every developer runs into over and over again, why not keep around a few fancy extension methods to make life a little easier?

I would love to hear about any alternative methods that you use to quickly extract and save data on the spot such as when debugging. Let me know in the comments or on twitter@ScottKerlagon!

Guard Clauses Via Extension Methods

Extension methods are pretty cool. They allow you to add functionality to a class or to a type’s behavior. Extension methods usually make life much easier and your code cleaner. I am going to share a few extension methods that make repetitive guard clauses, shorter and easier to read.

Guard Clauses

Guard clauses are those checks at the top of a method to validate all the parameters being passed in. Most of the time, they are used to check if an object is null, a string is empty, or that a collection at least contains something and is not empty. When the parameters do not pass the checks usually some sort of Argument Exception should be thrown. This helps to validate all the parameters before your method begins working on them and provides a better explanation than what could possibly be thrown. Without guard clauses we would have null reference exceptions all over the place which are a pain to debug.

Below I have a method that prints a heading and then a list of names. I want to validate the heading and all the names before I actually print them out. The traditional way is to have code that looks like the following:

public static void PrintNames(string heading, ICollection<string> names)
{
    if (string.IsNullOrEmpty(heading))
        throw new ArgumentNullException("heading");
    if (names == null || names.Count == 0)
        throw new ArgumentNullException("names");
    foreach (string name in names)
    {
        if (string.IsNullOrEmpty(name))
            throw new ArgumentNullException("names has a null or empty item.");
    }

This is pretty ugly to have all over the place and this type of checking is extremely repetitive across all your methods. So I have a class of extension methods that make this cleaner and easier.

public static class GuardExtensions
{
    public static T GuardClauseIsNull<T>(this T parameter, string name) where T : class
    {
        if (parameter == null)
            throw new ArgumentNullException(name);
 
        return parameter;
    }
 
    public static string GuardClauseIsNullOrEmpty(this string parameter, string name)
    {
        if (string.IsNullOrEmpty(parameter))
            throw new ArgumentNullException(name);
        return parameter;
    }
 
    public static ICollection<T> GuardClauseCollectionEmptyOrNull<T>(this ICollection<T> list, string name)
    {
        if (list == null || list.Count == 0)
            throw new ArgumentNullException(name);
 
        return list;
    }
 
    public static ICollection<T> GuardClauseCollectionEmptyOrNullAndAnItemIsNull<T>(this ICollection<T> list, string name) where T : class
    {
        if (list == null || list.Count == 0)
            throw new ArgumentNullException(name);
 
        foreach (T item in list)
        {
            item.GuardClauseIsNull(name + " has a null item.");
        }
 
        return list;
    }
 
    public static ICollection<string> GuardClauseCollectionEmptyOrNullAndAnItemIsNullOrEmpty(this ICollection<string> list, string name)
    {
        if (list == null || list.Count == 0)
            throw new ArgumentNullException(name);
 
        foreach (string item in list)
        {
            item.GuardClauseIsNullOrEmpty(name + " has a null or empty item.");
        }
 
        return list;
    }
}

The first method is a generic extension method and works on any object that derives from class, which almost all do. All these methods take in the name of the parameter, to be used in the exception message. The others are variations of this logic that also check if a collection is null or empty and optional methods to check each item. I have found these variations to be useful in different cases. This makes the calling code cleaner and less repetitive as you will see next.

Usage

I made a simple console application to show you how this works in different cases. I have different cases with different variations of null objects and lists to test with.

class Program
{
    static void Main(string[] args)
    {
        List<string> myNames = new List<string>() { "Scott""James""Bob" };
        List<string> myNamesWithNull = new List<string>() { "Scott"null"Bob" };
        List<string> myNamesWithEmpty = new List<string>() { "Scott""""Bob" };
        List<string> emptyList = new List<string>();
 
        PrintNames("The Names are:", myNames);              //Run 1
        PrintNames(null, myNames);                         //Run 2
        PrintNames("The Names are:", myNamesWithNull);      //Run 3
        PrintNames("The Names are:", myNamesWithEmpty);     //Run 4
        PrintNames("The Names are:", emptyList);            //Run 5
        PrintNames("The Names are:"null);                  //Run 6
        Console.ReadKey();
    }
 
    public static void PrintNames(string heading, ICollection<string> names)
    {
        try
        {
            heading.GuardClauseIsNullOrEmpty("heading");
            names.GuardClauseCollectionEmptyOrNull("names");
            names.GuardClauseCollectionEmptyOrNullAndAnItemIsNull("names");
            names.GuardClauseCollectionEmptyOrNullAndAnItemIsNullOrEmpty("names");
        }
        catch (Exception guardException)
        {
            Console.WriteLine(guardException.GetType().ToString() + Environment.NewLine + guardException.Message);
            return;
        }
 
        Console.WriteLine(heading);
 
        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
    }
}

To save space after each run I comment out the previous runs. And the checking of the names collection would only use one of the extension methods instead of all three.

Output

Run 1

As you can see here the heading and names print out correctly.

Guard Clauses Run1

Run 2

Here the heading value is null which triggers an exception from our extension method.

Guard Clauses Run2

Run 3

In this run our extension methods catches that one of our names is null.

Guard Clauses Run3

Run 4

Here we catch that a name is an empty string. Not all cases would we care if it was empty but the extension methods give us that flexibility.

Guard Clauses Run4

Run 5

Here an empty list is caught.

Guard Clauses Run5

Run 6

And finally we see that a null list of names also throws an exception.

Guard Clauses Run6

Summary

Most of examples of extension methods are purely academic, such as counting words in a string or something that is generally just as useless. Here though these extensions for guard clauses can save us countless lines of repetitive code. Using the generic null check method covers the large majority of most guard clause usage. I am sure that you can come up with several other extension methods to cover specific types and use cases.

As always, let me know of ways to improve these guard clauses or your special use cases either in the comments or on twitter @ScottKerlagon!