Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to support additional IDbConnection methods in Dapper (using DapperWrapper)?
    text
    copied!<p>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 <a href="https://github.com/half-ogre/dapper-wrapper/blob/master/README.markdown" rel="nofollow">acknowledges this problem</a> 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.</p> <p>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.</p> <pre class="lang-cs prettyprint-override"><code>private IDbExecutor _db; protected IDbExecutor Db { get { if (_db == null) { _db = DbConnectionFactory.GetConnection(); _db.Open(); } else if (_db != null &amp;&amp; _db.State != ConnectionState.Open) { _db.Open(); } return _db; } set { _db = value; } } </code></pre> <p>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?</p> <p><strong>Edit</strong>:</p> <p>Below is an example of a test I'm trying to run. I've created a TestExecutor class that implements IDbExecutor. In this case it's set up to return the test values we want - we'll probably use a factory to create it according to our test needs later. </p> <pre><code>[Test] public void TestGetAllDealsSuccess() { var executor = new TestExecutor(); var dealRepo = new DealRepository(executor); var deals = dealRepo.GetAllDeals(); //verify that expected deal data was returned } </code></pre> <p>The DealRepository is based on an AbstractRepository class that contains the DB property shown above.</p> <pre><code>protected AbstractBaseRepository(IDbExecutor conn) { _db = conn; } </code></pre> <p>Then in the DealRepository itself we run our queries against the DB object:</p> <pre><code>var results = Db.Query&lt;Deal&gt;("GetDeals", sprocParameters).ToList(); </code></pre> <p>When that Db property is called it runs the get code in my original excerpt above, which ensures the object exists and is open, and if not creates one and opens it. That's where we run into the problem - since the Open and State members don't exist on IDbExecutor.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload