Tuesday, August 6, 2013

How to support addition IDbConnection methods in Dapper (using DapperWrapper)?

How to support addition IDbConnection methods in Dapper (using
DapperWrapper)?

I'm attempting to create some unit tests for a repository that uses Dapper
methods, but I'm having trouble figuring out how to get my code to accept
a mocked DbConnection. I found that DapperWrapper acknowledges this
problem and accomplishes most of what I need, but the IDbExecutor
interface it provides does not include some of the base IDbConnection's
methods I need in my code to open and close the connection.
For example, below is a property from my base repository class that
defines the database connection used in all the repositories, retrieves it
from a factory, and Opens the connection if it's closed or doesn't yet
exist. This code doesn't work because IDbExecutor does not expose the Open
or Close methods, nor can there be a State property on the interface.
private IDbExecutor _db;
protected IDbExecutor Db
{
get
{
if (_db == null)
{
_db = DbConnectionFactory.GetConnection();
_db.Open();
}
else if (_db != null && _db.State != ConnectionState.Open)
{
_db.Open();
}
return _db;
}
set
{
_db = value;
}
}
Is there a way to extend or inherit the IDbExecutor in such a way that I
can reference these methods and properties for the SQL connection (and in
my mock connection object)? Or is there a better approach to this whole
problem?

No comments:

Post a Comment