title: Simple but good example on how to use Dapper with Structuremap and dependency injection
body: <p>I am trying to understand how to use Dependency Injection with Dapper (IDbConnection) and still being able to use built in dispose.</p>
  
  <p>I have found a couple of articles on the web but non that I think is easy to understand.</p>
  
  <p>What I am trying to figure out is how to make this simple class be testable:</p>
  
  <pre><code>public class UserProfileRepository : IUserProfileRepository
  {
      private readonly IConfigRepository _configRepository;
  
      public UserProfileRepository(IConfigRepository configRepository)
      {
          _configRepository = configRepository;
      }
  
      public UserProfile GetUserProfile(string userId)
      {
          const string query = @"Select UserId, UserName
                                  From Users
                                  Where UserId = @UserId";
  
          using (var conn = new SqlConnection(_configRepository.GetConnectionString("MyConnectionString")))
          {
              conn.Open();
              return conn.Query&lt;UserProfile&gt;(query, new { UserId = userId }).SingleOrDefault();
          }
      }
  }
  </code></pre>
  
  <p>I have a config repository that looks like this so I can mock the request to web.config away:</p>
  
  <pre><code>public class ConfigRepository : IConfigRepository
  {
      public string GetConnectionString(string key)
      {
          var conString = ConfigurationManager.ConnectionStrings[key];
          if (conString != null)
          {
              return conString.ConnectionString;
          }
  
          return string.Empty;
      }
  }
  </code></pre>
  
  <p>I have read that you could use ConnectionFactory but has not figur out how to implement it and still know I am disposing it correct.</p>
  
  <p>Can anyone point me in the right direction?</p>