让Dapper支持Mock

  Dapper,在.net的ORM中占有一席之地,用法简单,灵活,使用如下。但也带来一个问题,就是在单元测试时,Mock比较难办。

 public List<Goods> GetAllGoods()
 {
     using var con = new SqlConnection();
     var sql = "select * from Goodses";
     var list = con.Query<Goods>(sql, new { id = 1 }).ToList();
     return list;
 }

  mock最好是一个接口,但Dapper是基于IDbConnection做的扩展方法,为了能把这些方法的功能抽出来到一个接口,这里要经过一个设计,来转嫁调用这些扩展方法,具体做法如下:

定义接口IGUIDapper.cs

  /// <summary>
    /// IDapperPlus接口
    /// </summary>
    public interface IDapperPlus
    {
        /// <summary>
        /// 连接
        /// </summary>
        public IDbConnection Connection
        {
            get;
        }

        /// <summary>
        /// Executes a single-row query, returning the data typed as type.
        /// </summary>
        /// <param name="type">The type to return.</param>
        /// <param name="sql">The SQL to execute for the query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="buffered">Whether to buffer results in memory.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <returns>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.
        /// </returns>
        IEnumerable<object> Query(Type type, string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);                       

        /// <summary>
        /// Execute parameterized SQL.
        /// </summary>
        /// <param name="sql">The SQL to execute for this query.</param>
        /// <param name="param">The parameters to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <param name="commandType">Is it a stored proc or a batch?</param>
        /// <returns>The number of rows affected.</returns>
        int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
      
        //这里省略n多个方法
    }

  DapperPlus.cs接口的实现类,这里把对Dapper基于IDbConnection扩展方法的调用,转到方法的实现中调用,实现方法与扩展方法完全相同。

 /// <summary>
    /// DapperPlus 类
    /// </summary>
    public class DapperPlus : IDapperPlus
    {
        protected IDbConnection _connection;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="connection">连接</param>
        /// <param name="configuration">配置</param>
        public DapperPlus (IDbConnection connection, IConfiguration configuration)
        {
            _connection = connection;
            _connection.ConnectionString = configuration.GetConnectionString("DefaultConnectionString");  
        }  
        /// <summary>
        /// 连接
        /// </summary>
        public IDbConnection Connection
        {
            get
            {
                return _connection;
            }
        }   
        /// <summary>
        /// Executes a single-row query, returning the data typed as type.
        /// </summary>
        /// <param name="type">The type to return.</param>
        /// <param name="sql">The SQL to execute for the query.</param>
        /// <param name="param">The parameters to pass, if any.</param>
        /// <param name="transaction">The transaction to use, if any.</param>
        /// <param name="buffered">Whether to buffer results in memory.</param>
        /// <param name="commandTimeout">The command timeout (in seconds).</param>
        /// <param name="commandType">The type of command to execute.</param>
        /// <returns>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.
        /// </returns>
        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);
        }
        /// <summary>
        /// Execute parameterized SQL.
        /// </summary>
        /// <param name="sql">The SQL to execute for this query.</param>
        /// <param name="param">The parameters to use for this query.</param>
        /// <param name="transaction">The transaction to use for this query.</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout.</param>
        /// <param name="commandType">Is it a stored proc or a batch?</param>
        /// <returns>The number of rows affected.</returns>
        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<IDbConnection, SqlConnection>();
    services.AddScoped<IDapperPlus, DapperPlus>();
}

appsettings.json

 "ConnectionStrings": {
    "DefaultConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=read_mysql_testdb"
  }

 

  想要更快更方便的了解相关知识,可以关注微信公众号    让Dapper支持Mock
上一篇:接口测试apifox


下一篇:让Dapper支持Mock