mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	FreeSql.DbContext 内部仓储融合
This commit is contained in:
		@@ -13,8 +13,8 @@ namespace FreeSql {
 | 
			
		||||
		internal IFreeSql _orm;
 | 
			
		||||
		internal IFreeSql _fsql => _orm ?? throw new ArgumentNullException("请在 OnConfiguring 或 AddFreeDbContext 中配置 UseFreeSql");
 | 
			
		||||
 | 
			
		||||
		Object<DbConnection> _conn;
 | 
			
		||||
		DbTransaction _tran;
 | 
			
		||||
		UnitOfWork _uowPriv;
 | 
			
		||||
		internal UnitOfWork _uow => _uowPriv ?? (_uowPriv = new UnitOfWork(_fsql));
 | 
			
		||||
 | 
			
		||||
		static ConcurrentDictionary<Type, PropertyInfo[]> _dicGetDbSetProps = new ConcurrentDictionary<Type, PropertyInfo[]>();
 | 
			
		||||
		protected DbContext() {
 | 
			
		||||
@@ -34,8 +34,6 @@ namespace FreeSql {
 | 
			
		||||
				prop.SetValue(this, set);
 | 
			
		||||
				AllSets.Add(prop.Name, set);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			//_fsql.Aop.ToList += AopToList;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		protected virtual void OnConfiguring(DbContextOptionsBuilder builder) {
 | 
			
		||||
@@ -68,49 +66,18 @@ namespace FreeSql {
 | 
			
		||||
			_actions.Enqueue(new ExecCommandInfo { actionType = actionType, dbSet = dbSet, stateType = stateType, state = state });
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		void ReturnObject() {
 | 
			
		||||
			_fsql.Ado.MasterPool.Return(_conn);
 | 
			
		||||
			_tran = null;
 | 
			
		||||
			_conn = null;
 | 
			
		||||
		}
 | 
			
		||||
		internal DbTransaction GetOrBeginTransaction(bool isCreate = true) {
 | 
			
		||||
 | 
			
		||||
			if (_tran != null) return _tran;
 | 
			
		||||
			if (isCreate == false) return null;
 | 
			
		||||
			if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
 | 
			
		||||
 | 
			
		||||
			_conn = _fsql.Ado.MasterPool.Get();
 | 
			
		||||
			try {
 | 
			
		||||
				_tran = _conn.Value.BeginTransaction();
 | 
			
		||||
			} catch {
 | 
			
		||||
				ReturnObject();
 | 
			
		||||
				throw;
 | 
			
		||||
			}
 | 
			
		||||
			return _tran;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		void Commit() {
 | 
			
		||||
			if (_tran != null) {
 | 
			
		||||
				try {
 | 
			
		||||
					_tran.Commit();
 | 
			
		||||
				} finally {
 | 
			
		||||
					ReturnObject();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		void Rollback() {
 | 
			
		||||
			_actions.Clear();
 | 
			
		||||
			if (_tran != null) {
 | 
			
		||||
				try {
 | 
			
		||||
					_tran.Rollback();
 | 
			
		||||
				} finally {
 | 
			
		||||
					ReturnObject();
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		~DbContext() {
 | 
			
		||||
			this.Dispose();
 | 
			
		||||
		}
 | 
			
		||||
		bool _isdisposed = false;
 | 
			
		||||
		public void Dispose() {
 | 
			
		||||
			//_fsql.Aop.ToList -= AopToList;
 | 
			
		||||
			this.Rollback();
 | 
			
		||||
			if (_isdisposed) return;
 | 
			
		||||
			try {
 | 
			
		||||
				_uow.Rollback();
 | 
			
		||||
			} finally {
 | 
			
		||||
				_isdisposed = true;
 | 
			
		||||
				GC.SuppressFinalize(this);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user