- 调整 Aop 改为 event 事件;

- 调整 Ado.AopCommandExecuting/AopCommandExecuted 到 Aop.CommandBefore/After;
- 增加 Aop.TraceBefore/After 事件;
This commit is contained in:
28810
2020-03-02 18:57:53 +08:00
parent 32703e016a
commit 24cc8bc1da
58 changed files with 1109 additions and 638 deletions

View File

@ -24,6 +24,8 @@ namespace FreeSql
protected IFreeSql _fsql;
protected Object<DbConnection> _conn;
protected DbTransaction _tran;
protected Aop.TraceBeforeEventArgs _tranBefore;
protected Aop.TraceBeforeEventArgs _uowBefore;
/// <summary>
/// 开启事务后有值,是 UnitOfWork 的唯一标识<para></para>
@ -35,6 +37,11 @@ namespace FreeSql
public UnitOfWork(IFreeSql fsql)
{
_fsql = fsql;
if (_fsql == null) throw new ArgumentNullException(nameof(fsql));
_uowBefore = new Aop.TraceBeforeEventArgs("UnitOfWork", null);
_fsql?.Aop.TraceBeforeHandler?.Invoke(this, _uowBefore);
#if netcoreapp
Current.Value = this;
#endif
@ -77,49 +84,83 @@ namespace FreeSql
if (!Enable) return null;
if (_conn != null) _fsql.Ado.MasterPool.Return(_conn);
_conn = _fsql.Ado.MasterPool.Get();
_tranBefore = new Aop.TraceBeforeEventArgs("BeginTransaction", IsolationLevel);
_fsql?.Aop.TraceBeforeHandler?.Invoke(this, _tranBefore);
try
{
_tran = IsolationLevel == null ?
_conn.Value.BeginTransaction() :
_conn.Value.BeginTransaction(IsolationLevel.Value);
_conn = _fsql.Ado.MasterPool.Get();
try
{
_tran = IsolationLevel == null ?
_conn.Value.BeginTransaction() :
_conn.Value.BeginTransaction(IsolationLevel.Value);
this.Id = $"{DateTime.Now.ToString("yyyyMMdd_HHmmss")}_{Interlocked.Increment(ref _seed)}";
DebugBeingUsed.TryAdd(this.Id, this);
this.Id = $"{DateTime.Now.ToString("yyyyMMdd_HHmmss")}_{Interlocked.Increment(ref _seed)}";
DebugBeingUsed.TryAdd(this.Id, this);
}
catch
{
ReturnObject();
throw;
}
}
catch
catch (Exception ex)
{
ReturnObject();
throw;
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "失败", ex));
throw ex;
}
return _tran;
}
public void Commit()
{
var isCommited = false;
try
{
if (_tran != null)
{
_tran.Commit();
isCommited = true;
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交", null));
if (EntityChangeReport != null && EntityChangeReport.OnChange != null && EntityChangeReport.Report.Any() == true)
EntityChangeReport.OnChange.Invoke(EntityChangeReport.Report);
}
}
catch (Exception ex)
{
if (isCommited == false)
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "提交失败", ex));
throw ex;
}
finally
{
ReturnObject();
_tranBefore = null;
}
}
public void Rollback()
{
var isRollbacked = false;
try
{
if (_tran != null) _tran.Rollback();
if (_tran != null)
{
_tran.Rollback();
isRollbacked = true;
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚", null));
}
}
catch (Exception ex)
{
if (isRollbacked == false)
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_tranBefore, "回滚失败", ex));
throw ex;
}
finally
{
ReturnObject();
_tranBefore = null;
}
}
@ -137,6 +178,7 @@ namespace FreeSql
}
finally
{
_fsql?.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(_uowBefore, "释放", null));
GC.SuppressFinalize(this);
}
}