Update Core

This commit is contained in:
sunkaixuan 2022-05-23 23:52:08 +08:00
parent f98bb577fd
commit fe2cc584d7
7 changed files with 129 additions and 2 deletions

View File

@ -956,6 +956,16 @@ namespace SqlSugar
#endregion
#region SimpleClient
public T CreateContext<T>(bool isTran) where T : SugarUnitOfWork, new()
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");
return null;
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");
return null;
}
//[Obsolete("Use SqlSugarClient.GetSimpleClient() Or SqlSugarClient.GetSimpleClient<T>() ")]
//public virtual SimpleClient SimpleClient
//{

View File

@ -188,7 +188,16 @@ namespace SqlSugar
{
return ScopedContext.GetDate();
}
public T CreateContext<T>(bool isTran) where T : SugarUnitOfWork, new()
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");
return null;
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContextchildDb不能使用");
return null;
}
public SimpleClient<T> GetSimpleClient<T>() where T : class, new()
{
return ScopedContext.GetSimpleClient<T>();

View File

@ -44,6 +44,8 @@ namespace SqlSugar
#endregion
#region Other methods
T CreateContext<T>(bool isTran=true) where T : SugarUnitOfWork, new();
SugarUnitOfWork CreateContext(bool isTran = true);
SplitTableContext SplitHelper<T>() where T : class, new();
SplitTableContextResult<T> SplitHelper<T>(T data) where T : class, new();
SplitTableContextResult<T> SplitHelper<T>(List<T> data) where T : class, new();

View File

@ -23,7 +23,7 @@ namespace SqlSugar
return this.Context;
}
private SimpleClient()
public SimpleClient()
{
}

View File

@ -67,6 +67,47 @@ namespace SqlSugar
#endregion
#region SimpleClient
public T CreateContext<T>(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<T> GetSimpleClient<T>() where T : class, new()
{
return this.Context.GetSimpleClient<T>();

View File

@ -175,6 +175,15 @@ namespace SqlSugar
return ScopedContext.GetDate();
}
public T CreateContext<T>(bool isTran = true) where T : SugarUnitOfWork, new()
{
return ScopedContext.CreateContext<T>(isTran);
}
public SugarUnitOfWork CreateContext(bool isTran = true)
{
return ScopedContext.CreateContext(isTran);
}
public SimpleClient<T> GetSimpleClient<T>() where T : class, new()
{
return ScopedContext.GetSimpleClient<T>();

View File

@ -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<T> GetRepository<T>() where T : class, new()
{
return new SimpleClient<T>(Db);
}
public RepositoryType GetMyRepository<RepositoryType>() 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;
}
}
}
}