【发布时间】:2014-09-18 20:58:27
【问题描述】:
我正在尝试在服务层上使用 Dapper。 IDbConnection 应该如何处理?
我在一个 MVC5 项目中使用这个,它带有 StructureMap for IoC。
我可以在服务器层类中注入它吗?或者可能是如下的 DapperWrapper:
public class DapperWrapper {
private ConnectionString { get { return Settings.ConnectionString; } }
public IEnumerable<T> Query<T>(String sql, dynamic parameters = null) {
IEnumerable<T> result;
using (IDbConnection connection = new SqlConnection(_connection)) {
connection.Open();
result = SqlMapper.Query<T>(connection, sql, parameters);
}
return result;
} // Query
// OTHER DAPPER METHODS
}
恐怕这种方法会打开和关闭许多连接。这是个问题吗?
我应该在 DapperWrapper 中注入连接吗?如果是,如何?
最好的方法是什么?
【问题讨论】:
-
stackoverflow.com/questions/9218847/… 有一些不错的解决方案
标签: inversion-of-control structuremap dapper