博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dapper 仓储接口类 实现总结
阅读量:5275 次
发布时间:2019-06-14

本文共 9303 字,大约阅读时间需要 31 分钟。

using CustomerInterface;using Dapper;using System;using System.Collections.Generic;using System.Data;using System.Linq.Expressions;using System.Linq;using IITDeduction.CustomerDAL.Expressions;using DapperExtensions;using DapperExtensions.Mapper;using DapperExtensions.Sql;using IITDeduction.Common;namespace IITDeduction.CustomerDAL{    public class DapperRepository
: IDisposable,IRepository
where T : class { private IDbConnection _innerConn = null; private IDbTransaction _innerTran = null; private IDbConnection _refConn = null; private IDbTransaction _refTran = null; ///
/// 返回仓储类当前连接 /// public IDbConnection Connection { get { if (_refConn != null) { return _refConn; } else { return _innerConn; } } } ///
/// 返回仓储类当前事务 /// public IDbTransaction Transaction { get { if (_refTran != null) { return _refTran; } else { return _innerTran; } } } public DapperRepository() { _innerConn = DbConnectionFactory.CreateDbConnection(); _innerConn.Open(); _innerConn.Execute("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED"); } public DapperRepository(IDbConnection conn,IDbTransaction trans=null) { if (conn == null) { throw new Exception("conn can not be null!"); } if (trans != null) { if (trans.Connection != conn) { throw new Exception("trans'connection must be same as conn!"); } } _refConn = conn; _refTran = trans; } public void BeginTrans() { _innerTran = this.Connection.BeginTransaction(IsolationLevel.ReadUncommitted); } public void Rollback() { if (Transaction != null) { this.Transaction.Rollback(); } } public void Commit() { if (this.Transaction != null) { this.Transaction.Commit(); } } ///
/// 添加数据 /// ///
添加数据对象 ///
返回插入数据的主键
public dynamic Add(T entity) { return this.Connection.Insert
(entity,this.Transaction); } ///
/// 添加多组数据 /// ///
IEnumerable
///
public List
AddBatch(IEnumerable
entitys) { List
retVal = new List
(); foreach (T entity in entitys) { retVal.Add( Add(entity)); } return retVal; } ///
/// 更新数据 /// ///
///
bool
public bool Update(T entity) { return this.Connection.Update(entity,this.Transaction); } ///
/// 删除数据 根据对象删除 /// ///
///
bool
public bool Delete(T entity) { return this.Connection.Delete(entity, this.Transaction); } ///
/// 删除数据 根据主键Id删除 /// ///
///
public bool Delete(object predicate=null) { return this.Connection.Delete(predicate, this.Transaction); } ///
/// 获取数据 /// ///
///
public T Get(object Id) { return this.Connection.Get
(Id,this.Transaction); } ///
/// 返回所有数据 /// ///
public IEnumerable
GetAll() { return this.Connection.GetList
(this.Transaction); } public IEnumerable
GetList(string sql, object parameters = null) { return this.Connection.Query
(sql, parameters,this.Transaction); } public int Execute(string sql, object parameters = null) { return this.Connection.Execute(sql, parameters, this.Transaction); } public IEnumerable
GetList(IPredicateGroup predGroup, List
sort) { IEnumerable
list = this.Connection.GetList
(predGroup, sort, this.Transaction); return list; } public Tuple
> GetPage(IPredicateGroup predicate, int pageindex, int pageSize,List
sort) { var multi = this.Connection.GetPage
(predicate, sort, pageindex, pageSize,this.Transaction); var count = multi.Count(); var results = multi.ToList(); return new Tuple
>(count, results); } public PagedDataTable GetPagedTable(IPredicateGroup predicate, int pageindex, int pageSize, IList
sort) { var totalCount=this.Connection.Count
(predicate,this.Transaction); List
multi = this.Connection.GetPage
(predicate, sort, pageindex, pageSize, this.Transaction).ToList(); PagedDataTable retVal = new PagedDataTable() { Data=IITDeductionDataType.Convert
(multi), TotalCount = totalCount, PageIndex=pageindex, PageSize=pageSize }; return retVal; } public long Count(IPredicateGroup predicate) { return this.Connection.Count
(predicate, this.Transaction); } public object ExecuteScalar(string query, object parameters = null) { return this.Connection.ExecuteScalar(query, parameters,this.Transaction); } ///
/// 多条件组合查询 /// ///
///
IEnumerable
public IEnumerable
QueryByPredGroup(IPredicateGroup predGroup, List
sort) { IEnumerable
list = this.Connection.GetList
(predGroup, sort); return list; } ///
/// 查询返回List /// ///
自定义传输返回的Obect
/// querySql /// querySql参数 ///
public IEnumerable
Query
(string query, object parameters = null) where TAny : class { return Connection.Query
(query, parameters, Transaction); } ///
/// 通过Linq方式查询 /// ///
///
public T FirstOrDefault(Expression
> expression) { IPredicate ipredicate = expression.ToPredicateGroup(); var List = this.Connection.GetList
(ipredicate,null,this.Transaction).FirstOrDefault(); return List; } ///
/// 通过Linq获取LIST数据 /// ///
///
public IEnumerable
GetList(Expression
> expression) { IPredicate ipredicate = expression.ToPredicateGroup(); IEnumerable
list = this.Connection.GetList
(expression, null, this.Transaction); return list; } public string AddPageQuery(string sql) { string querySql ="select * from("+sql + @")AS RowConstrainedResult WHERE RowNum >= (@PageIndex * @PageSize + 1) AND RowNum <= (@PageIndex + 1) * @PageSize ORDER BY RowNum"; return querySql; } public void Dispose() { if (_innerTran != null) { _innerTran.Dispose(); _innerTran = null; } if (_innerConn != null) { _innerConn.Close(); _innerConn.Dispose(); _innerConn = null; } } }}
View Code
using DapperExtensions;using Spring.Web.UI;using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Linq.Expressions;using System.Text;namespace CustomerInterface{    public interface IRepository
where T : class { IDbConnection Connection{
get;} IDbTransaction Transaction{
get;} dynamic Add(T entity); List
AddBatch(IEnumerable
entitys); bool Update(T entity); bool Delete(T entity); bool Delete(object Id); T Get(object Id); IEnumerable
GetAll(); IEnumerable
GetList(string sql, object parameters = null); int Execute(string sql, object parameters = null); long Count(IPredicateGroup predicate); object ExecuteScalar(string query, object parameters = null); T FirstOrDefault(Expression
> expression); IEnumerable
GetList(Expression
> expression); IEnumerable
GetList(IPredicateGroup predGroup, List
sort); IEnumerable
Query
(string query, object parameters = null) where TAny : class; }}
View Code
using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Linq;using System.Text;namespace IITDeduction.CustomerDAL{    public class DbConnectionFactory    {        private static readonly string connectionString;        private static readonly string databaseType;        static DbConnectionFactory()        {            connectionString = ConfigurationManager.AppSettings["IITDeductionConnection"];            databaseType = ConfigurationManager.AppSettings["IITDeductionProvider"];        }        public static IDbConnection CreateDbConnection()        {            IDbConnection connection = null;            switch (databaseType)            {                case "system.data.sqlclient":                    connection = new System.Data.SqlClient.SqlConnection(connectionString);                    break;                case "mysql":                    //connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);                    break;                case "oracle":                    //connection = new Oracle.DataAccess.Client.OracleConnection(connectionString);                    //connection = new System.Data.OracleClient.OracleConnection(connectionString);                    break;                case "db2":                    connection = new System.Data.OleDb.OleDbConnection(connectionString);                    break;                default:                    connection = new System.Data.SqlClient.SqlConnection(connectionString);                    break;            }            return connection;        }    }}
View Code

 

转载于:https://www.cnblogs.com/MasterLin/p/11159013.html

你可能感兴趣的文章
string与stringbuilder的区别
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>
iOS8统一的系统提示控件——UIAlertController
查看>>
PAT甲级——1101 Quick Sort (快速排序)
查看>>
python创建进程的两种方式
查看>>
1.2 基础知识——关于猪皮(GP,Generic Practice)
查看>>
迭代器Iterator
查看>>
java易错题----静态方法的调用
查看>>
php建立MySQL数据表
查看>>
最简单的线程同步的例子
查看>>
旅途上看的电影和观后感
查看>>
Ztree异步树加载
查看>>
关于IE和火狐,谷歌,Safari对Html标签Object和Embed的支持问题
查看>>
poj3320 Jessica's Reading Problem(尺取思路+STL)
查看>>
分布式计算开源框架Hadoop介绍
查看>>
安卓平台接口剖析
查看>>
坏的事情不都会带来坏的结果
查看>>
RPC的基础:调研EOS插件http_plugin
查看>>
第二次团队冲刺第二天
查看>>
bzoj 2257 (JSOI 2009) 瓶子与燃料
查看>>