diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs index 87ef3ce4d..c363163de 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs @@ -956,6 +956,16 @@ namespace SqlSugar #endregion #region SimpleClient + public T CreateContext(bool isTran) where T : SugarUnitOfWork, new() + { + Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); + return null; + } + public SugarUnitOfWork CreateContext(bool isTran = true) + { + Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); + return null; + } //[Obsolete("Use SqlSugarClient.GetSimpleClient() Or SqlSugarClient.GetSimpleClient() ")] //public virtual SimpleClient SimpleClient //{ diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs index 42de4e6e8..827bc3698 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs @@ -188,7 +188,16 @@ namespace SqlSugar { return ScopedContext.GetDate(); } - + public T CreateContext(bool isTran) where T : SugarUnitOfWork, new() + { + Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); + return null; + } + public SugarUnitOfWork CreateContext(bool isTran = true) + { + Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); + return null; + } public SimpleClient GetSimpleClient() where T : class, new() { return ScopedContext.GetSimpleClient(); diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs index 15165f4b5..8d1bdad0b 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/ISqlSugarClient.cs @@ -44,6 +44,8 @@ namespace SqlSugar #endregion #region Other methods + T CreateContext(bool isTran=true) where T : SugarUnitOfWork, new(); + SugarUnitOfWork CreateContext(bool isTran = true); SplitTableContext SplitHelper() where T : class, new(); SplitTableContextResult SplitHelper(T data) where T : class, new(); SplitTableContextResult SplitHelper(List data) where T : class, new(); diff --git a/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs b/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs index e4ba9daa0..758226072 100644 --- a/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SimpleClient.cs @@ -23,7 +23,7 @@ namespace SqlSugar return this.Context; } - private SimpleClient() + public SimpleClient() { } diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs index 3a8886064..9e6051dd5 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarClient.cs @@ -67,6 +67,47 @@ namespace SqlSugar #endregion #region SimpleClient + + public T CreateContext(bool isTran=true) where T : SugarUnitOfWork, new() + { + T result = new T(); + _CreateContext(isTran, result); + var type = typeof(T); + var ps = type.GetProperties(); + var cacheKey = "SugarUnitOfWork" + typeof(T).FullName + typeof(T).GetHashCode(); + var properies = new ReflectionInoCacheService().GetOrCreate(cacheKey, + () => + ps.Where(it => + + (it.PropertyType.BaseType != null && it.PropertyType.BaseType.Name.StartsWith("SimpleClient`")) + || + it.PropertyType.Name.StartsWith("SimpleClient`") + + )); + foreach (var item in properies) + { + var value = Activator.CreateInstance(item.PropertyType); + value.GetType().GetProperty("Context").SetValue(value, this); + item.SetValue(result, value); + } + return result; + } + public SugarUnitOfWork CreateContext(bool isTran = true) + { + SugarUnitOfWork sugarUnitOf = new SugarUnitOfWork(); + return _CreateContext(isTran, sugarUnitOf); + } + + private SugarUnitOfWork _CreateContext(bool isTran, SugarUnitOfWork sugarUnitOf) + { + sugarUnitOf.Db = this; + sugarUnitOf.Tenant = this; + sugarUnitOf.IsTran = true; + this.Open(); + if (isTran) + this.BeginTran(); + return sugarUnitOf; + } public SimpleClient GetSimpleClient() where T : class, new() { return this.Context.GetSimpleClient(); diff --git a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs index 62f6d527f..352e4cef8 100644 --- a/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.NetCore2/SqlSugar/SqlSugarScope.cs @@ -175,6 +175,15 @@ namespace SqlSugar return ScopedContext.GetDate(); } + public T CreateContext(bool isTran = true) where T : SugarUnitOfWork, new() + { + return ScopedContext.CreateContext(isTran); + } + public SugarUnitOfWork CreateContext(bool isTran = true) + { + return ScopedContext.CreateContext(isTran); + } + public SimpleClient GetSimpleClient() where T : class, new() { return ScopedContext.GetSimpleClient(); diff --git a/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs b/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs new file mode 100644 index 000000000..c14618f1a --- /dev/null +++ b/Src/Asp.NetCore2/SqlSugar/SugarUnitOfWork.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SugarUnitOfWork : IDisposable + { + public ISqlSugarClient Db { get; internal set; } + public ITenant Tenant { get; internal set; } + public bool IsTran { get; internal set; } + public bool IsCommit { get; internal set; } + public bool IsClose { get; internal set; } + + public void Dispose() + { + + if (this.IsTran && IsCommit == false) + { + this.Tenant.RollbackTran(); + } + if (IsClose == false) + { + this.Db.Close(); + } + } + + public SimpleClient GetRepository() where T : class, new() + { + return new SimpleClient(Db); + } + + public RepositoryType GetMyRepository() where RepositoryType : ISugarRepository, new() + { + var result = new RepositoryType(); + result.Context = this.Db; + return result; + } + + public void Commit() + { + if (this.IsTran && this.IsCommit == false) + { + this.Tenant.CommitTran(); + IsCommit = true; + } + if (this.IsClose == false) + { + this.Db.Close(); + IsClose = true; + } + } + } +}