Just because it is cleaner, I would take advantage of the fact that you’re using async and await to do away with the cast in the ContinueWith(). await evaluates to an object of type TResult when used on a Task<TResult>. I was going to suggest the syntax return (IDataReader)await dbCommand.ExecuteReaderAsync();, but then I remembered that the compiler already knows that DbDataReader is IDataReader. Tested in VS 2013 and VS 2015 Preview (not sure what you’re targeting, but I assume all C# compilers that support await should work with this):
public async static Task<IDataReader> ExecuteReaderAsync(this IDbCommand self) {
DbCommand dbCommand = self as DbCommand;
if (dbCommand != null) {
return await dbCommand.ExecuteReaderAsync();
} else {
return await Task.Run(() => self.ExecuteReader());
}
}
Now you’re using await to its fuller potential and saving a few bytes of code ;-).
The biggest concern with this implementation is, of course, the runtime type testing in self as DbCommand. In my opinion, DbCommand should be used instead of IDbCommand. This would let you remove the runtime cast. However, you probably wouldn’t have written this library if there was no problem with switching everything from IDbCommand to DbCommand and the runtime type check is probably performant enough.
Visual Studio 2017 Syntax
With newer versions of C#, you can use the is keyword instead of as to write more concise code:
public async static Task<IDataReader> ExecuteReaderAsync(this IDbCommand self) {
if (self is DbCommand dbCommand) {
return await dbCommand.ExecuteReaderAsync();
} else {
return await Task.Run(() => self.ExecuteReader());
}
}