feat: 完善sass多租户,支持不同数据库类型

This commit is contained in:
橙子
2024-07-07 00:59:34 +08:00
parent b49ae9c034
commit 38bfc87fc1
6 changed files with 91 additions and 23 deletions

View File

@@ -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;
}
}

View File

@@ -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));
}

View File

@@ -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)