最稳的pk10计划iphone 北京pk10计划手机软件 北京pk10数字的规律 超神手机版pk10软件 pk10北京赛车9码技巧 pk10四期倍投计划表 pk10极速赛车论坛 北京赛车冠军怎样选5码 北京赛车系统下载安装 pk10教程视频 北京pk10选号公式 北京赛车pk10赚钱技巧 北京赛车怎么提升概率 pk10技巧北京快三 北京pk10大小计划
VB.net 2010 視頻教程 VB.net 2010 視頻教程 python基礎視頻教程
SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
當前位置:
首頁 > 編程開發 > c#教程 >
  • C#教程之[開源]Dapper Repository 一種實現方式

  • 2019-04-13 21:49 來源:未知
  1. 接著上篇[開源]Entity Framework 6 Repository 一種實現方式
  2. 由于Dapper 本身就是輕量級Orm特性,這里參考Creating a Data Repository using Dapper dynamic queries in dapper 代碼,來解決實體類與Expression<Func<T, bool>> predicate問題;
  3. 您可以通過Nuget:Install-Package MasterChief.DotNet.Core.Dapper;
  4. 您可以通過GitHub:MasterChief 查看具體源碼以及單元測試;
  5. 歡迎Star,歡迎Issues;

插播一條求職#

  1. 小弟擁有多年C#開發經驗,從事過路燈,消防平臺物聯網平臺開發,坐標上海;
  1. 如果貴司在招聘,煩請大佬考慮下,聯系郵箱:[email protected]

