mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add UnitOfWork
This commit is contained in:
parent
05abdbbf35
commit
b9c5395dae
@ -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才能使用CreateContext,childDb不能使用");
|
||||
return null;
|
||||
}
|
||||
//[Obsolete("Use SqlSugarClient.GetSimpleClient() Or SqlSugarClient.GetSimpleClient<T>() ")]
|
||||
//public virtual SimpleClient SimpleClient
|
||||
//{
|
||||
|
@ -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才能使用CreateContext,childDb不能使用");
|
||||
return null;
|
||||
}
|
||||
public SimpleClient<T> GetSimpleClient<T>() where T : class, new()
|
||||
{
|
||||
return ScopedContext.GetSimpleClient<T>();
|
||||
|
@ -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();
|
||||
|
@ -23,7 +23,7 @@ namespace SqlSugar
|
||||
return this.Context;
|
||||
}
|
||||
|
||||
private SimpleClient()
|
||||
public SimpleClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -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" />
|
||||
|
@ -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>();
|
||||
|
@ -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>();
|
||||
|
56
Src/Asp.Net/SqlSugar/SugarUnitOfWork.cs
Normal file
56
Src/Asp.Net/SqlSugar/SugarUnitOfWork.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user