Beautification code

This commit is contained in:
sunkaixuan 2019-05-07 12:48:47 +08:00
parent 67be63712b
commit f9f3c32b83
3 changed files with 121 additions and 29 deletions

View File

@ -213,7 +213,7 @@ namespace OrmTest
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
try try
{ {
db.BeginAllTran(); db.BeginTran();
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
db.Deleteable<Order>().ExecuteCommand(); db.Deleteable<Order>().ExecuteCommand();
@ -226,11 +226,11 @@ namespace OrmTest
Console.WriteLine(db.Queryable<Order>().Count()); Console.WriteLine(db.Queryable<Order>().Count());
throw new Exception(); throw new Exception();
db.CommitAllTran(); db.CommitTran();
} }
catch catch
{ {
db.RollbackAllTran(); db.RollbackTran();
Console.WriteLine("---Roll back"); Console.WriteLine("---Roll back");
db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver db.ChangeDatabase(it => it.DbType == DbType.SqlServer);//use sqlserver
Console.WriteLine(db.CurrentConnectionConfig.DbType); Console.WriteLine(db.CurrentConnectionConfig.DbType);
@ -243,9 +243,6 @@ namespace OrmTest
Console.WriteLine("#### Distributed TransactionExample End ####"); Console.WriteLine("#### Distributed TransactionExample End ####");
} }
} }
/// <summary> /// <summary>

View File

@ -2,15 +2,24 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace SqlSugar namespace SqlSugar
{ {
public interface ITenant public interface ITenant
{ {
void BeginAllTran(); void BeginTran();
void CommitAllTran(); void CommitTran();
void RollbackAllTran(); void RollbackTran();
void ChangeDatabase(string configId); void ChangeDatabase(string configId);
void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression); void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression);
DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null);
Task<DbResult<bool>> UseTranAsync(Action action, Action<Exception> errorCallBack = null);
DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null);
Task<DbResult<T>> UseTranAsync<T>(Func<T> action, Action<Exception> errorCallBack = null);
void Close();
void Open();
} }
} }

View File

