Dapper. IoC, Testing and Agatha

agatha-rrsl dapper inversion-of-control testing


On a MVC 3 project I am using EF4, IoC and Agatha-RRSL as my service layer.

Fortunately, this week I found Dapper and I am moving from EF4 to Dapper!

Usually I inject a Generic Repository in my Agatha Request Handlers ...

But how should I use Dapper?

1) Should I inject a IDbConnection in the Agatha Handler? Then inside the handler I use it with Dapper Query or Dapper Execute? Is this testable? And what about mocking?

2) Should I create a generic repository for Dapper? Maybe the repository would be something like:

public class Repository {

  private IDbConnection _connection;

  public Repository(IDbConnection connection) {
    _connection = connection;
  } // Repository

  public Int32 Execute(String sql, dynamic param = null) {
    return _connection.Execute(sql, param); 
  } // Execute

  // Query code   

And the IDbConnection would be injected here.

And the repository would be injected inside the handlers.

Not sure how to test this ...

3) Should I just place all my code inside the handler?

using (SqlConnection connection = new SqlConnection(connectionString)) {
 connection.Execute(@"insert Roles(Name) values (@name)", new { name = "Role" }); 

But what about testing?

4) I have realize that Dapper uses static methods. Doesn't this raise some memory problems?

Sorry for so many questions ... I am trying to make this right.

Thank You, Miguel

Popular Answer

Late answer, but I want to point out that your repository implementation is really a "data access object". Repositories deal with whole aggregates, ie. a group of things that belong together. Ideally your interface should only accept and return objects of the aggregate root type (for example, a Repository<User> may have methods void Update(User user) {} or IEnumerable<User> Find(...) {}).

Usage or existence of any persistence mechanism is an implementation detail and would not typically belong in a repository interface, nor would it speak SQL.

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow