feat: 搭建多租户框架
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain
|
||||
{
|
||||
public interface ISqlSugarTenantRepository : ISqlSugarRepository<TenantAggregateRoot, Guid>
|
||||
{
|
||||
Task<TenantAggregateRoot> FindByNameAsync(string name, bool includeDetails = true);
|
||||
|
||||
Task<List<TenantAggregateRoot>> GetListAsync(string sorting = null,
|
||||
int maxResultCount = int.MaxValue,
|
||||
int skipCount = 0,
|
||||
string filter = null,
|
||||
bool includeDetails = false);
|
||||
|
||||
|
||||
Task<long> GetCountAsync(
|
||||
string filter = null);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using JetBrains.Annotations;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Caching;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.ObjectMapping;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain
|
||||
{
|
||||
public class SqlSugarTenantStore : ITenantStore
|
||||
{
|
||||
private ISqlSugarTenantRepository TenantRepository { get; }
|
||||
protected ICurrentTenant CurrentTenant { get; }
|
||||
protected IDistributedCache<TenantCacheItem> Cache { get; }
|
||||
public SqlSugarTenantStore(ISqlSugarTenantRepository repository,
|
||||
IDistributedCache<TenantCacheItem> cache,
|
||||
ICurrentTenant currentTenant)
|
||||
{ TenantRepository = repository;
|
||||
Cache=cache;
|
||||
CurrentTenant=currentTenant;
|
||||
}
|
||||
|
||||
public TenantConfiguration? Find(string name)
|
||||
{
|
||||
throw new NotImplementedException("请使用异步方法");
|
||||
}
|
||||
|
||||
public TenantConfiguration? Find(Guid id)
|
||||
{
|
||||
throw new NotImplementedException("请使用异步方法");
|
||||
}
|
||||
|
||||
public async Task<TenantConfiguration?> FindAsync(string name)
|
||||
{
|
||||
return (await GetCacheItemAsync(null, name)).Value;
|
||||
}
|
||||
|
||||
public async Task<TenantConfiguration?> FindAsync(Guid id)
|
||||
{
|
||||
return (await GetCacheItemAsync(id, null)).Value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
protected virtual async Task<TenantCacheItem> GetCacheItemAsync(Guid? id, string name)
|
||||
{
|
||||
var cacheKey = CalculateCacheKey(id, name);
|
||||
|
||||
var cacheItem = await Cache.GetAsync(cacheKey, considerUow: true);
|
||||
if (cacheItem != null)
|
||||
{
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
if (id.HasValue)
|
||||
{
|
||||
using (CurrentTenant.Change(null)) //TODO: No need this if we can implement to define host side (or tenant-independent) entities!
|
||||
{
|
||||
var tenant = await TenantRepository.FindAsync(id.Value);
|
||||
return await SetCacheAsync(cacheKey, tenant);
|
||||
}
|
||||
}
|
||||
|
||||
if (!name.IsNullOrWhiteSpace())
|
||||
{
|
||||
using (CurrentTenant.Change(null)) //TODO: No need this if we can implement to define host side (or tenant-independent) entities!
|
||||
{
|
||||
var tenant = await TenantRepository.FindByNameAsync(name);
|
||||
return await SetCacheAsync(cacheKey, tenant);
|
||||
}
|
||||
}
|
||||
throw new AbpException("Both id and name can't be invalid.");
|
||||
}
|
||||
|
||||
protected virtual async Task<TenantCacheItem> SetCacheAsync(string cacheKey, [CanBeNull] TenantAggregateRoot tenant)
|
||||
{
|
||||
var tenantConfiguration = tenant != null ? MapToConfiguration(tenant) : null;
|
||||
var cacheItem = new TenantCacheItem(tenantConfiguration);
|
||||
await Cache.SetAsync(cacheKey, cacheItem, considerUow: true);
|
||||
return cacheItem;
|
||||
}
|
||||
|
||||
private TenantConfiguration MapToConfiguration(TenantAggregateRoot tenantAggregateRoot)
|
||||
{
|
||||
var tenantConfiguration = new TenantConfiguration();
|
||||
tenantConfiguration.Id = tenantAggregateRoot.Id;
|
||||
tenantConfiguration.Name = tenantAggregateRoot.Name;
|
||||
tenantConfiguration.ConnectionStrings = MaptoString(tenantAggregateRoot.TenantConnectionString);
|
||||
tenantConfiguration.IsActive = true;
|
||||
return tenantConfiguration;
|
||||
}
|
||||
|
||||
private ConnectionStrings? MaptoString(string tenantConnectionString)
|
||||
{
|
||||
|
||||
tenantConnectionString = tenantConnectionString.TrimEnd(';');
|
||||
var strSpiteds = tenantConnectionString.Split(";");
|
||||
if (strSpiteds.Count() == 0)
|
||||
{
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
var connectionStrings = new ConnectionStrings();
|
||||
foreach (string strSpited in strSpiteds)
|
||||
{
|
||||
var key = strSpited.Split('=')[0];
|
||||
var value = strSpited.Split('=')[1];
|
||||
connectionStrings[key] = value;
|
||||
}
|
||||
return connectionStrings;
|
||||
}
|
||||
|
||||
protected virtual string CalculateCacheKey(Guid? id, string name)
|
||||
{
|
||||
return TenantCacheItem.CalculateCacheKey(id, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using JetBrains.Annotations;
|
||||
using SqlSugar;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Auditing;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Entities.Auditing;
|
||||
using Volo.Abp.TenantManagement;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain
|
||||
{
|
||||
[SugarTable("Tenant")]
|
||||
public class TenantAggregateRoot : FullAuditedAggregateRoot<Guid>, IHasEntityVersion
|
||||
{
|
||||
public TenantAggregateRoot()
|
||||
{
|
||||
|
||||
}
|
||||
protected internal TenantAggregateRoot(Guid id, [NotNull] string name)
|
||||
: base(id)
|
||||
{
|
||||
SetName(name);
|
||||
}
|
||||
|
||||
[SugarColumn(IsPrimaryKey =true)]
|
||||
public override Guid Id { get ; protected set; }
|
||||
public virtual string Name { get; protected set; }
|
||||
public int EntityVersion { get; protected set; }
|
||||
|
||||
public string TenantConnectionString { get; protected set; }
|
||||
|
||||
public DbType DbType { get; protected set; }
|
||||
|
||||
[SugarColumn(IsIgnore=true)]
|
||||
public override ExtraPropertyDictionary ExtraProperties { get => base.ExtraProperties; protected set => base.ExtraProperties = value; }
|
||||
public virtual void SetConnectionString(DbType dbType,string connectionString)
|
||||
{
|
||||
DbType = dbType;
|
||||
TenantConnectionString = connectionString;
|
||||
}
|
||||
|
||||
protected internal virtual void SetName([NotNull] string name)
|
||||
{
|
||||
Name = Volo.Abp.Check.NotNullOrWhiteSpace(name, nameof(name), TenantConsts.MaxNameLength);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain;
|
||||
|
||||
[Serializable]
|
||||
[IgnoreMultiTenancy]
|
||||
public class TenantCacheItem
|
||||
{
|
||||
private const string CacheKeyFormat = "i:{0},n:{1}";
|
||||
|
||||
public TenantConfiguration Value { get; set; }
|
||||
|
||||
public TenantCacheItem()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public TenantCacheItem(TenantConfiguration value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public static string CalculateCacheKey(Guid? id, string name)
|
||||
{
|
||||
if (id == null && name.IsNullOrWhiteSpace())
|
||||
{
|
||||
throw new AbpException("Both id and name can't be invalid.");
|
||||
}
|
||||
|
||||
return string.Format(CacheKeyFormat,
|
||||
id?.ToString() ?? "null",
|
||||
name.IsNullOrWhiteSpace() ? "null" : name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="8.0.0" />
|
||||
<PackageReference Include="Volo.Abp.TenantManagement.Domain.Shared" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Volo.Abp.Domain;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.MultiTenancy;
|
||||
using Volo.Abp.TenantManagement;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Domain
|
||||
{
|
||||
[DependsOn(typeof(AbpDddDomainModule),
|
||||
typeof(AbpTenantManagementDomainSharedModule))]
|
||||
public class YiFrameworkTenantManagementDomainModule : AbpModule
|
||||
{
|
||||
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
var services = context.Services;
|
||||
services.Replace(new ServiceDescriptor(typeof(ITenantStore),typeof(SqlSugarTenantStore), ServiceLifetime.Transient));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
using Yi.Framework.SqlSugarCore.Repositories;
|
||||
using Yi.Framework.TenantManagement.Domain;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.SqlSugarCore
|
||||
{
|
||||
public class SqlSugarTenantRepository : SqlSugarRepository<TenantAggregateRoot, Guid>, ISqlSugarTenantRepository,ITransientDependency
|
||||
{
|
||||
public SqlSugarTenantRepository(ISugarDbContextProvider<ISqlSugarDbContext> sugarDbContextProvider) : base(sugarDbContextProvider)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<TenantAggregateRoot> FindByNameAsync(string name, bool includeDetails = true)
|
||||
{
|
||||
return await _DbQueryable.FirstAsync(x => x.Name == name);
|
||||
}
|
||||
|
||||
public async Task<long> GetCountAsync(string filter = null)
|
||||
{
|
||||
return await _DbQueryable.WhereIF(!string.IsNullOrEmpty(filter),x=>x.Name.Contains(filter)) .CountAsync();
|
||||
}
|
||||
|
||||
public async Task<List<TenantAggregateRoot>> GetListAsync(string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, string filter = null, bool includeDetails = false)
|
||||
{
|
||||
|
||||
|
||||
return await _DbQueryable.WhereIF(!string.IsNullOrEmpty(filter), x => x.Name.Contains(filter))
|
||||
.OrderByIF(!string.IsNullOrEmpty(sorting), sorting)
|
||||
.ToPageListAsync(skipCount, maxResultCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore\Yi.Framework.SqlSugarCore.csproj" />
|
||||
<ProjectReference Include="..\Yi.Framework.TenantManagement.Domain\Yi.Framework.TenantManagement.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
using Volo.Abp.Modularity;
|
||||
using Yi.Framework.TenantManagement.Domain;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.SqlSugarCore
|
||||
{
|
||||
[DependsOn(typeof(YiFrameworkTenantManagementDomainModule))]
|
||||
public class YiFrameworkTenantManagementSqlSugarCoreModule : AbpModule
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user