mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 18:52:50 +08:00
增加 UnitOfWorkManager 类管理工作单元,移除 FreeSql.UnitOfWork.Current 静态属性;
This commit is contained in:
parent
658540774e
commit
fc4e834452
Binary file not shown.
Binary file not shown.
@ -70,12 +70,12 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return Orm.Update<TEntity>(this as TEntity)
|
return Orm.Update<TEntity>(this as TEntity)
|
||||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
|
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
||||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrows() == 1;
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.IsDeleted = value;
|
this.IsDeleted = value;
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
return this.Repository.Update(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -89,7 +89,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
|
return Orm.Delete<TEntity>(this as TEntity).ExecuteAffrows() == 1;
|
||||||
//this.SetTenantId();
|
//this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.Delete(this as TEntity) == 1;
|
return this.Repository.Delete(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -107,11 +107,11 @@ namespace FreeSql
|
|||||||
this.UpdateTime = DateTime.Now;
|
this.UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return Orm.Update<TEntity>()
|
return Orm.Update<TEntity>()
|
||||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
|
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
||||||
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
.SetSource(this as TEntity).ExecuteAffrows() == 1;
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.Update(this as TEntity) == 1;
|
return this.Repository.Update(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -124,7 +124,7 @@ namespace FreeSql
|
|||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.Insert(this as TEntity);
|
return this.Repository.Insert(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ namespace FreeSql
|
|||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.InsertOrUpdate(this as TEntity);
|
return this.Repository.InsertOrUpdate(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +152,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
|
this.Repository.SaveMany(this as TEntity, navigatePropertyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,11 +53,11 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return await Orm.Update<TEntity>(this as TEntity)
|
return await Orm.Update<TEntity>(this as TEntity)
|
||||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
|
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
||||||
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
.Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
|
||||||
|
|
||||||
this.IsDeleted = value;
|
this.IsDeleted = value;
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -71,7 +71,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
|
return await Orm.Delete<TEntity>(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||||
//this.SetTenantId();
|
//this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return await this.Repository.DeleteAsync(this as TEntity) == 1;
|
return await this.Repository.DeleteAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -89,11 +89,11 @@ namespace FreeSql
|
|||||||
this.UpdateTime = DateTime.Now;
|
this.UpdateTime = DateTime.Now;
|
||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
return await Orm.Update<TEntity>()
|
return await Orm.Update<TEntity>()
|
||||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
|
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction())
|
||||||
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1;
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
return await this.Repository.UpdateAsync(this as TEntity) == 1;
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -106,7 +106,7 @@ namespace FreeSql
|
|||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.InsertAsync(this as TEntity);
|
return this.Repository.InsertAsync(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ namespace FreeSql
|
|||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.SetTenantId();
|
this.SetTenantId();
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.InsertOrUpdateAsync(this as TEntity);
|
return this.Repository.InsertOrUpdateAsync(this as TEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ namespace FreeSql
|
|||||||
if (this.Repository == null)
|
if (this.Repository == null)
|
||||||
this.Repository = Orm.GetRepository<TEntity>();
|
this.Repository = Orm.GetRepository<TEntity>();
|
||||||
|
|
||||||
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
|
this.Repository.UnitOfWork = CurrentUnitOfWork;
|
||||||
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
|
return this.Repository.SaveManyAsync(this as TEntity, navigatePropertyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,9 +106,11 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
var uow = Orm.CreateUnitOfWork();
|
var uow = Orm.CreateUnitOfWork();
|
||||||
uow.IsolationLevel = level;
|
uow.IsolationLevel = level;
|
||||||
|
CurrentUnitOfWork = uow;
|
||||||
return uow;
|
return uow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static readonly AsyncLocal<IUnitOfWork> _AsyncUnitOfWork = new AsyncLocal<IUnitOfWork>();
|
||||||
static readonly AsyncLocal<string> _AsyncTenantId = new AsyncLocal<string>();
|
static readonly AsyncLocal<string> _AsyncTenantId = new AsyncLocal<string>();
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取或设置当前租户id
|
/// 获取或设置当前租户id
|
||||||
@ -118,6 +120,14 @@ namespace FreeSql
|
|||||||
get => _AsyncTenantId.Value;
|
get => _AsyncTenantId.Value;
|
||||||
set => _AsyncTenantId.Value = value;
|
set => _AsyncTenantId.Value = value;
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置当前租户id
|
||||||
|
/// </summary>
|
||||||
|
public static IUnitOfWork CurrentUnitOfWork
|
||||||
|
{
|
||||||
|
get => _AsyncUnitOfWork.Value;
|
||||||
|
set => _AsyncUnitOfWork.Value = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -143,7 +153,7 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
var select = Orm.Select<TEntity>()
|
var select = Orm.Select<TEntity>()
|
||||||
.TrackToList(TrackToList) //自动为每个元素 Attach
|
.TrackToList(TrackToList) //自动为每个元素 Attach
|
||||||
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction(false));
|
.WithTransaction(CurrentUnitOfWork?.GetOrBeginTransaction(false));
|
||||||
if (string.IsNullOrEmpty(CurrentTenantId) == false)
|
if (string.IsNullOrEmpty(CurrentTenantId) == false)
|
||||||
select.WhereCascade(a => (a as ITenant).TenantId == CurrentTenantId);
|
select.WhereCascade(a => (a as ITenant).TenantId == CurrentTenantId);
|
||||||
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
|
return select.WhereCascade(a => (a as BaseEntity).IsDeleted == false);
|
||||||
|
@ -79,7 +79,7 @@ namespace FreeSql
|
|||||||
buf.Add(this as TEntity);
|
buf.Add(this as TEntity);
|
||||||
buf.AddRange(this.GetAllChilds());
|
buf.AddRange(this.GetAllChilds());
|
||||||
var repo = Orm.GetRepository<TEntity>();
|
var repo = Orm.GetRepository<TEntity>();
|
||||||
repo.UnitOfWork = UnitOfWork.Current.Value;
|
repo.UnitOfWork = CurrentUnitOfWork;
|
||||||
buf = repo.Select.WhereDynamic(buf)
|
buf = repo.Select.WhereDynamic(buf)
|
||||||
.Include(a => ((((((((((a as BaseEntityTree<TEntity, TKey>).Parent
|
.Include(a => ((((((((((a as BaseEntityTree<TEntity, TKey>).Parent
|
||||||
as BaseEntityTree<TEntity, TKey>).Parent
|
as BaseEntityTree<TEntity, TKey>).Parent
|
||||||
@ -110,7 +110,7 @@ namespace FreeSql
|
|||||||
var childs = GetAllChilds();
|
var childs = GetAllChilds();
|
||||||
childs.Add(this as TEntity);
|
childs.Add(this as TEntity);
|
||||||
var repo = Orm.GetRepository<TEntity>();
|
var repo = Orm.GetRepository<TEntity>();
|
||||||
repo.UnitOfWork = UnitOfWork.Current.Value;
|
repo.UnitOfWork = CurrentUnitOfWork;
|
||||||
repo.Attach(childs);
|
repo.Attach(childs);
|
||||||
foreach (var item in childs)
|
foreach (var item in childs)
|
||||||
(item as BaseEntity).IsDeleted = false;
|
(item as BaseEntity).IsDeleted = false;
|
||||||
|
@ -227,6 +227,41 @@
|
|||||||
<param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
<param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:FreeSql.RepositoryUnitOfWorkManager">
|
||||||
|
<summary>
|
||||||
|
仓储的工作单元管理器
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.RepositoryUnitOfWorkManager.Propagation.Requierd">
|
||||||
|
<summary>
|
||||||
|
如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,默认的选择。
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.RepositoryUnitOfWorkManager.Propagation.Supports">
|
||||||
|
<summary>
|
||||||
|
支持当前事务,如果没有当前事务,就以非事务方法执行。
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.RepositoryUnitOfWorkManager.Propagation.Mandatory">
|
||||||
|
<summary>
|
||||||
|
使用当前事务,如果没有当前事务,就抛出异常。
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.RepositoryUnitOfWorkManager.Propagation.NotSupported">
|
||||||
|
<summary>
|
||||||
|
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.RepositoryUnitOfWorkManager.Propagation.Never">
|
||||||
|
<summary>
|
||||||
|
以非事务方式执行操作,如果当前事务存在则抛出异常。
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
|
<member name="F:FreeSql.RepositoryUnitOfWorkManager.Propagation.Nested">
|
||||||
|
<summary>
|
||||||
|
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,就新建一个事务。
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.IDataFilter`1.Enable(System.String[])">
|
<member name="M:FreeSql.IDataFilter`1.Enable(System.String[])">
|
||||||
<summary>
|
<summary>
|
||||||
开启过滤器,若使用 using 则使用完后,恢复为原有状态
|
开启过滤器,若使用 using 则使用完后,恢复为原有状态
|
||||||
@ -299,6 +334,11 @@
|
|||||||
<param name="entity">实体对象</param>
|
<param name="entity">实体对象</param>
|
||||||
<param name="propertyName">属性名</param>
|
<param name="propertyName">属性名</param>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="T:FreeSql.IUnitOfWork">
|
||||||
|
<summary>
|
||||||
|
工作单元
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.IUnitOfWork.GetOrBeginTransaction(System.Boolean)">
|
<member name="M:FreeSql.IUnitOfWork.GetOrBeginTransaction(System.Boolean)">
|
||||||
<summary>
|
<summary>
|
||||||
开启事务,或者返回已开启的事务
|
开启事务,或者返回已开启的事务
|
||||||
@ -326,7 +366,7 @@
|
|||||||
</member>
|
</member>
|
||||||
<member name="P:FreeSql.IUnitOfWork.EntityChangeReport">
|
<member name="P:FreeSql.IUnitOfWork.EntityChangeReport">
|
||||||
<summary>
|
<summary>
|
||||||
此工作单元内的实体变化跟踪
|
工作单元内的实体变化跟踪
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="P:FreeSql.UnitOfWork.DebugBeingUsed">
|
<member name="P:FreeSql.UnitOfWork.DebugBeingUsed">
|
||||||
|
@ -0,0 +1,201 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace FreeSql
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 仓储的工作单元管理器
|
||||||
|
/// </summary>
|
||||||
|
public class RepositoryUnitOfWorkManager : IDisposable
|
||||||
|
{
|
||||||
|
IFreeSql _fsql;
|
||||||
|
List<IRepositoryUnitOfWork> _uows = new List<IRepositoryUnitOfWork>();
|
||||||
|
bool _isNotSupported = false;
|
||||||
|
|
||||||
|
public RepositoryUnitOfWorkManager(IFreeSql fsql)
|
||||||
|
{
|
||||||
|
_fsql = fsql ?? throw new ArgumentNullException($"{nameof(RepositoryUnitOfWorkManager)} 构造参数 {nameof(fsql)} 不能为 null");
|
||||||
|
}
|
||||||
|
|
||||||
|
~RepositoryUnitOfWorkManager() => this.Dispose();
|
||||||
|
int _disposeCounter;
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (Interlocked.Increment(ref _disposeCounter) != 1) return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Exception exception = null;
|
||||||
|
for (var a = _uows.Count - 1; a >= 0; a--)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (exception == null) _uows[a].Commit();
|
||||||
|
else _uows[a].Rollback();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
if (exception == null) exception = ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (exception != null) throw exception;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_uows.Clear();
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Propagation
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,默认的选择。
|
||||||
|
/// </summary>
|
||||||
|
Requierd,
|
||||||
|
/// <summary>
|
||||||
|
/// 支持当前事务,如果没有当前事务,就以非事务方法执行。
|
||||||
|
/// </summary>
|
||||||
|
Supports,
|
||||||
|
/// <summary>
|
||||||
|
/// 使用当前事务,如果没有当前事务,就抛出异常。
|
||||||
|
/// </summary>
|
||||||
|
Mandatory,
|
||||||
|
/// <summary>
|
||||||
|
/// 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
|
||||||
|
/// </summary>
|
||||||
|
NotSupported,
|
||||||
|
/// <summary>
|
||||||
|
/// 以非事务方式执行操作,如果当前事务存在则抛出异常。
|
||||||
|
/// </summary>
|
||||||
|
Never,
|
||||||
|
/// <summary>
|
||||||
|
/// 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,就新建一个事务。
|
||||||
|
/// </summary>
|
||||||
|
Nested
|
||||||
|
}
|
||||||
|
|
||||||
|
public IRepositoryUnitOfWork Begin(Propagation propagation, IsolationLevel? isolationLevel = null)
|
||||||
|
{
|
||||||
|
if (propagation == Propagation.Requierd)
|
||||||
|
{
|
||||||
|
if (_isNotSupported == false)
|
||||||
|
{
|
||||||
|
for (var a = _uows.Count - 1; a >= 0; a--)
|
||||||
|
if (_uows[a].GetOrBeginTransaction(false) != null)
|
||||||
|
return new UnitOfWorkProxy(_uows[a]);
|
||||||
|
}
|
||||||
|
var uow = new RepositoryUnitOfWork(_fsql);
|
||||||
|
if (isolationLevel != null) uow.IsolationLevel = isolationLevel.Value;
|
||||||
|
try { uow.GetOrBeginTransaction(); }
|
||||||
|
catch { uow.Dispose(); throw; }
|
||||||
|
_uows.Add(uow);
|
||||||
|
return uow;
|
||||||
|
}
|
||||||
|
if (propagation == Propagation.Supports)
|
||||||
|
{
|
||||||
|
if (_isNotSupported == false)
|
||||||
|
{
|
||||||
|
for (var a = _uows.Count - 1; a >= 0; a--)
|
||||||
|
if (_uows[a].GetOrBeginTransaction(false) != null)
|
||||||
|
return new UnitOfWorkProxy(_uows[a]);
|
||||||
|
}
|
||||||
|
return new UnitOfWorkNothing(_fsql);
|
||||||
|
}
|
||||||
|
if (propagation == Propagation.Mandatory)
|
||||||
|
{
|
||||||
|
if (_isNotSupported == false)
|
||||||
|
{
|
||||||
|
for (var a = _uows.Count - 1; a >= 0; a--)
|
||||||
|
if (_uows[a].GetOrBeginTransaction(false) != null)
|
||||||
|
return new UnitOfWorkProxy(_uows[a]);
|
||||||
|
throw new Exception("Propagation_Mandatory: 使用当前事务,如果没有当前事务,就抛出异常");
|
||||||
|
}
|
||||||
|
throw new Exception("Propagation_Mandatory: 使用当前事务,如果没有当前事务,就抛出异常(NotSupported 事务挂起中)");
|
||||||
|
}
|
||||||
|
if (propagation == Propagation.NotSupported)
|
||||||
|
{
|
||||||
|
if (_isNotSupported == false)
|
||||||
|
{
|
||||||
|
_isNotSupported = true;
|
||||||
|
return new UnitOfWorkNothing(_fsql) { OnDispose = () => _isNotSupported = false };
|
||||||
|
}
|
||||||
|
return new UnitOfWorkNothing(_fsql);
|
||||||
|
}
|
||||||
|
if (propagation == Propagation.Never)
|
||||||
|
{
|
||||||
|
if (_isNotSupported == false)
|
||||||
|
{
|
||||||
|
for (var a = _uows.Count - 1; a >= 0; a--)
|
||||||
|
if (_uows[a].GetOrBeginTransaction(false) != null)
|
||||||
|
throw new Exception("Propagation_Never: 以非事务方式执行操作,如果当前事务存在则抛出异常");
|
||||||
|
}
|
||||||
|
return new UnitOfWorkNothing(_fsql);
|
||||||
|
}
|
||||||
|
if (propagation == Propagation.Nested)
|
||||||
|
{
|
||||||
|
var uow = new RepositoryUnitOfWork(_fsql);
|
||||||
|
if (isolationLevel != null) uow.IsolationLevel = isolationLevel.Value;
|
||||||
|
try { uow.GetOrBeginTransaction(); }
|
||||||
|
catch { uow.Dispose(); throw; }
|
||||||
|
_uows.Add(uow);
|
||||||
|
return uow;
|
||||||
|
}
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
class UnitOfWorkProxy : IRepositoryUnitOfWork
|
||||||
|
{
|
||||||
|
IRepositoryUnitOfWork _baseUow;
|
||||||
|
public UnitOfWorkProxy(IRepositoryUnitOfWork baseUow) => _baseUow = baseUow;
|
||||||
|
public IsolationLevel? IsolationLevel { get => _baseUow.IsolationLevel; set { } }
|
||||||
|
public DbContext.EntityChangeReport EntityChangeReport => _baseUow.EntityChangeReport;
|
||||||
|
|
||||||
|
public bool Enable => _baseUow.Enable;
|
||||||
|
public void Close() => _baseUow.Close();
|
||||||
|
public void Open() => _baseUow.Open();
|
||||||
|
|
||||||
|
public DbTransaction GetOrBeginTransaction(bool isCreate = true) => _baseUow.GetOrBeginTransaction(isCreate);
|
||||||
|
public void Commit() => this.Dispose();
|
||||||
|
public void Rollback() => _baseUow.Rollback();
|
||||||
|
public void Dispose() { }
|
||||||
|
|
||||||
|
public IBaseRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class => _baseUow.GetRepository<TEntity, TKey>(filter);
|
||||||
|
public IBaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class => _baseUow.GetRepository<TEntity>(filter);
|
||||||
|
public IBaseRepository<TEntity, Guid> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class => _baseUow.GetGuidRepository<TEntity>(filter);
|
||||||
|
}
|
||||||
|
class UnitOfWorkNothing : IRepositoryUnitOfWork
|
||||||
|
{
|
||||||
|
internal IFreeSql _fsql;
|
||||||
|
internal Action OnDispose;
|
||||||
|
public UnitOfWorkNothing(IFreeSql fsql) => _fsql = fsql;
|
||||||
|
public IsolationLevel? IsolationLevel { get; set; }
|
||||||
|
public DbContext.EntityChangeReport EntityChangeReport { get; } = new DbContext.EntityChangeReport();
|
||||||
|
|
||||||
|
public bool Enable { get; }
|
||||||
|
public void Close() { }
|
||||||
|
public void Open() { }
|
||||||
|
|
||||||
|
public DbTransaction GetOrBeginTransaction(bool isCreate = true) => null;
|
||||||
|
public void Commit()
|
||||||
|
{
|
||||||
|
if (EntityChangeReport != null && EntityChangeReport.OnChange != null && EntityChangeReport.Report.Any() == true)
|
||||||
|
EntityChangeReport.OnChange.Invoke(EntityChangeReport.Report);
|
||||||
|
this.Dispose();
|
||||||
|
}
|
||||||
|
public void Rollback() => this.Dispose();
|
||||||
|
public void Dispose() {
|
||||||
|
EntityChangeReport?.Report.Clear();
|
||||||
|
OnDispose?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IBaseRepository<TEntity, TKey> GetRepository<TEntity, TKey>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class => new DefaultRepository<TEntity, TKey>(_fsql, filter);
|
||||||
|
public IBaseRepository<TEntity> GetRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null) where TEntity : class => new DefaultRepository<TEntity, int>(_fsql, filter);
|
||||||
|
public IBaseRepository<TEntity, Guid> GetGuidRepository<TEntity>(Expression<Func<TEntity, bool>> filter = null, Func<string, string> asTable = null) where TEntity : class => new GuidRepository<TEntity>(_fsql, filter, asTable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,9 @@ using System.Data.Common;
|
|||||||
|
|
||||||
namespace FreeSql
|
namespace FreeSql
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 工作单元
|
||||||
|
/// </summary>
|
||||||
public interface IUnitOfWork : IDisposable
|
public interface IUnitOfWork : IDisposable
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -40,7 +43,7 @@ namespace FreeSql
|
|||||||
void Open();
|
void Open();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 此工作单元内的实体变化跟踪
|
/// 工作单元内的实体变化跟踪
|
||||||
/// </summary>
|
/// </summary>
|
||||||
DbContext.EntityChangeReport EntityChangeReport { get; }
|
DbContext.EntityChangeReport EntityChangeReport { get; }
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,6 @@ namespace FreeSql
|
|||||||
{
|
{
|
||||||
public class UnitOfWork : IUnitOfWork
|
public class UnitOfWork : IUnitOfWork
|
||||||
{
|
{
|
||||||
#if netcoreapp
|
|
||||||
public static readonly AsyncLocal<IUnitOfWork> Current = new AsyncLocal<IUnitOfWork>();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int _seed;
|
static int _seed;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 正在使用中的工作单元(调试)
|
/// 正在使用中的工作单元(调试)
|
||||||
@ -40,11 +36,7 @@ namespace FreeSql
|
|||||||
if (_fsql == null) throw new ArgumentNullException(nameof(fsql));
|
if (_fsql == null) throw new ArgumentNullException(nameof(fsql));
|
||||||
|
|
||||||
_uowBefore = new Aop.TraceBeforeEventArgs("UnitOfWork", null);
|
_uowBefore = new Aop.TraceBeforeEventArgs("UnitOfWork", null);
|
||||||
_fsql?.Aop.TraceBeforeHandler?.Invoke(this, _uowBefore);
|
_fsql.Aop.TraceBeforeHandler?.Invoke(this, _uowBefore);
|
||||||
|
|
||||||
#if netcoreapp
|
|
||||||
Current.Value = this;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReturnObject()
|
void ReturnObject()
|
||||||
@ -55,9 +47,6 @@ namespace FreeSql
|
|||||||
_fsql.Ado.MasterPool.Return(_conn);
|
_fsql.Ado.MasterPool.Return(_conn);
|
||||||
_tran = null;
|
_tran = null;
|
||||||
_conn = null;
|
_conn = null;
|
||||||
#if netcoreapp
|
|
||||||
Current.Value = null;
|
|
||||||
#endif
|
|
||||||
EntityChangeReport?.Report.Clear();
|
EntityChangeReport?.Report.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +163,6 @@ namespace FreeSql
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.Rollback();
|
this.Rollback();
|
||||||
this.Close();
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user