feat: 完善sass多租户,支持不同数据库类型
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Security.Policy;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -37,7 +38,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
|
||||
public IEntityChangeEventHelper EntityChangeEventHelper => LazyServiceProvider.LazyGetService<IEntityChangeEventHelper>(NullEntityChangeEventHelper.Instance);
|
||||
public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
|
||||
public AbpDbConnectionOptions ConnectionOptions=> LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbConnectionOptions>>().Value;
|
||||
public AbpDbConnectionOptions ConnectionOptions => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbConnectionOptions>>().Value;
|
||||
private ISqlSugarDbConnectionCreator _dbConnectionCreator;
|
||||
|
||||
public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient)
|
||||
@@ -56,9 +57,11 @@ namespace Yi.Framework.SqlSugarCore
|
||||
connectionCreator.OnLogExecuting = OnLogExecuting;
|
||||
connectionCreator.OnLogExecuted = OnLogExecuted;
|
||||
var currentConnection = GetCurrentConnectionString();
|
||||
var currentDbType = GetCurrentDbType();
|
||||
SqlSugarClient = new SqlSugarClient(connectionCreator.Build(action: options =>
|
||||
{
|
||||
options.ConnectionString = currentConnection;
|
||||
|
||||
}));
|
||||
connectionCreator.SetDbAop(SqlSugarClient);
|
||||
}
|
||||
@@ -78,7 +81,7 @@ namespace Yi.Framework.SqlSugarCore
|
||||
|
||||
//开启了多租户
|
||||
var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService<IConnectionStringResolver>();
|
||||
var connectionString = connectionStringResolver.ResolveAsync().Result;
|
||||
var connectionString = connectionStringResolver.ResolveAsync().GetAwaiter().GetResult();
|
||||
|
||||
|
||||
//没有检测到使用多租户功能,默认使用默认库即可
|
||||
@@ -90,6 +93,50 @@ namespace Yi.Framework.SqlSugarCore
|
||||
return connectionString!;
|
||||
}
|
||||
|
||||
protected virtual DbType GetCurrentDbType()
|
||||
{
|
||||
if (CurrentTenant.Name is not null)
|
||||
{
|
||||
var dbTypeFromTenantName = GetDbTypeFromTenantName(CurrentTenant.Name);
|
||||
if (dbTypeFromTenantName is not null)
|
||||
{
|
||||
return dbTypeFromTenantName.Value;
|
||||
}
|
||||
}
|
||||
Volo.Abp.Check.NotNull(Options.DbType, "默认DbType未配置!");
|
||||
return Options.DbType!.Value;
|
||||
}
|
||||
|
||||
//根据租户name进行匹配db类型: Test_Sqlite,[来自AI]
|
||||
private DbType? GetDbTypeFromTenantName(string name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 查找下划线的位置
|
||||
int underscoreIndex = name.LastIndexOf('_');
|
||||
|
||||
if (underscoreIndex == -1 || underscoreIndex == name.Length - 1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 提取 枚举 部分
|
||||
string enumString = name.Substring(underscoreIndex + 1);
|
||||
|
||||
// 尝试将 尾缀 转换为枚举
|
||||
if (Enum.TryParse<DbType>(enumString, out DbType result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
// 条件不满足时返回 null
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 上下文对象扩展
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
public class UnitOfWorkSqlsugarDbContextProvider<TDbContext> : ISugarDbContextProvider<TDbContext> where TDbContext : ISqlSugarDbContext
|
||||
{
|
||||
private readonly ISqlSugarDbConnectionCreator _dbConnectionCreator;
|
||||
|
||||
|
||||
public ILogger<UnitOfWorkSqlsugarDbContextProvider<TDbContext>> Logger { get; set; }
|
||||
public IServiceProvider ServiceProvider { get; set; }
|
||||
|
||||
@@ -64,8 +64,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
|
||||
|
||||
|
||||
//lock (_databaseApiLock)
|
||||
//{
|
||||
|
||||
//尝试当前工作单元获取db
|
||||
var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey);
|
||||
|
||||
@@ -74,7 +73,7 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
{
|
||||
//db根据连接字符串来创建
|
||||
databaseApi = new SqlSugarDatabaseApi(
|
||||
CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result
|
||||
await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString)
|
||||
);
|
||||
|
||||
//await Console.Out.WriteLineAsync(">>>----------------实例化了db"+ ((SqlSugarDatabaseApi)databaseApi).DbContext.SqlSugarClient.ContextID.ToString());
|
||||
@@ -83,8 +82,6 @@ namespace Yi.Framework.SqlSugarCore.Uow
|
||||
|
||||
}
|
||||
return (TDbContext)((SqlSugarDatabaseApi)databaseApi).DbContext;
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,42 +1,64 @@
|
||||
using JetBrains.Annotations;
|
||||
using System.Xml.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.MultiTenancy.ConfigurationStore;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain
|
||||
{
|
||||
public class SqlSugarTenantStore : ITenantStore
|
||||
public class SqlSugarAndConfigurationTenantStore : DefaultTenantStore, ITenantStore
|
||||
{
|
||||
private ISqlSugarTenantRepository TenantRepository { get; }
|
||||
protected ICurrentTenant CurrentTenant { get; }
|
||||
protected IDistributedCache<TenantCacheItem> Cache { get; }
|
||||
public SqlSugarTenantStore(ISqlSugarTenantRepository repository,
|
||||
public SqlSugarAndConfigurationTenantStore(ISqlSugarTenantRepository repository,
|
||||
IDistributedCache<TenantCacheItem> cache,
|
||||
ICurrentTenant currentTenant)
|
||||
{ TenantRepository = repository;
|
||||
Cache=cache;
|
||||
CurrentTenant=currentTenant;
|
||||
ICurrentTenant currentTenant,
|
||||
IOptionsMonitor<AbpDefaultTenantStoreOptions> options) : base(options)
|
||||
{
|
||||
TenantRepository = repository;
|
||||
Cache = cache;
|
||||
CurrentTenant = currentTenant;
|
||||
}
|
||||
|
||||
public TenantConfiguration? Find(string name)
|
||||
public new TenantConfiguration? Find(string name)
|
||||
{
|
||||
throw new NotImplementedException("请使用异步方法");
|
||||
}
|
||||
|
||||
public TenantConfiguration? Find(Guid id)
|
||||
public new TenantConfiguration? Find(Guid id)
|
||||
{
|
||||
throw new NotImplementedException("请使用异步方法");
|
||||
}
|
||||
|
||||
public async Task<TenantConfiguration?> FindAsync(string name)
|
||||
public new async Task<TenantConfiguration?> FindAsync(string name)
|
||||
{
|
||||
return (await GetCacheItemAsync(null, name)).Value;
|
||||
var tenantFromOptions = await base.FindAsync(name);
|
||||
//如果配置文件不存在改租户
|
||||
if (tenantFromOptions is null)
|
||||
{
|
||||
return (await GetCacheItemAsync(null, name)).Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return tenantFromOptions;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TenantConfiguration?> FindAsync(Guid id)
|
||||
public new async Task<TenantConfiguration?> FindAsync(Guid id)
|
||||
{
|
||||
return (await GetCacheItemAsync(id, null)).Value;
|
||||
var tenantFromOptions = await base.FindAsync(id);
|
||||
if (tenantFromOptions is null)
|
||||
{
|
||||
return (await GetCacheItemAsync(id, null)).Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
return tenantFromOptions;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Yi.Framework.TenantManagement.Domain
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
var services = context.Services;
|
||||
services.Replace(new ServiceDescriptor(typeof(ITenantStore), typeof(SqlSugarTenantStore), ServiceLifetime.Transient));
|
||||
services.Replace(new ServiceDescriptor(typeof(ITenantStore), typeof(SqlSugarAndConfigurationTenantStore), ServiceLifetime.Transient));
|
||||
|
||||
services.Replace(new ServiceDescriptor(typeof(IConnectionStringResolver), typeof(YiMultiTenantConnectionStringResolver), ServiceLifetime.Transient));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ public class YiTenantConfigurationProvider : ITenantConfigurationProvider, ITran
|
||||
|
||||
public virtual async Task<TenantConfiguration?> GetAsync(bool saveResolveResult = false)
|
||||
{
|
||||
//租户解析器获取到当前解析成功的租户
|
||||
var resolveResult = await TenantResolver.ResolveTenantIdOrNameAsync();
|
||||
|
||||
if (saveResolveResult)
|
||||
@@ -36,6 +37,7 @@ public class YiTenantConfigurationProvider : ITenantConfigurationProvider, ITran
|
||||
TenantConfiguration? tenant = null;
|
||||
if (resolveResult.TenantIdOrName != null)
|
||||
{
|
||||
//根据租户信息获取租户
|
||||
tenant = await FindTenantAsync(resolveResult.TenantIdOrName);
|
||||
|
||||
if (tenant == null)
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user