@ -17,6 +17,7 @@ namespace SqlSugar
private ConnectionConfig _CurrentConnectionConfig; private ConnectionConfig _CurrentConnectionConfig;
private List<SugarTenant> _AllClients; private List<SugarTenant> _AllClients;
private bool _IsAllTran = false; private bool _IsAllTran = false;
private bool _IsOpen = false;
private MappingTableList _MappingTables; private MappingTableList _MappingTables;
private MappingColumnList _MappingColumns; private MappingColumnList _MappingColumns;
private IgnoreColumnList _IgnoreColumns; private IgnoreColumnList _IgnoreColumns;
@ -481,15 +482,6 @@ namespace SqlSugar
#region Ado #region Ado
public IAdo Ado => this.Context.Ado; public IAdo Ado => this.Context.Ado;
public void Close()
{
this.Context.Close();
}
public void Open()
{
this.Context.Open();
}
#endregion #endregion
#region Deleteable #region Deleteable
@ -541,13 +533,15 @@ namespace SqlSugar
public QueryFilterProvider QueryFilter { get => this.Context.QueryFilter; set => this.Context.QueryFilter = value; } public QueryFilterProvider QueryFilter { get => this.Context.QueryFilter; set => this.Context.QueryFilter = value; }
#endregion #endregion
#region ITenant #region TenantManager
public void ChangeDatabase(string configId) public void ChangeDatabase(string configId)
{ {
Check.Exception(!_AllClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId); Check.Exception(!_AllClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId);
InitTenant(_AllClients.First(it => it.ConnectionConfig.ConfigId == configId)); InitTenant(_AllClients.First(it => it.ConnectionConfig.ConfigId == configId));
if (this._IsAllTran) if (this._IsAllTran)
this.Ado.BeginTran(); this.Ado.BeginTran();
if (this._IsOpen)
this.Open();
} }
public void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression) public void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression)
{ {
@ -556,41 +550,115 @@ namespace SqlSugar
InitTenant(_AllClients.First(it => it.ConnectionConfig == allConfigs.First(changeExpression))); InitTenant(_AllClients.First(it => it.ConnectionConfig == allConfigs.First(changeExpression)));
if (this._IsAllTran) if (this._IsAllTran)
this.Ado.BeginTran(); this.Ado.BeginTran();
if (this._IsOpen)
this.Open();
} }
public void BeginAllTran() public void BeginTran()
{ {
_IsAllTran = true; _IsAllTran = true;
this.Context.Ado.BeginTran(); this.Context.Ado.BeginTran();
} }
public void CommitAllTran() public void CommitTran()
{ {
if (_AllClients.HasValue()) this.Context.Ado.CommitTran();
{ AllClientEach(it => it.Ado.CommitTran());
foreach (var item in _AllClients.Where(it => it.Context.HasValue()))
{
item.Context.Ado.CommitTran();
}
}
_IsAllTran = false; _IsAllTran = false;
} }
public DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null)
{
var result = new DbResult<bool>();
try
{
this.BeginTran();
if (action != null)
action();
this.CommitTran();
result.Data = result.IsSuccess = true;
}
catch (Exception ex)
{
result.ErrorException = ex;
result.ErrorMessage = ex.Message;
result.IsSuccess = false;
this.RollbackTran();
if (errorCallBack != null)
{
errorCallBack(ex);
}
}
return result;
}
public void RollbackAllTran() public Task<DbResult<bool>> UseTranAsync(Action action, Action<Exception> errorCallBack = null)
{ {
if (_AllClients.HasValue()) Task<DbResult<bool>> result = new Task<DbResult<bool>>(() =>
{ {
foreach (var item in _AllClients.Where(it => it.Context.HasValue())) return UseTran(action, errorCallBack);
});
TaskStart(result);
return result;
}
public DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null)
{ {
item.Context.Ado.RollbackTran(); var result = new DbResult<T>();
try
{
this.BeginTran();
if (action != null)
result.Data = action();
this.CommitTran();
result.IsSuccess = true;
}
catch (Exception ex)
{
result.ErrorException = ex;
result.ErrorMessage = ex.Message;
result.IsSuccess = false;
this.RollbackTran();
if (errorCallBack != null)
{
errorCallBack(ex);
} }
} }
return result;
}
public Task<DbResult<T>> UseTranAsync<T>(Func<T> action, Action<Exception> errorCallBack = null)
{
Task<DbResult<T>> result = new Task<DbResult<T>>(() =>
{
return UseTran(action, errorCallBack);
});
TaskStart(result);
return result;
}
public void RollbackTran()
{
this.Context.Ado.RollbackTran();
AllClientEach(it=>it.Ado.RollbackTran());
_IsAllTran = false; _IsAllTran = false;
} }
public void Close()
{
this.Context.Close();
AllClientEach(it => it.Close());
_IsOpen = false;
}
public void Open()
{
this.Context.Open();
_IsOpen = true;
}
#endregion #endregion
#region IDispose #region IDispose
public void Dispose() public void Dispose()
{ {
this.Context.Dispose(); this.Context.Dispose();
AllClientEach(it => it.Ado.RollbackTran());
} }
#endregion #endregion
@ -716,6 +784,16 @@ namespace SqlSugar
} }
} }
} }
private void AllClientEach(Action<ISqlSugarClient> action)
{
if (_AllClients.HasValue())
{
foreach (var item in _AllClients.Where(it => it.Context.HasValue()))
{
action(item.Context);
}
}
}
private void InitTenant(SugarTenant Tenant) private void InitTenant(SugarTenant Tenant)
{ {
@ -726,6 +804,14 @@ namespace SqlSugar
_Context = Tenant.Context; _Context = Tenant.Context;
this.CurrentConnectionConfig = Tenant.ConnectionConfig; this.CurrentConnectionConfig = Tenant.ConnectionConfig;
} }
private void TaskStart<Type>(Task<Type> result)
{
if (this.Context.CurrentConnectionConfig.IsShardSameThread)
{
Check.Exception(true, "IsShardSameThread=true can't be used async method");
}
result.Start();
}
#endregion #endregion
#region Obsolete #region Obsolete