I am trying to use Dapper on a service layer. How should it handle the IDbConnection?
I am using this in a MVC5 project with StructureMap for IoC.
Can I inject it in the server layer classes? Or maybe a DapperWrapper as follows:
public class DapperWrapper {
private ConnectionString { get { return Settings.ConnectionString; } }
public IEnumerable<T> Query<T>(String sql, dynamic parameters = null) {
IEnumerable<T> result;
using (IDbConnection connection = new SqlConnection(_connection)) {
connection.Open();
result = SqlMapper.Query<T>(connection, sql, parameters);
}
return result;
} // Query
// OTHER DAPPER METHODS
}
I am afraid this approach opens and closes many connections. Is this a problem?
Should I inject a connection in the DapperWrapper, and if so, How?
What is the best way to do this?