mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-20 09:28:53 +08:00
*事务成功后事件功能,通过TempItems实现
This commit is contained in:
parent
b3334d2084
commit
22448e8456
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
@ -15,4 +15,8 @@
|
||||
<ProjectReference Include="..\SqlSugar\SqlSugar.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="model\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using xTPLM.RFQ.Model.XRFQ_APP;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
@ -22,6 +23,65 @@ namespace OrmTest
|
||||
_a1_Delete.Init();
|
||||
_a2_Sql.Init();
|
||||
_a3_Merge.Init();
|
||||
|
||||
var DB = DbHelper.GetNewDb();
|
||||
|
||||
using (var u1 = DB.CreateContext(DB.Ado.IsNoTran()))
|
||||
{
|
||||
DB.Insertable<TRFQ_SYS_APILOG>(new TRFQ_SYS_APILOG
|
||||
{
|
||||
REQUEST_BODY = "我是U1事件未完成",
|
||||
CREATE_TIME = DateTime.Now,
|
||||
}).ExecuteCommand();
|
||||
Console.WriteLine("我是U1事件未完成");
|
||||
u1.AppendTranSuccessEvent(edb =>
|
||||
{
|
||||
DB.Insertable<TRFQ_SYS_APILOG>(new TRFQ_SYS_APILOG
|
||||
{
|
||||
REQUEST_BODY = "我是U1完成事件",
|
||||
CREATE_TIME = DateTime.Now,
|
||||
}).ExecuteCommand();
|
||||
Console.WriteLine("我是U1完成事件");
|
||||
});
|
||||
using (var u2 = DB.CreateContext(DB.Ado.IsNoTran()))
|
||||
{
|
||||
DB.Insertable<TRFQ_SYS_APILOG>(new TRFQ_SYS_APILOG
|
||||
{
|
||||
REQUEST_BODY = "我是U2事件未完成",
|
||||
CREATE_TIME = DateTime.Now,
|
||||
}).ExecuteCommand();
|
||||
Console.WriteLine("我是U2事件未完成");
|
||||
u2.AppendTranSuccessEvent(edb =>
|
||||
{
|
||||
DB.Insertable<TRFQ_SYS_APILOG>(new TRFQ_SYS_APILOG
|
||||
{
|
||||
REQUEST_BODY = "我是U2完成事件",
|
||||
CREATE_TIME = DateTime.Now,
|
||||
}).ExecuteCommand();
|
||||
Console.WriteLine("我是U2完成事件");
|
||||
});
|
||||
using (var u3 = DB.CreateContext(DB.Ado.IsNoTran()))
|
||||
{
|
||||
DB.Insertable<TRFQ_SYS_APILOG>(new TRFQ_SYS_APILOG
|
||||
{
|
||||
REQUEST_BODY = "我是U3事件未完成",
|
||||
CREATE_TIME = DateTime.Now,
|
||||
}).ExecuteCommand();
|
||||
Console.WriteLine("我是U3事件未完成");
|
||||
u3.AppendTranSuccessEvent(edb =>
|
||||
{
|
||||
DB.Insertable<TRFQ_SYS_APILOG>(new TRFQ_SYS_APILOG
|
||||
{
|
||||
REQUEST_BODY = "我是U3完成事件",
|
||||
CREATE_TIME = DateTime.Now,
|
||||
}).ExecuteCommand();
|
||||
Console.WriteLine("我是U3完成事件");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
u1.Commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,9 +121,9 @@ namespace OrmTest
|
||||
// Logging SQL statements and parameters before execution
|
||||
// 在执行前记录 SQL 语句和参数
|
||||
it.Aop.OnLogExecuting = (sql, para) =>
|
||||
{
|
||||
Console.WriteLine(UtilMethods.GetNativeSql(sql, para));
|
||||
};
|
||||
//{
|
||||
// //Console.WriteLine(UtilMethods.GetNativeSql(sql, para));
|
||||
//};
|
||||
});
|
||||
return db;
|
||||
}
|
||||
|
||||
81
Src/Asp.NetCore2/OracleTest/model/TRFQ_SYS_APILOG.cs
Normal file
81
Src/Asp.NetCore2/OracleTest/model/TRFQ_SYS_APILOG.cs
Normal file
@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using SqlSugar;
|
||||
|
||||
namespace xTPLM.RFQ.Model.XRFQ_APP
|
||||
{
|
||||
///<summary>
|
||||
///接口调用记录
|
||||
///</summary>
|
||||
public partial class TRFQ_SYS_APILOG
|
||||
{
|
||||
public TRFQ_SYS_APILOG()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Desc:自增主键
|
||||
/// Default:
|
||||
/// Nullable:False
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = false, IsPrimaryKey = true, OracleSequenceName = "TRFQ_SYS_APILOG$SEQ", IsIdentity = true)]
|
||||
public decimal ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:接口路径
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true, Length = 256)]
|
||||
public string API_URL { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:请求类型
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true, Length = 256)]
|
||||
public string METHOD { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:请求报文
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public string REQUEST_BODY { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:应答报文
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public string RESPONSE_BODY { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:接口耗时(毫秒)
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public int? TIME_OUT { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:如果发生异常,记录信息
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true, Length = 2048)]
|
||||
public string MESSAGE { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Desc:交易日期
|
||||
/// Default:
|
||||
/// Nullable:True
|
||||
/// </summary>
|
||||
[SugarColumn(IsNullable = true)]
|
||||
public DateTime? CREATE_TIME { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@ -1877,41 +1877,6 @@ namespace SqlSugar
|
||||
await _ThenMapperAsync(pageList, action);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
public void AppendTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
Root.AppendTranSuccessEvent(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void SubtractTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
Root.SubtractTranSuccessEvent(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空事务成功后事件
|
||||
/// </summary>
|
||||
public void RemoveTranSuccessEvent()
|
||||
{
|
||||
Root.RemoveTranSuccessEvent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用事务成功后事件
|
||||
/// </summary>
|
||||
public void InvokeTranSuccessEvent()
|
||||
{
|
||||
Root.InvokeTranSuccessEvent();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -864,39 +864,6 @@ namespace SqlSugar
|
||||
{
|
||||
ScopedContext.ClearTracking();
|
||||
}
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
public void AppendTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
ScopedContext.AppendTranSuccessEvent(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void SubtractTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
ScopedContext.SubtractTranSuccessEvent(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空事务成功后事件
|
||||
/// </summary>
|
||||
public void RemoveTranSuccessEvent()
|
||||
{
|
||||
ScopedContext.RemoveTranSuccessEvent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用事务成功后事件
|
||||
/// </summary>
|
||||
public void InvokeTranSuccessEvent()
|
||||
{
|
||||
ScopedContext.InvokeTranSuccessEvent();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -67,28 +67,6 @@ namespace SqlSugar
|
||||
void Open();
|
||||
void Close();
|
||||
ITenant AsTenant();
|
||||
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
void AppendTranSuccessEvent(Action<ISqlSugarClient> action);
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
void SubtractTranSuccessEvent(Action<ISqlSugarClient> action);
|
||||
|
||||
/// <summary>
|
||||
/// 清空事务成功后事件
|
||||
/// </summary>
|
||||
void RemoveTranSuccessEvent();
|
||||
|
||||
/// <summary>
|
||||
/// 调用事务成功后事件
|
||||
/// </summary>
|
||||
void InvokeTranSuccessEvent();
|
||||
#endregion
|
||||
|
||||
#region Insertable
|
||||
|
||||
@ -48,27 +48,5 @@ namespace SqlSugar
|
||||
|
||||
void Close();
|
||||
void Open();
|
||||
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
void AppendTranSuccessEvent(Action<ISqlSugarClient> action);
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
void SubtractTranSuccessEvent(Action<ISqlSugarClient> action);
|
||||
|
||||
/// <summary>
|
||||
/// 清空事务成功后事件
|
||||
/// </summary>
|
||||
void RemoveTranSuccessEvent();
|
||||
|
||||
/// <summary>
|
||||
/// 调用事务成功后事件
|
||||
/// </summary>
|
||||
void InvokeTranSuccessEvent();
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,10 +28,6 @@ namespace SqlSugar
|
||||
private IgnoreColumnList _IgnoreColumns;
|
||||
private IgnoreColumnList _IgnoreInsertColumns;
|
||||
private Action<SqlSugarClient> _configAction;
|
||||
/// <summary>
|
||||
/// 事务成功事件,当事务提交成功后,触发此事件
|
||||
/// </summary>
|
||||
private event Action<ISqlSugarClient> _TranSuccessEvent;
|
||||
|
||||
internal Guid? AsyncId { get; set; }
|
||||
internal bool? IsSingleInstance { get; set; }
|
||||
@ -1301,51 +1297,25 @@ namespace SqlSugar
|
||||
this.Context.InitMappingInfo(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
public void AppendTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
_TranSuccessEvent += action;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void SubtractTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
_TranSuccessEvent -= action;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空事务成功后事件
|
||||
/// </summary>
|
||||
public void RemoveTranSuccessEvent()
|
||||
private void RemoveTranSuccessEvent()
|
||||
{
|
||||
if (_TranSuccessEvent != null)
|
||||
if (this.TempItems.ContainsKey(SugarUnitOfWork.TranSuccessEvent))
|
||||
{
|
||||
//使用取消订阅的方式,这样更为稳妥
|
||||
var actionBody = _TranSuccessEvent.GetInvocationList();
|
||||
if (actionBody.Length > 0)
|
||||
{
|
||||
foreach (Action<ISqlSugarClient> handler in actionBody)
|
||||
{
|
||||
_TranSuccessEvent -= handler;
|
||||
}
|
||||
}
|
||||
this.TempItems.Remove(SugarUnitOfWork.TranSuccessEvent);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用事务成功后事件
|
||||
/// </summary>
|
||||
public void InvokeTranSuccessEvent()
|
||||
private void InvokeTranSuccessEvent()
|
||||
{
|
||||
if (_TranSuccessEvent != null)
|
||||
if (this.TempItems.TryGetValue(SugarUnitOfWork.TranSuccessEvent, out object value) && value is Action<ISqlSugarClient> eventAction)
|
||||
{
|
||||
_TranSuccessEvent(this);
|
||||
eventAction(this);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -921,39 +921,5 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.GetCurrentConfigIds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
public void AppendTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
ScopedContext.AppendTranSuccessEvent(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void SubtractTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
ScopedContext.SubtractTranSuccessEvent(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空事务成功后事件
|
||||
/// </summary>
|
||||
public void RemoveTranSuccessEvent()
|
||||
{
|
||||
ScopedContext.RemoveTranSuccessEvent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用事务成功后事件
|
||||
/// </summary>
|
||||
public void InvokeTranSuccessEvent()
|
||||
{
|
||||
ScopedContext.InvokeTranSuccessEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ namespace SqlSugar
|
||||
public interface ISugarUnitOfWork<T> where T : SugarUnitOfWork, new()
|
||||
{
|
||||
ISqlSugarClient Db { get; set; }
|
||||
T CreateContext(bool isTran=true);
|
||||
T CreateContext(bool isTran = true);
|
||||
}
|
||||
public class SugarUnitOfWork<T> : ISugarUnitOfWork<T> where T : SugarUnitOfWork, new()
|
||||
{
|
||||
@ -19,7 +19,7 @@ namespace SqlSugar
|
||||
this.Db = db;
|
||||
}
|
||||
public ISqlSugarClient Db { get; set; }
|
||||
public T CreateContext(bool isTran=true)
|
||||
public T CreateContext(bool isTran = true)
|
||||
{
|
||||
return Db.CreateContext<T>(isTran);
|
||||
}
|
||||
@ -36,10 +36,22 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
public interface ISugarUnitOfWork : ISugarUnitOfWorkClear
|
||||
{
|
||||
ISqlSugarClient Db { get; }
|
||||
ITenant Tenant { get; }
|
||||
ISqlSugarClient Db { get; }
|
||||
ITenant Tenant { get; }
|
||||
|
||||
SimpleClient<T> GetRepository<T>() where T : class, new();
|
||||
|
||||
/// <summary>
|
||||
/// 增加事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
void AppendTranSuccessEvent(Action<ISqlSugarClient> action);
|
||||
|
||||
/// <summary>
|
||||
/// 减少事务成功后事件
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
void SubtractTranSuccessEvent(Action<ISqlSugarClient> action);
|
||||
}
|
||||
/// <summary>
|
||||
/// SugarUnitOfWork->ISugarUnitOfWork->ISaugarUnitOfWorkClear
|
||||
@ -48,6 +60,10 @@ namespace SqlSugar
|
||||
/// </summary>
|
||||
public class SugarUnitOfWork : IDisposable, ISugarUnitOfWork
|
||||
{
|
||||
/// <summary>
|
||||
/// 事务后事件唯一Key
|
||||
/// </summary>
|
||||
internal const string TranSuccessEvent = "SqlSugar_tranSuccessEvent";
|
||||
public ISqlSugarClient Db { get; internal set; }
|
||||
public ITenant Tenant { get; internal set; }
|
||||
public bool IsTran { get; internal set; }
|
||||
@ -61,7 +77,7 @@ namespace SqlSugar
|
||||
{
|
||||
this.Tenant.RollbackTran();
|
||||
}
|
||||
if (this.Db.Ado.Transaction==null&&IsClose == false)
|
||||
if (this.Db.Ado.Transaction == null && IsClose == false)
|
||||
{
|
||||
this.Db.Close();
|
||||
}
|
||||
@ -74,13 +90,13 @@ namespace SqlSugar
|
||||
{
|
||||
return new SimpleClient<T>(Db);
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
return new SimpleClient<T>(Db.AsTenant().GetConnection(tenantAttribute.configId));
|
||||
}
|
||||
}
|
||||
|
||||
public RepositoryType GetMyRepository<RepositoryType>() where RepositoryType:new()
|
||||
public RepositoryType GetMyRepository<RepositoryType>() where RepositoryType : new()
|
||||
{
|
||||
var result = (ISugarRepository)new RepositoryType();
|
||||
var type = typeof(RepositoryType).GetGenericArguments().FirstOrDefault();
|
||||
@ -89,7 +105,7 @@ namespace SqlSugar
|
||||
{
|
||||
result.Context = this.Db;
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId);
|
||||
}
|
||||
@ -103,7 +119,7 @@ namespace SqlSugar
|
||||
this.Tenant.CommitTran();
|
||||
IsCommit = true;
|
||||
}
|
||||
if (this.Db.Ado.Transaction==null&&this.IsClose == false)
|
||||
if (this.Db.Ado.Transaction == null && this.IsClose == false)
|
||||
{
|
||||
this.Db.Close();
|
||||
IsClose = true;
|
||||
@ -117,7 +133,15 @@ namespace SqlSugar
|
||||
/// <param name="action">需要执行的委托</param>
|
||||
public void AppendTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
this.Tenant.AppendTranSuccessEvent(action);
|
||||
if (Db.TempItems.TryGetValue(TranSuccessEvent, out object value) && value != null && value is Action<ISqlSugarClient> eventAction)
|
||||
{
|
||||
eventAction += action;
|
||||
Db.TempItems[TranSuccessEvent] = eventAction;
|
||||
}
|
||||
else
|
||||
{
|
||||
Db.TempItems[TranSuccessEvent] = action;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -126,7 +150,15 @@ namespace SqlSugar
|
||||
/// <param name="action"></param>
|
||||
public void SubtractTranSuccessEvent(Action<ISqlSugarClient> action)
|
||||
{
|
||||
this.Tenant.SubtractTranSuccessEvent(action);
|
||||
if (Db.TempItems.TryGetValue(TranSuccessEvent, out object value) && value is Action<ISqlSugarClient> eventAction)
|
||||
{
|
||||
eventAction -= action;
|
||||
Db.TempItems[TranSuccessEvent] = eventAction;
|
||||
if (eventAction == null)
|
||||
{
|
||||
Db.TempItems.Remove(TranSuccessEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user