Add UnitOfWork

This commit is contained in:
sunkaixuan 2022-05-23 20:49:27 +08:00
parent 05abdbbf35
commit b9c5395dae
8 changed files with 87 additions and 2 deletions

View File

@ -956,6 +956,11 @@ namespace SqlSugar
#endregion
#region SimpleClient
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,11 @@ namespace SqlSugar
{
return ScopedContext.GetDate();
}
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,7 @@ namespace SqlSugar
#endregion
#region Other methods
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

@ -103,6 +103,7 @@
<Compile Include="Abstract\SaveableProvider\StorageableDataTable.cs" />
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="SugarUnitOfWork.cs" />
<Compile Include="Entities\SugarAbMapping.cs" />
<Compile Include="Entities\JoinMapper.cs" />
<Compile Include="Enum\DbLockType.cs" />

View File

@ -67,6 +67,19 @@ namespace SqlSugar
#endregion
#region SimpleClient
public SugarUnitOfWork CreateContext(bool isTran = true)
{
SugarUnitOfWork sugarUnitOf = new SugarUnitOfWork();
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,11 @@ namespace SqlSugar
return ScopedContext.GetDate();
}
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;
}
}
}
}