- 增加 UnitOfWork.Current 静态属性,AsyncLocal 实现 [NETStandard 2.0];

This commit is contained in:
28810 2019-07-25 16:45:07 +08:00
parent f9600d6c76
commit fc84f68f3a
6 changed files with 69 additions and 228 deletions

View File

@ -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<TEntity> : BaseEntity where TEntity : class
{
public static ISelect<TEntity> Select => Orm.Select<TEntity>()
.WithTransaction(CurrentUow?.GetOrBeginTransaction(false))
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction(false))
.WhereCascade(a => (a as BaseEntity<TEntity>).IsDeleted == false);
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
@ -70,11 +70,11 @@ public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
{
if (this.Repository == null)
return await Orm.Update<TEntity>(this as TEntity)
.WithTransaction(CurrentUow?.GetOrBeginTransaction())
.WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
.Set(a => (a as BaseEntity<TEntity>).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;
}
/// <summary>
@ -109,10 +109,10 @@ public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
this.UpdateTime = DateTime.Now;
if (this.Repository == null)
return await Orm.Update<TEntity>()
.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;
}
/// <summary>
@ -124,7 +124,7 @@ public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();
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<TEntity> : BaseEntity where TEntity : class
if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>();
this.Repository.UnitOfWork = CurrentUow;
this.Repository.UnitOfWork = UnitOfWork.Current.Value;
await this.Repository.InsertOrUpdateAsync(this as TEntity);
}
}

View File

@ -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<IUnitOfWork> _asyncUow = new AsyncLocal<IUnitOfWork>();
protected IFreeSql _fsql;
protected Object<DbConnection> _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;
}
/// <summary>
/// 是否启用工作单元
/// </summary>
public bool Enable { get; private set; } = true;
/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
/// </summary>
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);
}
}
}

View File

@ -174,19 +174,6 @@
开启工作单元
</summary>
</member>
<member name="P:FreeSql.UnitOfWork.Enable">
<summary>
是否启用工作单元
</summary>
</member>
<member name="M:FreeSql.UnitOfWork.Close">
<summary>
禁用工作单元
<exception cref="T:System.Exception"></exception>
<para></para>
若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
</summary>
</member>
<member name="M:FreeSqlDbContextExtenssions.CreateDbContext(IFreeSql)">
<summary>
创建普通数据上下文档对象

View File

@ -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;
}

View File

@ -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<IUnitOfWork> Current = new AsyncLocal<IUnitOfWork>();
#endif
protected IFreeSql _fsql;
protected Object<DbConnection> _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
}
/// <summary>
/// 是否启用工作单元
/// </summary>
public bool Enable { get; private set; } = true;
/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
/// </summary>
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;
@ -77,33 +73,27 @@ namespace FreeSql
}
public void Commit()
{
if (_tran != null)
{
try
{
_tran.Commit();
if (_tran != null) _tran.Commit();
}
finally
{
ReturnObject();
}
}
}
public void Rollback()
{
if (_tran != null)
{
try
{
_tran.Rollback();
if (_tran != null) _tran.Rollback();
}
finally
{
ReturnObject();
}
}
}
~UnitOfWork()
{
this.Dispose();
@ -112,15 +102,10 @@ namespace FreeSql
public void Dispose()
{
if (_isdisposed) return;
try
{
this.Rollback();
}
finally
{
_isdisposed = true;
this.Rollback();
this.Close();
GC.SuppressFinalize(this);
}
}
}
}