In my project I am using the following approach to querying data from the database:
If it suits your conditions, you can hijack generics to overload extension methods. Lets take the following example:
interface ISession
{
// session members
}
class FakeSession : ISession
{
public void Query()
{
Console.WriteLine("fake implementation");
}
}
static class ISessionExtensions
{
public static void Query(this ISession test)
{
Console.WriteLine("real implementation");
}
}
static void Stub1(ISession test)
{
test.Query(); // calls the real method
}
static void Stub2(TTest test) where TTest : FakeSession
{
test.Query(); // calls the fake method
}