chore: 构建
This commit is contained in:
@@ -44,19 +44,6 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
public virtual async Task<TDbContext> GetDbContextAsync()
|
||||
{
|
||||
|
||||
var unitOfWork = UnitOfWorkManager.Current;
|
||||
if (unitOfWork == null || unitOfWork.Options.IsTransactional == false)
|
||||
{
|
||||
if (ContextInstance.Current is null)
|
||||
{
|
||||
ContextInstance.Current = (TDbContext)ServiceProvider.GetRequiredService<ISqlSugarDbContext>();
|
||||
}
|
||||
//提高体验,取消工作单元强制性
|
||||
//throw new AbpException("A DbContext can only be created inside a unit of work!");
|
||||
//如果不启用工作单元,创建一个新的db,不开启事务即可
|
||||
return (TDbContext)ContextInstance.Current;
|
||||
}
|
||||
|
||||
var connectionStringName = ConnectionStrings.DefaultConnectionStringName;
|
||||
|
||||
//获取当前连接字符串,未多租户时,默认为空
|
||||
@@ -64,25 +51,42 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
var dbContextKey = $"{this.GetType().FullName}_{connectionString}";
|
||||
|
||||
|
||||
var unitOfWork = UnitOfWorkManager.Current;
|
||||
if (unitOfWork == null || unitOfWork.Options.IsTransactional == false)
|
||||
{
|
||||
if (ContextInstance.Current is null)
|
||||
{
|
||||
ContextInstance.Current = (TDbContext)ServiceProvider.GetRequiredService<ISqlSugarDbContext>();
|
||||
}
|
||||
var dbContext = (TDbContext)ContextInstance.Current;
|
||||
var output = DatabaseChange(dbContext, connectionStringName, connectionString);
|
||||
//提高体验,取消工作单元强制性
|
||||
//throw new AbpException("A DbContext can only be created inside a unit of work!");
|
||||
//如果不启用工作单元,创建一个新的db,不开启事务即可
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//lock (_databaseApiLock)
|
||||
//{
|
||||
//尝试当前工作单元获取db
|
||||
var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);
|
||||
//尝试当前工作单元获取db
|
||||
var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);
|
||||
|
||||
//当前没有db创建一个新的db
|
||||
if (databaseApi == null)
|
||||
{
|
||||
//db根据连接字符串来创建
|
||||
databaseApi = new SqlSugarDatabaseApi(
|
||||
CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result
|
||||
);
|
||||
//当前没有db创建一个新的db
|
||||
if (databaseApi == null)
|
||||
{
|
||||
//db根据连接字符串来创建
|
||||
databaseApi = new SqlSugarDatabaseApi(
|
||||
CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result
|
||||
);
|
||||
|
||||
//创建的db加入到当前工作单元中
|
||||
unitOfWork.AddDatabaseApi(dbContextKey, databaseApi);
|
||||
//创建的db加入到当前工作单元中
|
||||
unitOfWork.AddDatabaseApi(dbContextKey, databaseApi);
|
||||
|
||||
}
|
||||
return (TDbContext)((SqlSugarDatabaseApi)databaseApi).DbContext;
|
||||
}
|
||||
return (TDbContext)((SqlSugarDatabaseApi)databaseApi).DbContext;
|
||||
//}
|
||||
|
||||
}
|
||||
@@ -97,13 +101,6 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
{
|
||||
var dbContext = await CreateDbContextAsync(unitOfWork);
|
||||
|
||||
//没有检测到使用多租户功能,默认使用默认库即可
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
connectionString = dbContext.Options.Url;
|
||||
connectionStringName = DbConnOptions.TenantDbDefaultName;
|
||||
}
|
||||
|
||||
//获取到DB之后,对多租户多库进行处理
|
||||
var changedDbContext = DatabaseChange(dbContext, connectionStringName, connectionString);
|
||||
return changedDbContext;
|
||||
@@ -112,6 +109,13 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
|
||||
protected virtual TDbContext DatabaseChange(TDbContext dbContext, string configId, string connectionString)
|
||||
{
|
||||
//没有检测到使用多租户功能,默认使用默认库即可
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
{
|
||||
connectionString = dbContext.Options.Url;
|
||||
configId = DbConnOptions.TenantDbDefaultName;
|
||||
}
|
||||
|
||||
var dbOption = dbContext.Options;
|
||||
var db = dbContext.SqlSugarClient.AsTenant();
|
||||
//主库的Db切换,当操作的是租户表的时候
|
||||
|
||||
@@ -16,7 +16,8 @@ namespace Yi.Abp.Application.Services
|
||||
public class TestService : ApplicationService
|
||||
{
|
||||
/// <summary>
|
||||
/// 属性注入,不推荐,坑太多,容易把自己玩死,简单的东西可以用一用
|
||||
/// 属性注入
|
||||
/// 不推荐,坑太多,容易把自己玩死,简单的东西可以用一用
|
||||
/// </summary>
|
||||
public ISqlSugarRepository<BannerEntity> sqlSugarRepository { get; set; }
|
||||
|
||||
@@ -45,11 +46,12 @@ namespace Yi.Abp.Application.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 工作单元魔改
|
||||
/// 工作单元
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task GetUowAsync()
|
||||
{
|
||||
//魔改
|
||||
// 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持多线程无工作单元,支持。。。
|
||||
// 自动在各个情况处理db客户端最优解之一
|
||||
int i = 10;
|
||||
|
||||
Reference in New Issue
Block a user