mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-19 16:06:36 +08:00
feat: 优化多租户配置
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
|
||||
namespace Yi.Framework.SqlSugarCore;
|
||||
|
||||
/// <summary>
|
||||
/// 租户配置
|
||||
/// </summary>
|
||||
public class TenantConfigurationWrapper : ITransientDependency
|
||||
{
|
||||
private ICurrentTenant _currentTenant;
|
||||
private ITenantStore _tenantStore;
|
||||
|
||||
public TenantConfigurationWrapper(ICurrentTenant currentTenant, ITenantStore tenantStore)
|
||||
{
|
||||
_currentTenant = currentTenant;
|
||||
_tenantStore = tenantStore;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取租户信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<TenantConfiguration?> GetAsync()
|
||||
{
|
||||
if (_currentTenant.Id is not null)
|
||||
{
|
||||
return await _tenantStore.FindAsync(_currentTenant.Id.Value);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_currentTenant.Name))
|
||||
{
|
||||
return await _tenantStore.FindAsync(_currentTenant.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return await _tenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前连接字符串
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetCurrentConnectionStringAsync()
|
||||
{
|
||||
return (await GetAsync()).ConnectionStrings.Default!;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前连接名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetCurrentConnectionNameAsync()
|
||||
{
|
||||
return (await GetAsync()).Name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TenantConfigurationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前连接字符串
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCurrentConnectionString(this TenantConfiguration tenantConfiguration)
|
||||
{
|
||||
return tenantConfiguration.ConnectionStrings.Default!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前连接名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetCurrentConnectionName(this TenantConfiguration tenantConfiguration)
|
||||
{
|
||||
return tenantConfiguration.Name;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user