using System.Data; using Znyc.Recruitment.Admin.Commons.Core.DataManager; namespace Znyc.Recruitment.Admin.Commons.Core.Dapper { /// /// 注册的时候 InstancePerLifetimeScope /// 线程内唯一(也就是单个请求内唯一) /// public class DapperDbContext { private IDbConnection dbConnection { get; set; } /// /// 事务 /// public IDbTransaction DbTransaction { get; set; } /// /// 是否已被提交 /// public bool Committed { get; private set; } = true; /// /// 获取的数据库连接 /// /// /// /// public IDbConnection GetConnection(bool masterDb = true) where T : class { if (dbConnection == null || dbConnection.State == ConnectionState.Closed) { dbConnection = DBServerProvider.GetDBConnection(masterDb); } //if (MiniProfiler.Current != null) //{ // dbConnection = new StackExchange.Profiling.Data.ProfiledDbConnection((DbConnection)dbConnection, MiniProfiler.Current); //} return dbConnection; } /// /// 开启事务 /// public void BeginTransaction() { Committed = false; bool isClosed = dbConnection.State == ConnectionState.Closed; if (isClosed) { dbConnection.Open(); } DbTransaction = dbConnection?.BeginTransaction(); } /// /// 事务提交 /// public void CommitTransaction() { DbTransaction?.Commit(); Committed = true; Dispose(); } /// /// 事务回滚 /// public void RollBackTransaction() { DbTransaction?.Rollback(); Committed = true; Dispose(); } #region Dispose实现 private bool disposedValue; // 要检测冗余调用 /// /// 释放 /// /// protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: 释放托管状态(托管对象)。 } // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。 // TODO: 将大型字段设置为 null。 disposedValue = true; } if (dbConnection != null) { DbTransaction.Dispose(); dbConnection.Dispose(); } } /// /// 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。 /// public void Dispose() { // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。 Dispose(true); DbTransaction?.Dispose(); if (dbConnection.State == ConnectionState.Open) { dbConnection?.Close(); } // TODO: 如果在以上内容中替代了终结器,则取消注释以下行。 // GC.SuppressFinalize(this); } #endregion Dispose实现 } }