/// <summary>/// DapperPlus讀接口/// </summary>public interface IDapperPlusRead{/// <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);//這時省略n個方法}
IDapperPlusWrite.cs
/// <summary>/// 寫Dapper接口/// </summary>public interface IDapperPlusWrite{/// <summary>/// 連接/// </summary>public IDbConnection Connection{get;}/// <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);/// <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);//這時省略n個方法}
/// <summary>/// DapperPlus讀實(shí)現(xiàn)類/// </summary>public class DapperPlusRead : IDapperPlusRead{protected IDbConnection _connection;/// <summary>/// 構(gòu)造/// </summary>/// <param name="connection">連接</param>/// <param name="configuration">配置</param>public DapperPlusRead(IDbConnection connection, IConfiguration configuration){????????????var connectionStrings = configuration.GetSection("ConnectionStrings").Get<Dictionary<string, string>>();_connection = connection;_connection.ConnectionString = connectionStrings.Where(s => s.Key.ToLower().Contains("read")).FirstOrDefault().Value; }///?<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);}//這時省略n個方法}
DapperPlusWrite.cs
/// <summary>/// 寫Dapper類/// </summary>public class DapperPlusWrite : IDapperPlusWrite{private readonly IDbConnection _connection;/// <summary>/// 構(gòu)造/// </summary>/// <param name="connection">連接</param>/// <param name="configuration">配置</param>public DapperPlusWrite(IDbConnection connection, IConfiguration configuration){var connectionStrings = configuration.GetSection("ConnectionStrings").Get<Dictionary<string, string>>();_connection = connection;_connection.ConnectionString = connectionStrings.Where(s => s.Key.ToLower().Contains("write")).FirstOrDefault().Value;}///?<summary>/// 連接/// </summary>public IDbConnection Connection{get{return _connection;}}/// <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);}/// <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);}//這時省略n個方法}