- 增加 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() => Begin(null);
public static IUnitOfWork Begin(IsolationLevel? level) public static IUnitOfWork Begin(IsolationLevel? level)
{ {
var uow = new BaseEntityUnitOfWork(Orm); var uow = Orm.CreateUnitOfWork();
uow.IsolationLevel = level; uow.IsolationLevel = level;
UnitOfWork.Current.Value = uow;
return uow; return uow;
} }
protected static IUnitOfWork CurrentUow => BaseEntityUnitOfWork._asyncUow.Value;
} }
[Table(DisableSyncStructure = true)] [Table(DisableSyncStructure = true)]
public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
{ {
public static ISelect<TEntity> Select => Orm.Select<TEntity>() 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); .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> 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); 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) if (this.Repository == null)
return await Orm.Update<TEntity>(this as TEntity) 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; .Set(a => (a as BaseEntity<TEntity>).IsDeleted, this.IsDeleted = value).ExecuteAffrowsAsync() == 1;
this.IsDeleted = value; this.IsDeleted = value;
this.Repository.UnitOfWork = CurrentUow; this.Repository.UnitOfWork = UnitOfWork.Current.Value;
return await this.Repository.UpdateAsync(this as TEntity) == 1; return await this.Repository.UpdateAsync(this as TEntity) == 1;
} }
/// <summary> /// <summary>
@ -109,10 +109,10 @@ public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
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(CurrentUow?.GetOrBeginTransaction()) .WithTransaction(UnitOfWork.Current.Value?.GetOrBeginTransaction())
.SetSource(this as TEntity).ExecuteAffrowsAsync() == 1; .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; return await this.Repository.UpdateAsync(this as TEntity) == 1;
} }
/// <summary> /// <summary>
@ -124,7 +124,7 @@ public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
if (this.Repository == null) if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>(); this.Repository = Orm.GetRepository<TEntity>();
this.Repository.UnitOfWork = CurrentUow; this.Repository.UnitOfWork = UnitOfWork.Current.Value;
await this.Repository.InsertAsync(this as TEntity); await this.Repository.InsertAsync(this as TEntity);
} }
@ -138,7 +138,7 @@ public abstract class BaseEntity<TEntity> : BaseEntity where TEntity : class
if (this.Repository == null) if (this.Repository == null)
this.Repository = Orm.GetRepository<TEntity>(); this.Repository = Orm.GetRepository<TEntity>();
this.Repository.UnitOfWork = CurrentUow; this.Repository.UnitOfWork = UnitOfWork.Current.Value;
await this.Repository.InsertOrUpdateAsync(this as TEntity); 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> </summary>
</member> </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)"> <member name="M:FreeSqlDbContextExtenssions.CreateDbContext(IFreeSql)">
<summary> <summary>
创建普通数据上下文档对象 创建普通数据上下文档对象

View File

@ -26,16 +26,9 @@ namespace FreeSql
services.AddScoped(typeof(DefaultRepository<,>)); services.AddScoped(typeof(DefaultRepository<,>));
if (assemblies?.Any() == true) if (assemblies?.Any() == true)
{
foreach (var asse in assemblies) foreach (var asse in assemblies)
{ foreach (var repo in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a)))
foreach (var repos in asse.GetTypes().Where(a => a.IsAbstract == false && typeof(IBaseRepository).IsAssignableFrom(a))) services.AddScoped(repo);
{
services.AddScoped(repos);
}
}
}
return services; return services;
} }

View File

@ -2,11 +2,15 @@
using System; using System;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Threading;
namespace FreeSql 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 IFreeSql _fsql;
protected Object<DbConnection> _conn; protected Object<DbConnection> _conn;
@ -15,6 +19,9 @@ namespace FreeSql
public UnitOfWork(IFreeSql fsql) public UnitOfWork(IFreeSql fsql)
{ {
_fsql = fsql; _fsql = fsql;
#if ns20
Current.Value = this;
#endif
} }
void ReturnObject() void ReturnObject()
@ -22,30 +29,20 @@ namespace FreeSql
_fsql.Ado.MasterPool.Return(_conn); _fsql.Ado.MasterPool.Return(_conn);
_tran = null; _tran = null;
_conn = null; _conn = null;
#if ns20
Current.Value = null;
#endif
} }
/// <summary>
/// 是否启用工作单元
/// </summary>
public bool Enable { get; private set; } = true; public bool Enable { get; private set; } = true;
/// <summary>
/// 禁用工作单元
/// <exception cref="Exception"></exception>
/// <para></para>
/// 若已开启事务已有Insert/Update/Delete操作调用此方法将发生异常建议在执行逻辑前调用
/// </summary>
public void Close() public void Close()
{ {
if (_tran != null) if (_tran != null)
{
throw new Exception("已开启事务,不能禁用工作单元"); throw new Exception("已开启事务,不能禁用工作单元");
}
Enable = false; Enable = false;
} }
public void Open() public void Open()
{ {
Enable = true; Enable = true;
@ -55,7 +52,6 @@ namespace FreeSql
public DbTransaction GetOrBeginTransaction(bool isCreate = true) public DbTransaction GetOrBeginTransaction(bool isCreate = true)
{ {
if (_tran != null) return _tran; if (_tran != null) return _tran;
if (isCreate == false) return null; if (isCreate == false) return null;
if (!Enable) return null; if (!Enable) return null;
@ -77,33 +73,27 @@ namespace FreeSql
} }
public void Commit() public void Commit()
{
if (_tran != null)
{ {
try try
{ {
_tran.Commit(); if (_tran != null) _tran.Commit();
} }
finally finally
{ {
ReturnObject(); ReturnObject();
} }
} }
}
public void Rollback() public void Rollback()
{
if (_tran != null)
{ {
try try
{ {
_tran.Rollback(); if (_tran != null) _tran.Rollback();
} }
finally finally
{ {
ReturnObject(); ReturnObject();
} }
} }
}
~UnitOfWork() ~UnitOfWork()
{ {
this.Dispose(); this.Dispose();
@ -112,15 +102,10 @@ namespace FreeSql
public void Dispose() public void Dispose()
{ {
if (_isdisposed) return; if (_isdisposed) return;
try
{
this.Rollback();
}
finally
{
_isdisposed = true; _isdisposed = true;
this.Rollback();
this.Close();
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
} }
}
} }