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 IEnumerableQueryByPredGroup(IPredicateGroup predGroup, List sort) { IEnumerable list = this.Connection.GetList (predGroup, sort); return list; } /// /// 查询返回List
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 IRepositorywhere 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; }}
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; } }}