mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
- 增加 UnitOfWorkManager 工作单元管理器,实现多种传播事务;#289
This commit is contained in:
@ -35,21 +35,21 @@ namespace FreeSql
|
||||
|
||||
public ISelect<T1> Select<T1>() where T1 : class
|
||||
{
|
||||
_resolveDbContext()?.FlushCommand();
|
||||
_resolveDbContext?.Invoke()?.FlushCommand();
|
||||
return _originalFsql.Select<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction(false));
|
||||
}
|
||||
public ISelect<T1> Select<T1>(object dywhere) where T1 : class => Select<T1>().WhereDynamic(dywhere);
|
||||
|
||||
public IDelete<T1> Delete<T1>() where T1 : class
|
||||
{
|
||||
_resolveDbContext()?.FlushCommand();
|
||||
_resolveDbContext?.Invoke()?.FlushCommand();
|
||||
return _originalFsql.Delete<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
|
||||
}
|
||||
public IDelete<T1> Delete<T1>(object dywhere) where T1 : class => Delete<T1>().WhereDynamic(dywhere);
|
||||
|
||||
public IUpdate<T1> Update<T1>() where T1 : class
|
||||
{
|
||||
var db = _resolveDbContext();
|
||||
var db = _resolveDbContext?.Invoke();
|
||||
db?.FlushCommand();
|
||||
var update = _originalFsql.Update<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
|
||||
if (db?.Options.NoneParameter != null) update.NoneParameter(db.Options.NoneParameter.Value);
|
||||
@ -59,7 +59,7 @@ namespace FreeSql
|
||||
|
||||
public IInsert<T1> Insert<T1>() where T1 : class
|
||||
{
|
||||
var db = _resolveDbContext();
|
||||
var db = _resolveDbContext?.Invoke();
|
||||
db?.FlushCommand();
|
||||
var insert = _originalFsql.Insert<T1>().WithTransaction(_resolveUnitOfWork()?.GetOrBeginTransaction());
|
||||
if (db?.Options.NoneParameter != null) insert.NoneParameter(db.Options.NoneParameter.Value);
|
||||
|
@ -6,7 +6,7 @@
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>YeXiangQin</Authors>
|
||||
<Description>FreeSql is the ORM in .NetCore, .NetFramework, And Xamarin. It supports Mysql, Postgresql, SqlServer, Oracle, Sqlite, Odbc, 达梦, And Access</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql.DbContext</PackageProjectUrl>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql/wiki/DbContext</PackageProjectUrl>
|
||||
<PackageTags>FreeSql ORM DbContext</PackageTags>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
|
@ -227,41 +227,6 @@
|
||||
<param name="asTable">分表规则,参数:旧表名;返回:新表名 https://github.com/2881099/FreeSql/wiki/Repository</param>
|
||||
<returns></returns>
|
||||
</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[])">
|
||||
<summary>
|
||||
开启过滤器,若使用 using 则使用完后,恢复为原有状态
|
||||
@ -381,6 +346,65 @@
|
||||
例如:20191121_214504_1
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:FreeSql.UnitOfWorkManager">
|
||||
<summary>
|
||||
工作单元管理器
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:FreeSql.UnitOfWorkManager.Current">
|
||||
<summary>
|
||||
当前的工作单元
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSql.UnitOfWorkManager.Binding(FreeSql.IBaseRepository)">
|
||||
<summary>
|
||||
将仓储的事务交给我管理
|
||||
</summary>
|
||||
<param name="repository"></param>
|
||||
</member>
|
||||
<member name="M:FreeSql.UnitOfWorkManager.Begin(FreeSql.Propagation,System.Nullable{System.Data.IsolationLevel})">
|
||||
<summary>
|
||||
创建工作单元
|
||||
</summary>
|
||||
<param name="propagation">事务传播方式</param>
|
||||
<param name="isolationLevel">事务隔离级别</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:FreeSql.Propagation">
|
||||
<summary>
|
||||
事务传播方式
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Propagation.Requierd">
|
||||
<summary>
|
||||
如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,默认的选择。
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Propagation.Supports">
|
||||
<summary>
|
||||
支持当前事务,如果没有当前事务,就以非事务方法执行。
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Propagation.Mandatory">
|
||||
<summary>
|
||||
使用当前事务,如果没有当前事务,就抛出异常。
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Propagation.NotSupported">
|
||||
<summary>
|
||||
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Propagation.Never">
|
||||
<summary>
|
||||
以非事务方式执行操作,如果当前事务存在则抛出异常。
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:FreeSql.Propagation.Nested">
|
||||
<summary>
|
||||
以嵌套事务方式执行。
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSqlDbContextExtensions.Entity``1(FreeSql.ICodeFirst,System.Action{FreeSql.Extensions.EfCoreFluentApi.EfCoreTableFluent{``0}})">
|
||||
<summary>
|
||||
EFCore 95% 相似的 FluentApi 扩展方法
|
||||
|
@ -1,201 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -7,11 +7,19 @@ namespace FreeSql
|
||||
{
|
||||
public DefaultRepository(IFreeSql fsql) : base(fsql, null, null) { }
|
||||
public DefaultRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter) : base(fsql, filter, null) { }
|
||||
public DefaultRepository(IFreeSql fsql, UnitOfWorkManager uowManger) : base(uowManger?.Orm ?? fsql, null, null)
|
||||
{
|
||||
uowManger?.Binding(this);
|
||||
}
|
||||
}
|
||||
|
||||
public class GuidRepository<TEntity> : BaseRepository<TEntity, Guid> where TEntity : class
|
||||
{
|
||||
public GuidRepository(IFreeSql fsql) : this(fsql, null, null) { }
|
||||
public GuidRepository(IFreeSql fsql, Expression<Func<TEntity, bool>> filter, Func<string, string> asTable) : base(fsql, filter, asTable) { }
|
||||
public GuidRepository(IFreeSql fsql, UnitOfWorkManager uowManger) : base(uowManger?.Orm ?? fsql, null, null)
|
||||
{
|
||||
uowManger?.Binding(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
274
FreeSql.DbContext/UnitOfWork/UnitOfWorkManager.cs
Normal file
274
FreeSql.DbContext/UnitOfWork/UnitOfWorkManager.cs
Normal file
@ -0,0 +1,274 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace FreeSql
|
||||
{
|
||||
/// <summary>
|
||||
/// 工作单元管理器
|
||||
/// </summary>
|
||||
public class UnitOfWorkManager : IDisposable
|
||||
{
|
||||
DbContextScopedFreeSql _ormScoped;
|
||||
public IFreeSql Orm => _ormScoped;
|
||||
List<UowInfo> _rawUows = new List<UowInfo>();
|
||||
List<UowInfo> _allUows = new List<UowInfo>();
|
||||
List<RepoInfo> _repos = new List<RepoInfo>();
|
||||
|
||||
public UnitOfWorkManager(IFreeSql fsql)
|
||||
{
|
||||
if (fsql == null) throw new ArgumentNullException($"{nameof(UnitOfWorkManager)} 构造参数 {nameof(fsql)} 不能为 null");
|
||||
_ormScoped = DbContextScopedFreeSql.Create(fsql, null, () => this.Current);
|
||||
}
|
||||
|
||||
#region Dispose
|
||||
~UnitOfWorkManager() => this.Dispose();
|
||||
int _disposeCounter;
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Increment(ref _disposeCounter) != 1) return;
|
||||
try
|
||||
{
|
||||
Exception exception = null;
|
||||
for (var a = _rawUows.Count - 1; a >= 0; a--)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (exception == null) _rawUows[a].Uow.Commit();
|
||||
else _rawUows[a].Uow.Rollback();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (exception == null) exception = ex;
|
||||
}
|
||||
}
|
||||
if (exception != null) throw exception;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rawUows.Clear();
|
||||
_allUows.Clear();
|
||||
_repos.Clear();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 当前的工作单元
|
||||
/// </summary>
|
||||
public IUnitOfWork Current => _allUows.LastOrDefault()?.Uow;
|
||||
|
||||
/// <summary>
|
||||
/// 将仓储的事务交给我管理
|
||||
/// </summary>
|
||||
/// <param name="repository"></param>
|
||||
public void Binding(IBaseRepository repository)
|
||||
{
|
||||
var repoInfo = new RepoInfo(repository);
|
||||
repository.UnitOfWork = Current;
|
||||
_repos.Add(repoInfo);
|
||||
}
|
||||
void SetAllRepositoryUow()
|
||||
{
|
||||
foreach (var repo in _repos)
|
||||
repo.Repository.UnitOfWork = Current ?? repo.OrginalUow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建工作单元
|
||||
/// </summary>
|
||||
/// <param name="propagation">事务传播方式</param>
|
||||
/// <param name="isolationLevel">事务隔离级别</param>
|
||||
/// <returns></returns>
|
||||
public IUnitOfWork Begin(Propagation propagation = Propagation.Requierd, IsolationLevel? isolationLevel = null)
|
||||
{
|
||||
switch (propagation)
|
||||
{
|
||||
case Propagation.Requierd: return FindedUowCreateVirtual() ?? CreateUow(isolationLevel);
|
||||
case Propagation.Supports: return FindedUowCreateVirtual() ?? CreateUowNothing(_allUows.LastOrDefault()?.IsNotSupported ?? false);
|
||||
case Propagation.Mandatory: return FindedUowCreateVirtual() ?? throw new Exception("Propagation_Mandatory: 使用当前事务,如果没有当前事务,就抛出异常");
|
||||
case Propagation.NotSupported: return CreateUowNothing(true);
|
||||
case Propagation.Never:
|
||||
var isNotSupported = _allUows.LastOrDefault()?.IsNotSupported ?? false;
|
||||
if (isNotSupported == false)
|
||||
{
|
||||
for (var a = _rawUows.Count - 1; a >= 0; a--)
|
||||
if (_rawUows[a].Uow.GetOrBeginTransaction(false) != null)
|
||||
throw new Exception("Propagation_Never: 以非事务方式执行操作,如果当前事务存在则抛出异常");
|
||||
}
|
||||
return CreateUowNothing(isNotSupported);
|
||||
case Propagation.Nested: return CreateUow(isolationLevel);
|
||||
default: throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
IUnitOfWork FindedUowCreateVirtual()
|
||||
{
|
||||
var isNotSupported = _allUows.LastOrDefault()?.IsNotSupported ?? false;
|
||||
if (isNotSupported == false)
|
||||
{
|
||||
for (var a = _rawUows.Count - 1; a >= 0; a--)
|
||||
if (_rawUows[a].Uow.GetOrBeginTransaction(false) != null)
|
||||
{
|
||||
var uow = new UnitOfWorkVirtual(_rawUows[a].Uow);
|
||||
var uowInfo = new UowInfo(uow, UowInfo.UowType.Virtual, isNotSupported);
|
||||
uow.OnDispose = () => _allUows.Remove(uowInfo);
|
||||
_allUows.Add(uowInfo);
|
||||
SetAllRepositoryUow();
|
||||
return uow;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
IUnitOfWork CreateUowNothing(bool isNotSupported)
|
||||
{
|
||||
var uow = new UnitOfWorkNothing(Orm);
|
||||
var uowInfo = new UowInfo(uow, UowInfo.UowType.Nothing, isNotSupported);
|
||||
uow.OnDispose = () => _allUows.Remove(uowInfo);
|
||||
_allUows.Add(uowInfo);
|
||||
SetAllRepositoryUow();
|
||||
return uow;
|
||||
}
|
||||
IUnitOfWork CreateUow(IsolationLevel? isolationLevel)
|
||||
{
|
||||
var uow = new UnitOfWorkOrginal(new UnitOfWork(Orm));
|
||||
var uowInfo = new UowInfo(uow, UowInfo.UowType.Orginal, false);
|
||||
if (isolationLevel != null) uow.IsolationLevel = isolationLevel.Value;
|
||||
try { uow.GetOrBeginTransaction(); }
|
||||
catch { uow.Dispose(); throw; }
|
||||
|
||||
uow.OnDispose = () =>
|
||||
{
|
||||
_rawUows.Remove(uowInfo);
|
||||
_allUows.Remove(uowInfo);
|
||||
SetAllRepositoryUow();
|
||||
};
|
||||
_rawUows.Add(uowInfo);
|
||||
_allUows.Add(uowInfo);
|
||||
SetAllRepositoryUow();
|
||||
return uow;
|
||||
}
|
||||
|
||||
class RepoInfo
|
||||
{
|
||||
public IBaseRepository Repository;
|
||||
public IUnitOfWork OrginalUow;
|
||||
|
||||
public RepoInfo(IBaseRepository repository)
|
||||
{
|
||||
this.Repository = repository;
|
||||
this.OrginalUow = repository.UnitOfWork;
|
||||
}
|
||||
}
|
||||
class UowInfo
|
||||
{
|
||||
public IUnitOfWork Uow;
|
||||
public UowType Type;
|
||||
public bool IsNotSupported;
|
||||
public enum UowType { Orginal, Virtual, Nothing }
|
||||
|
||||
public UowInfo(IUnitOfWork uow, UowType type, bool isNotSupported)
|
||||
{
|
||||
this.Uow = uow;
|
||||
this.Type = type;
|
||||
this.IsNotSupported = isNotSupported;
|
||||
}
|
||||
}
|
||||
class UnitOfWorkOrginal : IUnitOfWork
|
||||
{
|
||||
IUnitOfWork _baseUow;
|
||||
internal Action OnDispose;
|
||||
public UnitOfWorkOrginal(IUnitOfWork baseUow) => _baseUow = baseUow;
|
||||
public IsolationLevel? IsolationLevel { get => _baseUow.IsolationLevel; set => _baseUow.IsolationLevel = value; }
|
||||
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() => _baseUow.Commit();
|
||||
public void Rollback() => _baseUow.Rollback();
|
||||
public void Dispose()
|
||||
{
|
||||
_baseUow.Dispose();
|
||||
OnDispose?.Invoke();
|
||||
}
|
||||
}
|
||||
class UnitOfWorkVirtual : IUnitOfWork
|
||||
{
|
||||
IUnitOfWork _baseUow;
|
||||
internal Action OnDispose;
|
||||
public UnitOfWorkVirtual(IUnitOfWork 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() { }
|
||||
public void Rollback() => _baseUow.Rollback();
|
||||
public void Dispose() => OnDispose?.Invoke();
|
||||
}
|
||||
class UnitOfWorkNothing : IUnitOfWork
|
||||
{
|
||||
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);
|
||||
EntityChangeReport?.Report.Clear();
|
||||
}
|
||||
public void Rollback() => EntityChangeReport?.Report.Clear();
|
||||
public void Dispose() => OnDispose?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 事务传播方式
|
||||
/// </summary>
|
||||
public enum Propagation
|
||||
{
|
||||
/// <summary>
|
||||
/// 如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,默认的选择。
|
||||
/// </summary>
|
||||
Requierd,
|
||||
/// <summary>
|
||||
/// 支持当前事务,如果没有当前事务,就以非事务方法执行。
|
||||
/// </summary>
|
||||
Supports,
|
||||
/// <summary>
|
||||
/// 使用当前事务,如果没有当前事务,就抛出异常。
|
||||
/// </summary>
|
||||
Mandatory,
|
||||
/// <summary>
|
||||
/// 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
|
||||
/// </summary>
|
||||
NotSupported,
|
||||
/// <summary>
|
||||
/// 以非事务方式执行操作,如果当前事务存在则抛出异常。
|
||||
/// </summary>
|
||||
Never,
|
||||
/// <summary>
|
||||
/// 以嵌套事务方式执行。
|
||||
/// </summary>
|
||||
Nested
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user