mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	## v0.3.16
- 修复 IInsert/IUpdate.NoneParameter() 设成了反作用的 bug; - 修复 IDbFirst.GetTablesByDatabase() 默认数据库 bool 判断 bug; - 增加 FreeSql.Repository 之 IUnitOfWork 实现,[查看参数资料](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83); - 增加 FreeSql.Repository 继承实现的仓储注入; ```csharp builder.RegisterFreeRepository( filter => filter.Apply<Song>("test", a => a.Title == DateTime.Now.ToString() + Thread.CurrentThread.ManagedThreadId), this.GetType().Assembly ); ```
This commit is contained in:
		
							
								
								
									
										31
									
								
								FreeSql.Repository/UnitOfWork/IUnitOfWork.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								FreeSql.Repository/UnitOfWork/IUnitOfWork.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql {
 | 
			
		||||
	public interface IUnitOfWork : IDisposable {
 | 
			
		||||
 | 
			
		||||
		/// <summary>
 | 
			
		||||
		/// 在工作单元内创建默认仓库类,工作单元下的仓储操作具有事务特点
 | 
			
		||||
		/// </summary>
 | 
			
		||||
		/// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
		/// <typeparam name="TKey"></typeparam>
 | 
			
		||||
		/// <param name="filter">数据过滤 + 验证</param>
 | 
			
		||||
		/// <returns></returns>
 | 
			
		||||
		DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class;
 | 
			
		||||
 | 
			
		||||
		/// <summary>
 | 
			
		||||
		/// 在工作单元内创建仓库类,适用 Insert 方法无须返回插入的数据,工作单元下的仓储操作具有事务特点
 | 
			
		||||
		/// </summary>
 | 
			
		||||
		/// <typeparam name="TEntity"></typeparam>
 | 
			
		||||
		/// <param name="filter">数据过滤 + 验证</param>
 | 
			
		||||
		/// <param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
 | 
			
		||||
		/// <returns></returns>
 | 
			
		||||
		GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class;
 | 
			
		||||
 | 
			
		||||
		void Commit();
 | 
			
		||||
 | 
			
		||||
		void Rollback();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										70
									
								
								FreeSql.Repository/UnitOfWork/UnitOfWork.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								FreeSql.Repository/UnitOfWork/UnitOfWork.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,70 @@
 | 
			
		||||
using SafeObjectPool;
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data.Common;
 | 
			
		||||
using System.Linq.Expressions;
 | 
			
		||||
using System.Text;
 | 
			
		||||
 | 
			
		||||
namespace FreeSql {
 | 
			
		||||
	class UnitOfWork : IUnitOfWork {
 | 
			
		||||
 | 
			
		||||
		IFreeSql _fsql;
 | 
			
		||||
		Object<DbConnection> _conn;
 | 
			
		||||
		DbTransaction _tran;
 | 
			
		||||
 | 
			
		||||
		bool _isCommitOrRoolback = false;
 | 
			
		||||
 | 
			
		||||
		public UnitOfWork(IFreeSql fsql) {
 | 
			
		||||
			_fsql = fsql;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		DbTransaction BeginTransaction() {
 | 
			
		||||
			_conn = _fsql.Ado.MasterPool.Get();
 | 
			
		||||
			try {
 | 
			
		||||
				_tran = _conn.Value.BeginTransaction();
 | 
			
		||||
			} catch {
 | 
			
		||||
				_fsql.Ado.MasterPool.Return(_conn);
 | 
			
		||||
				_conn = null;
 | 
			
		||||
				throw;
 | 
			
		||||
			}
 | 
			
		||||
			return _tran;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public void Commit() {
 | 
			
		||||
			_isCommitOrRoolback = true;
 | 
			
		||||
			if (_conn != null) {
 | 
			
		||||
				try {
 | 
			
		||||
					_tran.Commit();
 | 
			
		||||
				} finally {
 | 
			
		||||
					_fsql.Ado.MasterPool.Return(_conn);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		public void Rollback() {
 | 
			
		||||
			_isCommitOrRoolback = true;
 | 
			
		||||
			if (_conn != null) {
 | 
			
		||||
				try {
 | 
			
		||||
					_tran.Rollback();
 | 
			
		||||
				} finally {
 | 
			
		||||
					_fsql.Ado.MasterPool.Return(_conn);
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		public void Dispose() {
 | 
			
		||||
			if (_isCommitOrRoolback == false) {
 | 
			
		||||
				this.Commit();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public DefaultRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class {
 | 
			
		||||
			var repos = new DefaultRepository<TEntity, TKey>(_fsql, filter);
 | 
			
		||||
			repos._tran = BeginTransaction();
 | 
			
		||||
			return repos;
 | 
			
		||||
		}
 | 
			
		||||
		public GuidRepository<TEntity> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class {
 | 
			
		||||
			var repos = new GuidRepository<TEntity>(_fsql, filter, asTable);
 | 
			
		||||
			repos._tran = BeginTransaction();
 | 
			
		||||
			return repos;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user