Have you ever tried to test code that uses an implementation of IDataReader? Perhaps one that uses SqlCommand.ExecuteReader() to return an SqlDataReader. Such code is pretty hard to test because mocking or faking an IDataReader is pretty cumbersome. You’re forced to either mock every successive call to the reader’s Read() and Get*() methods, or you have to set up a DataTable (complete with dummy rows) and call its CreateDataReader() method.

To simplify all this I’ve created a ToDataReader() extension method which operates on IEnumerable<T>. You simply create an array of anonymous types and turn them into an IDataReader. Here’s some sample code to show you what I’m talking about:

var rows = new[]
{
    new { Id = 1, Name = "Foo" },
    new { Id = 2, Name = "Bar" }
};

using (var rdr = rows.ToDataReader()) 
{
    while (rdr.Read())
    {
        Console.WriteLine("{0}\t{1}", rdr.GetInt32(0), rdr.GetString(1));
    }
}

Pretty cool, huh? Provided you give your anonymous types the same order, property names and data types as the columns you expect your query to return, you can pass the ToDataReader() instance to any method expecting an IDataReader and it’ll return those values.

As an experiment, I’ve thrown the code (as well as a couple of other helpful extension methods on IDataRecord) onto the MSDN Code Gallery. You can reach it here: http://code.msdn.microsoft.com/datareaderext