Utility class for Dapper ORM similar to repository pattern

c# dapper

Question

I have created below repository methods for Dapper ORM.

public SqlMapper.GridReader QueryMultiple(string query, object cmdParams = null, CommandType cmdType = CommandType.Text)
{
     SqlMapper.GridReader objMulti;
     var conn = GetSqlConnection();            
     objMulti = conn.QueryMultiple(query, cmdParams, commandTimeout: 0, commandType: cmdType);                                            
     return objMulti;
}

public IEnumerable<T> GetAll<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class
{
      IEnumerable<T> objList;
      using (var conn = GetSqlConnection())
      {
           objList = conn.Query<T>(query, param: cmdParams, commandTimeout:0, commandType: cmdType);
           conn.Close();
      }
      return objList;
}

Usage of query method looks fine and connection is closed as soon as the operation completes.

new DapperRepository().Query("zyx").ToList();

But querymultiple returns gridreader. So, does Dapper dispose both connection and reader after read operations are done. Please advise about my approach.

using (SqlMapper.GridReader multiResult = new DapperRepository().QueryMultiple(sql, new { id = id }))
{
   var dbPRTGroup = multiResult.Read<Customer>().Single();
    XXXXX
}

Accepted Answer

Yes, it does.

According to your last code block, both connection and reader operations are disposed at the end. This is also ensured through usage of using keyword in .NET Framework.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why