基于Dapper 的Repository實現#


	
Copy
public abstract class DapperDbContextBase : IDbContext { #region Constructors /// <summary> /// 構造函數 /// </summary> /// <param name="connectString">連接字符串</param> protected DapperDbContextBase(string connectString) { ConnectString = connectString; } #endregion Constructors #region Properties /// <summary> /// 獲取 是否開啟事務提交 /// </summary> public IDbTransaction CurrentTransaction { get; private set; } #endregion Properties #region Fields /// <summary> /// 當前數據庫連接 /// </summary> public IDbConnection CurrentConnection => TransactionEnabled ? CurrentTransaction.Connection : CreateConnection(); /// <summary> /// 獲取 是否開啟事務提交 /// </summary> public bool TransactionEnabled => CurrentTransaction != null; /// <summary> /// 連接字符串 /// </summary> protected readonly string ConnectString; #endregion Fields #region Methods /// <summary> /// 顯式開啟數據上下文事務 /// </summary> /// <param name="isolationLevel">指定連接的事務鎖定行為</param> public void BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.Unspecified) { if (!TransactionEnabled) CurrentTransaction = CreateConnection().BeginTransaction(isolationLevel); } /// <summary> /// 提交當前上下文的事務更改 /// </summary> /// <exception cref="DataAccessException">提交數據更新時發生異常:" + msg</exception> public void Commit() { if (TransactionEnabled) try { CurrentTransaction.Commit(); } catch (Exception ex) { if (ex.InnerException?.InnerException is SqlException sqlEx) { var msg = DataBaseHelper.GetSqlExceptionMessage(sqlEx.Number); throw new DataAccessException("提交數據更新時發生異常:" + msg, sqlEx); } throw; } } /// <summary> /// 創建記錄 /// </summary> /// <param name="entity">需要操作的實體類</param> /// <returns>操作是否成功</returns> public bool Create<T>(T entity) where T : ModelBase { ValidateOperator.Begin().NotNull(entity, "需要新增的數據記錄"); // insert single data always return 0 but the data is inserted in database successfully //https://github.com/StackExchange/Dapper/issues/587 //List<T> data = new List<T>() { entity }; return CurrentConnection.Insert(new List<T> {entity}, CurrentTransaction) > 0; #region 測試代碼 //string sql = @"INSERT INTO [dbo].[EFSample] // ([ID] // ,[CreateTime] // ,[ModifyTime] // ,[Available] // ,[UserName]) //VALUES // (@ID // ,@CreateTime // ,@ModifyTime // ,@Available // ,@UserName)"; //return CurrentConnection.Execute(sql, entity) > 0; #endregion 測試代碼 } /// <summary> /// 創建數據庫連接IDbConnection /// </summary> /// <returns></returns> public abstract IDbConnection CreateConnection(); /// <summary> /// 刪除記錄 /// </summary> /// <returns>操作是否成功</returns> /// <param name="entity">需要操作的實體類.</param> public bool Delete<T>(T entity) where T : ModelBase { ValidateOperator.Begin().NotNull(entity, "需要刪除的數據記錄"); return CurrentConnection.Delete(entity); } /// <summary> /// 執行與釋放或重置非托管資源關聯的應用程序定義的任務。 /// </summary> public void Dispose() { if (CurrentTransaction != null) { CurrentTransaction.Dispose(); CurrentTransaction = null; } CurrentConnection?.Dispose(); } /// <summary> /// 條件判斷是否存在 /// </summary> /// <returns>是否存在</returns> /// <param name="predicate">判斷條件委托</param> public bool Exist<T>(Expression<Func<T, bool>> predicate = null) where T : ModelBase { var tableName = GetTableName<T>(); var queryResult = DynamicQuery.GetDynamicQuery(tableName, predicate); var result = CurrentConnection.ExecuteScalar(queryResult.Sql, (object) queryResult.Param, CurrentTransaction); return result != null; } /// <summary> /// 根據id獲取記錄 /// </summary> /// <returns>記錄</returns> /// <param name="id">id.</param> public T GetByKeyId<T>(object id) where T : ModelBase { ValidateOperator.Begin().NotNull(id, "Id"); return CurrentConnection.Get<T>(id, CurrentTransaction); } /// <summary> /// 條件獲取記錄集合 /// </summary> /// <returns>集合</returns> /// <param name="predicate">篩選條件.</param> public List<T> GetList<T>(Expression<Func<T, bool>> predicate = null) where T : ModelBase { var tableName = GetTableName<T>(); var queryResult = DynamicQuery.GetDynamicQuery(tableName, predicate); return CurrentConnection.Query<T>(queryResult.Sql, (object) queryResult.Param, CurrentTransaction).ToList(); } /// <summary> /// 條件獲取記錄第一條或者默認 /// </summary> /// <returns>記錄</returns> /// <param name="predicate">篩選條件.</param> public T GetFirstOrDefault<T>(Expression<Func<T, bool>> predicate = null) where T : ModelBase { var tableName = GetTableName<T>(); var queryResult = DynamicQuery.GetDynamicQuery(tableName, predicate); return CurrentConnection.QueryFirst<T>(queryResult.Sql, (object) queryResult.Param, CurrentTransaction); } /// <summary> /// 條件查詢 /// </summary> /// <returns>IQueryable</returns> /// <param name="predicate">篩選條件.</param> public IQueryable<T> Query<T>(Expression<Func<T, bool>> predicate = null) where T : ModelBase { throw new NotImplementedException(); } /// <summary> /// 顯式回滾事務,僅在顯式開啟事務后有用 /// </summary> public void Rollback() { if (TransactionEnabled) CurrentTransaction.Rollback(); } /// <summary> /// 執行Sql 腳本查詢 /// </summary> /// <param name="sql">Sql語句</param> /// <param name="parameters">參數</param> /// <returns>集合</returns> public IEnumerable<T> SqlQuery<T>(string sql, IDbDataParameter[] parameters) { ValidateOperator.Begin() .NotNullOrEmpty(sql, "Sql語句"); var dataParameters = CreateParameter(parameters); return CurrentConnection.Query<T>(sql, dataParameters, CurrentTransaction); } /// <summary> /// 根據記錄 /// </summary> /// <returns>操作是否成功.</returns> /// <param name="entity">實體類記錄.</param> public bool Update<T>(T entity) where T : ModelBase { ValidateOperator.Begin().NotNull(entity, "需要更新的數據記錄"); return CurrentConnection.Update(entity, CurrentTransaction); } private DapperParameter CreateParameter(IDbDataParameter[] parameters) { if (!(parameters?.Any() ?? false)) return null; var dataParameters = new DapperParameter(); foreach (var parameter in parameters) dataParameters.Add(parameter); return dataParameters; } private string GetTableName<T>() where T : ModelBase { var tableCfgInfo = AttributeHelper.Get<T, TableAttribute>(); return tableCfgInfo != null ? tableCfgInfo.Name.Trim() : typeof(T).Name; } #endregion Methods }

使用方法#


	
Copy
public class SampleService : ISampleService { private readonly IDatabaseContextFactory _contextFactory; public SampleService(IDatabaseContextFactory contextFactory) { _contextFactory = contextFactory; } /// <summary> /// 創建 /// </summary> /// <param name="sample">EFSample</param> /// <returns></returns> public bool Create(EfSample sample) { using (IDbContext context = _contextFactory.Create()) { return context.Create(sample); } } /// <summary> /// 條件查詢 /// </summary> /// <param name="predicate">The predicate.</param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public EfSample GetFirstOrDefault(Expression<Func<EfSample, bool>> predicate = null) { using (IDbContext context = _contextFactory.Create()) { return context.GetFirstOrDefault(predicate); } } /// <summary> /// 根據主鍵查詢 /// </summary> /// <param name="id">The identifier.</param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public EfSample GetByKeyId(Guid id) { using (IDbContext context = _contextFactory.Create()) { return context.GetByKeyId<EfSample>(id); } } /// <summary> /// 條件查詢集合 /// </summary> /// <param name="predicate">The predicate.</param> /// <returns></returns> public List<EfSample> GetList(Expression<Func<EfSample, bool>> predicate = null) { using (IDbContext context = _contextFactory.Create()) { return context.GetList(predicate); } } /// <summary> /// 添加判斷是否存在 /// </summary> /// <param name="predicate">The predicate.</param> /// <returns></returns> public bool Exist(Expression<Func<EfSample, bool>> predicate = null) { using (IDbContext context = _contextFactory.Create()) { return context.Exist(predicate); } } /// <summary> /// 腳本查詢 /// </summary> /// <param name="sql">The SQL.</param> /// <param name="parameter">DbParameter[]</param> /// <returns></returns> public List<EfSample> SqlQuery(string sql, DbParameter[] parameter) { using (IDbContext context = _contextFactory.Create()) { return context.SqlQuery<EfSample>(sql, parameter)?.ToList(); } } /// <summary> /// 更新 /// </summary> /// <param name="sample">The sample.</param> /// <returns></returns> public bool Update(EfSample sample) { using (IDbContext context = _contextFactory.Create()) { return context.Update(sample); } } /// <summary> /// 事務 /// </summary> /// <param name="sample">The sample.</param> /// <param name="sample2">The sample2.</param> /// <returns></returns> public bool CreateWithTransaction(EfSample sample, EfSample sample2) { bool result; using (IDbContext context = _contextFactory.Create()) { try { context.BeginTransaction();//開啟事務 context.Create(sample); context.Create(sample2); context.Commit(); result = true; } catch (Exception) { context.Rollback(); result = false; } } return result; } /// <summary> /// 刪除 /// </summary> /// <param name="sample"></param> /// <returns></returns> public bool Delete(EfSample sample) { using (IDbContext context = _contextFactory.Create()) { return context.Delete(sample); } } }

結語#

  1. Dapper與Entity Framework都是通過IRepository實現,所以您可以通過Ioc切換;
  2. 該篇的單元測試寫法與上篇一致;
  3. 小弟不才,大佬輕拍;

作者:YanZhiwei

出處:https://www.cnblogs.com/MeetYan/p/10681353.html

本站使用「署名 4.0 國際」創作共享協議,轉載請在文章明顯位置注明作者及出處。


pk10赛车冠军技巧
最稳的pk10计划iphone 北京pk10计划手机软件 北京pk10数字的规律 超神手机版pk10软件 pk10北京赛车9码技巧 pk10四期倍投计划表 pk10极速赛车论坛 北京赛车冠军怎样选5码 北京赛车系统下载安装 pk10教程视频 北京pk10选号公式 北京赛车pk10赚钱技巧 北京赛车怎么提升概率 pk10技巧北京快三 北京pk10大小计划
时时彩不亏钱的买法 铁板鱿鱼现在还能赚钱吗 明星江苏麻将老是输 欢乐捕鱼人怎么刷金币 时时彩正规平台注册 香港赛马会官方网站82042 M5彩票手机登录 天宫109赚钱攻略 养啥牛赚钱