diff --git a/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs b/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs index 496a77d01568765271532f57ddc36ac08f3bd18f..c6524b20354f255503ca4ab8c17e791ca47f57bb 100644 --- a/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs +++ b/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs @@ -10,7 +10,7 @@ namespace SqlSugar public interface ISugarUnitOfWork where T : SugarUnitOfWork, new() { ISqlSugarClient Db { get; set; } - T CreateContext(bool isTran=true); + T CreateContext(bool isTran = true); } public class SugarUnitOfWork : ISugarUnitOfWork 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(isTran); } @@ -36,8 +36,8 @@ namespace SqlSugar /// public interface ISugarUnitOfWork : ISugarUnitOfWorkClear { - ISqlSugarClient Db { get; } - ITenant Tenant { get; } + ISqlSugarClient Db { get; } + ITenant Tenant { get; } SimpleClient GetRepository() where T : class, new(); } @@ -48,6 +48,10 @@ namespace SqlSugar /// public class SugarUnitOfWork : IDisposable, ISugarUnitOfWork { + /// + /// 事务后事件唯一Key + /// + private 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 +65,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 +78,13 @@ namespace SqlSugar { return new SimpleClient(Db); } - else + else { return new SimpleClient(Db.AsTenant().GetConnection(tenantAttribute.configId)); } } - public RepositoryType GetMyRepository() where RepositoryType:new() + public RepositoryType GetMyRepository() where RepositoryType : new() { var result = (ISugarRepository)new RepositoryType(); var type = typeof(RepositoryType).GetGenericArguments().FirstOrDefault(); @@ -89,7 +93,7 @@ namespace SqlSugar { result.Context = this.Db; } - else + else { result.Context = this.Db.AsTenant().GetConnection(tenantAttribute.configId); } @@ -102,13 +106,52 @@ namespace SqlSugar { this.Tenant.CommitTran(); IsCommit = true; + if (Db.TempItems.TryGetValue(TranSuccessEvent, out object value) && value is Action eventAction) + { + eventAction(Db); + Db.TempItems.Remove(TranSuccessEvent); + } } - if (this.Db.Ado.Transaction==null&&this.IsClose == false) + if (this.Db.Ado.Transaction == null && this.IsClose == false) { this.Db.Close(); IsClose = true; } return IsCommit; } + + /// + /// 增加事务成功后事件 + /// + /// 需要执行的委托 + public void AppendTranSuccessEvent(Action action) + { + if (Db.TempItems.TryGetValue(TranSuccessEvent, out object value) && value != null && value is Action eventAction) + { + eventAction += action; + Db.TempItems[TranSuccessEvent] = eventAction; + } + else + { + Db.TempItems[TranSuccessEvent] = action; + } + } + + /// + /// 减少事务成功后事件 + /// + /// + public void SubtractTranSuccessEvent(Action action) + { + if (Db.TempItems.TryGetValue(TranSuccessEvent, out object value) && value is Action eventAction) + { + eventAction -= action; + Db.TempItems[TranSuccessEvent] = eventAction; + if (eventAction == null) + { + Db.TempItems.Remove(TranSuccessEvent); + } + } + } } }