From fc84f68f3a92513e1c13bc9f3744a82bf67348f6 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Thu, 25 Jul 2019 16:45:07 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20UnitOfWork.Current=20?= =?UTF-8?q?=E9=9D=99=E6=80=81=E5=B1=9E=E6=80=A7=EF=BC=8CAsyncLocal=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20[NETStandard=202.0]=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Examples/base_entity/BaseEntity.cs | 18 +-- Examples/base_entity/BaseEntityUnitOfWork.cs | 124 ------------------ FreeSql.DbContext/FreeSql.DbContext.csproj | 62 ++++----- FreeSql.DbContext/FreeSql.DbContext.xml | 13 -- .../Extenssions/DependencyInjection.cs | 11 +- FreeSql.DbContext/UnitOfWork/UnitOfWork.cs | 69 ++++------ 6 files changed, 69 insertions(+), 228 deletions(-) delete mode 100644 Examples/base_entity/BaseEntityUnitOfWork.cs diff --git a/Examples/base_entity/BaseEntity.cs b/Examples/base_entity/BaseEntity.cs index 84df35b2..a6618026 100644 --- a/Examples/base_entity/BaseEntity.cs +++ b/Examples/base_entity/BaseEntity.cs @@ -47,18 +47,18 @@ public abstract class BaseEntity public static IUnitOfWork Begin() => Begin(null); public static IUnitOfWork Begin(IsolationLevel? level) { - var uow = new BaseEntityUnitOfWork(Orm); + var uow = Orm.CreateUnitOfWork(); uow.IsolationLevel = level; + UnitOfWork.Current.Value = uow; return uow; } - protected static IUnitOfWork CurrentUow => BaseEntityUnitOfWork._asyncUow.Value; } [Table(DisableSyncStructure = true)] public abstract class BaseEntity : BaseEntity where TEntity : class { public static ISelect Select => Orm.Select() - .WithTransaction(CurrentUow?.GetOrBeginTransaction(false)) + .WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction(false)) .WhereCascade(a => (a as BaseEntity).IsDeleted == false); public static ISelect Where(Expression> exp) => Select.Where(exp); public static ISelect WhereIf(bool condition, Expression> exp) => Select.WhereIf(condition, exp); @@ -70,11 +70,11 @@ public abstract class BaseEntity : BaseEntity where TEntity : class { if (this.Repository == null) return await Orm.Update(this as TEntity) - .WithTransaction(CurrentUow?.GetOrBeginTransaction()) + .WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction()) .Set(a => (a as BaseEntity).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1; this.IsDeleted = value; - this.Repository.UnitOfWork = CurrentUow; + this.Repository.UnitOfWork = UnitOfWork.Current.Value; return await this.Repository.UpdateAsync(this as TEntity) == 1; } /// @@ -109,10 +109,10 @@ public abstract class BaseEntity : BaseEntity where TEntity : class this.UpdateTime = DateTime.Now; if (this.Repository == null) return await Orm.Update() - .WithTransaction(CurrentUow?.GetOrBeginTransaction()) + .WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction()) .SetSource(this as TEntity).ExecuteAffrowsAsync() == 1; - this.Repository.UnitOfWork = CurrentUow; + this.Repository.UnitOfWork = UnitOfWork.Current.Value; return await this.Repository.UpdateAsync(this as TEntity) == 1; } /// @@ -124,7 +124,7 @@ public abstract class BaseEntity : BaseEntity where TEntity : class if (this.Repository == null) this.Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = CurrentUow; + this.Repository.UnitOfWork = UnitOfWork.Current.Value; await this.Repository.InsertAsync(this as TEntity); } @@ -138,7 +138,7 @@ public abstract class BaseEntity : BaseEntity where TEntity : class if (this.Repository == null) this.Repository = Orm.GetRepository(); - this.Repository.UnitOfWork = CurrentUow; + this.Repository.UnitOfWork = UnitOfWork.Current.Value; await this.Repository.InsertOrUpdateAsync(this as TEntity); } } diff --git a/Examples/base_entity/BaseEntityUnitOfWork.cs b/Examples/base_entity/BaseEntityUnitOfWork.cs deleted file mode 100644 index 0199b2c1..00000000 --- a/Examples/base_entity/BaseEntityUnitOfWork.cs +++ /dev/null @@ -1,124 +0,0 @@ -using FreeSql; -using SafeObjectPool; -using System; -using System.Data; -using System.Data.Common; -using System.Threading; - -class BaseEntityUnitOfWork : IUnitOfWork -{ - internal readonly static AsyncLocal _asyncUow = new AsyncLocal(); - - protected IFreeSql _fsql; - protected Object _conn; - protected DbTransaction _tran; - - public BaseEntityUnitOfWork(IFreeSql fsql) - { - _fsql = fsql; - _asyncUow.Value = this; - } - - void ReturnObject() - { - _fsql.Ado.MasterPool.Return(_conn); - _tran = null; - _conn = null; - _asyncUow.Value = null; - } - - - /// - /// 是否启用工作单元 - /// - public bool Enable { get; private set; } = true; - - /// - /// 禁用工作单元 - /// - /// - /// 若已开启事务(已有Insert/Update/Delete操作),调用此方法将发生异常,建议在执行逻辑前调用 - /// - public void Close() - { - if (_tran != null) - throw new Exception("已开启事务,不能禁用工作单元"); - - Enable = false; - } - - public void Open() => - Enable = true; - - public IsolationLevel? IsolationLevel { get; set; } - - public DbTransaction GetOrBeginTransaction(bool isCreate = true) - { - - if (_tran != null) return _tran; - if (isCreate == false) return null; - if (!Enable) return null; - if (_conn != null) _fsql.Ado.MasterPool.Return(_conn); - - _conn = _fsql.Ado.MasterPool.Get(); - try - { - _tran = IsolationLevel == null ? - _conn.Value.BeginTransaction() : - _conn.Value.BeginTransaction(IsolationLevel.Value); - } - catch - { - ReturnObject(); - throw; - } - return _tran; - } - - public void Commit() - { - if (_tran != null) - { - try - { - _tran.Commit(); - } - finally - { - ReturnObject(); - } - } - } - public void Rollback() - { - if (_tran != null) - { - try - { - _tran.Rollback(); - } - finally - { - ReturnObject(); - } - } - } - ~BaseEntityUnitOfWork() - { - this.Dispose(); - } - bool _isdisposed = false; - public void Dispose() - { - if (_isdisposed) return; - _isdisposed = true; - try - { - this.Rollback(); - } - finally - { - GC.SuppressFinalize(this); - } - } -} \ No newline at end of file diff --git a/FreeSql.DbContext/FreeSql.DbContext.csproj b/FreeSql.DbContext/FreeSql.DbContext.csproj index 200e4fc2..2355211c 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.csproj +++ b/FreeSql.DbContext/FreeSql.DbContext.csproj @@ -1,36 +1,36 @@  - - netstandard2.0;net45 - 0.7.15 - true - YeXiangQin - FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite. - https://github.com/2881099/FreeSql.DbContext - FreeSql ORM DbContext - git - MIT - $(AssemblyName) - $(AssemblyName) - true - true - + + netstandard2.0;net45 + 0.7.15 + true + YeXiangQin + FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite. + https://github.com/2881099/FreeSql.DbContext + FreeSql ORM DbContext + git + MIT + $(AssemblyName) + $(AssemblyName) + true + true + - - FreeSql.DbContext.xml - 3 - + + FreeSql.DbContext.xml + 3 + + + + ns20;netstandard20 + + + + + + + + + - - ns20;netstandard20 - - - - - - - - - - diff --git a/FreeSql.DbContext/FreeSql.DbContext.xml b/FreeSql.DbContext/FreeSql.DbContext.xml index bdb7a92e..e603c8d0 100644 --- a/FreeSql.DbContext/FreeSql.DbContext.xml +++ b/FreeSql.DbContext/FreeSql.DbContext.xml @@ -174,19 +174,6 @@ 开启工作单元 - - - 是否启用工作单元 - - - - - 禁用工作单元 - - - 若已开启事务(已有Insert/Update/Delete操作),调用此方法将发生异常,建议在执行逻辑前调用 - - 创建普通数据上下文档对象 diff --git a/FreeSql.DbContext/Repository/Extenssions/DependencyInjection.cs b/FreeSql.DbContext/Repository/Extenssions/DependencyInjection.cs index f37848a8..9781bee9 100644 --- a/FreeSql.DbContext/Repository/Extenssions/DependencyInjection.cs +++ b/FreeSql.DbContext/Repository/Extenssions/DependencyInjection.cs @@ -26,16 +26,9 @@ namespace FreeSql services.AddScoped(typeof(DefaultRepository<,>)); if (assemblies?.Any() == true) - { foreach (var asse in assemblies) - { - foreach (var repos in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a))) - { - - services.AddScoped(repos); - } - } - } + foreach (var repo in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a))) + services.AddScoped(repo); return services; } diff --git a/FreeSql.DbContext/UnitOfWork/UnitOfWork.cs b/FreeSql.DbContext/UnitOfWork/UnitOfWork.cs index a11edae8..493c9bf5 100644 --- a/FreeSql.DbContext/UnitOfWork/UnitOfWork.cs +++ b/FreeSql.DbContext/UnitOfWork/UnitOfWork.cs @@ -2,11 +2,15 @@ using System; using System.Data; using System.Data.Common; +using System.Threading; namespace FreeSql { - class UnitOfWork : IUnitOfWork + public class UnitOfWork : IUnitOfWork { +#if ns20 + public static readonly AsyncLocal Current = new AsyncLocal(); +#endif protected IFreeSql _fsql; protected Object _conn; @@ -15,6 +19,9 @@ namespace FreeSql public UnitOfWork(IFreeSql fsql) { _fsql = fsql; +#if ns20 + Current.Value = this; +#endif } void ReturnObject() @@ -22,30 +29,20 @@ namespace FreeSql _fsql.Ado.MasterPool.Return(_conn); _tran = null; _conn = null; +#if ns20 + Current.Value = null; +#endif } - - /// - /// 是否启用工作单元 - /// public bool Enable { get; private set; } = true; - /// - /// 禁用工作单元 - /// - /// - /// 若已开启事务(已有Insert/Update/Delete操作),调用此方法将发生异常,建议在执行逻辑前调用 - /// public void Close() { if (_tran != null) - { throw new Exception("已开启事务,不能禁用工作单元"); - } Enable = false; } - public void Open() { Enable = true; @@ -55,7 +52,6 @@ namespace FreeSql public DbTransaction GetOrBeginTransaction(bool isCreate = true) { - if (_tran != null) return _tran; if (isCreate == false) return null; if (!Enable) return null; @@ -78,30 +74,24 @@ namespace FreeSql public void Commit() { - if (_tran != null) + try { - try - { - _tran.Commit(); - } - finally - { - ReturnObject(); - } + if (_tran != null) _tran.Commit(); + } + finally + { + ReturnObject(); } } public void Rollback() { - if (_tran != null) + try { - try - { - _tran.Rollback(); - } - finally - { - ReturnObject(); - } + if (_tran != null) _tran.Rollback(); + } + finally + { + ReturnObject(); } } ~UnitOfWork() @@ -112,15 +102,10 @@ namespace FreeSql public void Dispose() { if (_isdisposed) return; - try - { - this.Rollback(); - } - finally - { - _isdisposed = true; - GC.SuppressFinalize(this); - } + _isdisposed = true; + this.Rollback(); + this.Close(); + GC.SuppressFinalize(this); } } }