Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Created August 20, 2017 18:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfowl/29829253e4134b379007c541d720511c to your computer and use it in GitHub Desktop.
Save davidfowl/29829253e4134b379007c541d720511c to your computer and use it in GitHub Desktop.
// 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