Skip to content

Instantly share code, notes, and snippets.

@WilliamBerryiii
Created September 27, 2015 05:54
Show Gist options
  • Save WilliamBerryiii/d444f262444eb8e05249 to your computer and use it in GitHub Desktop.
Save WilliamBerryiii/d444f262444eb8e05249 to your computer and use it in GitHub Desktop.
An example repository with timing method wrapping the database calls.
public class Repository
{
private ISqlExecutioner _sqlExecutioner;
private string _connectionString;
private long _dbQueryRequestTime;
public long DbQueryRequestTime { get { return _dbQueryRequestTime; }}
public Repository (ISqlExecutioner sqlExecutioner, string connectionString)
{
_sqlExecutioner = sqlExecutioner;
_connectionString = connectionString;
}
public static DataSet GetSomeStuffFromTheDatabase(int thingId)
{
const string PROCEDURE_NAME = "GetMeSomeData";
var parameterLisst = new SqlParameterList();
parameterList.Add("@thing_id", SqlDbType.Int, thingId);
return TimeCall(() =>
_sqlExecutioner.ExecuteQuery(_connectionString, PROCEDURE_NAME, parameterList)
,ref _dbQueryRequestTime) ;
}
private static DataSet TimeCall(
Func<DataSet> func,
ref long dbQueryRequestTime)
{
var stopwatch = Stopwatch.StartNew();
var result = func();
stopwatch.Stop();
Interlocked.Add(ref dbQueryRequestTime, stopwatch.ElapsedTicks);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment