Wonderful. IoC, testing and agate

In the MVC 3 project, I use EF4, IoC, and Agatha-RRSL as my level of service.

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

I usually add a common repository to Agatha request handlers ...

But how do I use Dapper?

1) Should I inject IDbConnection into an Agatha handler? Then inside the handler, do I use it with Dapper Query or Dapper Execute? Can this be verified? What about ridicule?

2) Should I create a shared repository for Dapper? Perhaps the repository will look something like this:

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 here the IDbConnection will be introduced.

And the repository will be introduced inside the handlers.

I don't know how to check this ...

3) Should I just put all the 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 understand that Dapper uses static methods. Doesn't that create a memory problem?

Sorry for so many questions ... I'm trying to get it right.

Thanks Miguel

+4
source share
1 answer

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

The use or existence of some kind of persistence mechanism is an implementation detail and usually does not apply to the repository interface and will not speak SQL.

0
source

Source: https://habr.com/ru/post/1403178/


All Articles