让Dapper支持Mock
Dapper,在.net的ORM中占有一席之地,用法简单,灵活,使用如下。但也带来一个问题,就是在单元测试时,Mock比较难办。
public List GetAllGoods()
{
using var con = new SqlConnection();
var sql = "select * from Goodses";
var list = con.Query(sql, new { id = 1 }).ToList();
return list;
}
mock最好是一个接口,但Dapper是基于IDbConnection做的扩展方法,为了能把这些方法的功能抽出来到一个接口,这里要经过一个设计,来转嫁调用这些扩展方法,具体做法如下:
定义接口IGUIDapper.cs
///
/// IDapperPlus接口
///
public interface IDapperPlus
{
///
/// 连接
///
public IDbConnection Connection
{
get;
}
///
/// Executes a single-row query, returning the data typed as type.
///
/// The type to return.
/// The SQL to execute for the query.
/// The parameters to pass, if any.
/// The transaction to use, if any.
/// Whether to buffer results in memory.
/// The command timeout (in seconds).
/// The type of command to execute.
/// A sequence of data of the supplied type; if a basic type(int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is created per row, and a direct column-name===member-name mapping is assumed(case insensitive).
/// 异常:T:System.ArgumentNullException:type is null.
///
IEnumerable<object> Query(Type type, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);
///
/// Execute parameterized SQL.
///
/// The SQL to execute for this query.
/// The parameters to use for this query.
/// The transaction to use for this query.
/// Number of seconds before command execution timeout.
/// Is it a stored proc or a batch?
/// The number of rows affected.
int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
//这里省略n多个方法
}
DapperPlus.cs接口的实现类,这里把对Dapper基于IDbConnection扩展方法的调用,转到方法的实现中调用,实现方法与扩展方法完全相同。
///
/// DapperPlus 类
///
public class DapperPlus : IDapperPlus
{
protected IDbConnection _connection;
///
/// 构造
///
/// 连接
/// 配置
public DapperPlus (IDbConnection connection, IConfiguration configuration)
{
_connection = connection;
_connection.ConnectionString = configuration.GetConnectionString("DefaultConnectionString");
}
///
/// 连接
///
public IDbConnection Connection
{
get
{
return _connection;
}
}
///
/// Executes a single-row query, returning the data typed as type.
///
/// The type to return.
/// The SQL to execute for the query.
/// The parameters to pass, if any.
/// The transaction to use, if any.
/// Whether to buffer results in memory.
/// The command timeout (in seconds).
/// The type of command to execute.
/// A sequence of data of the supplied type; if a basic type(int, string, etc) is queried then the data from the first column in assumed, otherwise an instance is created per row, and a direct column-name===member-name mapping is assumed(case insensitive).
/// 异常:T:System.ArgumentNullException:type is null.
///
public IEnumerable<object> Query(Type type, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
{
return _connection.Query(type, sql, param, transaction, buffered, commandTimeout, commandType);
}
///
/// Execute parameterized SQL.
///
/// The SQL to execute for this query.
/// The parameters to use for this query.
/// The transaction to use for this query.
/// Number of seconds before command execution timeout.
/// Is it a stored proc or a batch?
/// The number of rows affected.
public int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
return _connection.Execute(sql, param, transaction, commandTimeout, commandType);
}
//这里省略n多个实现方法
}
Starup.cs中注入就ok了
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped();
services.AddScoped();
}
appsettings.json
"ConnectionStrings": {
"DefaultConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=read_mysql_testdb"
}
想要更快更方便的了解相关知识,可以关注微信公众号