Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created Aug 20, 2017
Embed
What would you like to do?
// Whatever shared state you need, the root of the universe
public class DapperContext
{
private QueryCache _cache = new QueryCache();
public static DapperContext Instance = new DapperContext();
public Task<IEnumerable<dynamic>> QueryAsync(IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
// Real logic happens here
}
}
// Extension methods act as sugar for accessing the "static" default query instance.
public static partial class SqlMapper
{
public static Task<IEnumerable<dynamic>> QueryAsync(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
return DapperContext.Instance.QueryAsync(conn, sql, param, transaction, commandTimeout, commandType);
}
}
// You now write unit tests against the DapperContext directly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment