Skip to content

Commit 97f214a

Browse files
committed
🎨 refactor(multi-tenancy): 重构多租户体系
- 优化了租户解析逻辑,支持更多种类型的租户标识 - 重构了租户配置存储接口,增加了获取所有租户列表的方法 - 改进了租户配置提供程序,支持异步获取租户配置 - 优化了当前租户访问器,支持可空类型 - 重构了租户解析上下文和解析结果访问器,支持可空类型 - 优化了多语言配置,为多租户支持做好准备
1 parent 16bc3ae commit 97f214a

30 files changed

+234
-90
lines changed

framework/src/Bing.AspNetCore.MultiTenancy/Bing/AspNetCore/MultiTenancy/DomainTenantResolveContributor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ namespace Bing.AspNetCore.MultiTenancy;
99
/// </summary>
1010
public class DomainTenantResolveContributor : HttpTenantResolveContributorBase
1111
{
12+
/// <summary>
13+
/// 域名格式字符串
14+
/// </summary>
15+
private readonly string _domainFormat;
16+
1217
/// <summary>
1318
/// 初始化一个<see cref="DomainTenantResolveContributor"/>类型的实例
1419
/// </summary>
1520
/// <param name="domainFormat">域名格式字符串。范例:{0}.a.com</param>
1621
public DomainTenantResolveContributor(string domainFormat)
1722
{
18-
DomainFormat = domainFormat;
23+
_domainFormat = DomainTenantResolverHelper.RemoveDomainPrefix(domainFormat);
1924
}
2025

2126
/// <summary>
@@ -31,7 +36,7 @@ public DomainTenantResolveContributor(string domainFormat)
3136
/// <summary>
3237
/// 域名格式字符串
3338
/// </summary>
34-
public string DomainFormat { get; }
39+
public string DomainFormat => _domainFormat;
3540

3641
/// <summary>
3742
/// 从 <see cref="HttpContext"/> 中获取租户标识、租户名称、null

framework/src/Bing.AspNetCore.MultiTenancy/Bing/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
8282
{
8383
BingMultiTenancyCookieHelper.SetTenantCookie(context, _currentTenant.Id, _options.TenantKey);
8484
}
85-
85+
// TODO: 设置国际化
8686
await next(context);
8787
}
8888
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Bing.AspNetCore.MultiTenancy;
2+
using Bing.Collections;
3+
4+
namespace Bing.MultiTenancy;
5+
6+
/// <summary>
7+
/// 租户解析选项配置(<see cref="BingTenantResolveOptions" />) 扩展
8+
/// </summary>
9+
public static class BingTenantResolveOptionsExtensions
10+
{
11+
/// <summary>
12+
/// 添加基于域名的租户解析器,在当前用户租户解析器之后插入。
13+
/// </summary>
14+
/// <param name="options">用户解析选项</param>
15+
/// <param name="domainFormat">用于解析租户的域名格式</param>
16+
public static void AddDomainTenantResolver(this BingTenantResolveOptions options, string domainFormat)
17+
{
18+
options.TenantResolvers.InsertAfter(
19+
r => r is CurrentUserTenantResolveContributor,
20+
new DomainTenantResolveContributor(domainFormat));
21+
}
22+
}

framework/src/Bing.MultiTenancy.Abstractions/Bing.MultiTenancy.Abstractions.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
<Import Project="..\..\..\framework.props" />
99

10+
<PropertyGroup>
11+
<Nullable>enable</Nullable>
12+
</PropertyGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\Bing.Core\Bing.Core.csproj" />
1216
<ProjectReference Include="..\Bing.Data\Bing.Data.csproj" />

framework/src/Bing.MultiTenancy.Abstractions/Bing/MultiTenancy/BasicTenantInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ public class BasicTenantInfo
88
/// <summary>
99
/// 租户标识
1010
/// </summary>
11-
public string TenantId { get; set; }
11+
public string? TenantId { get; }
1212

1313
/// <summary>
1414
/// 租户名称
1515
/// </summary>
16-
public string Name { get; set; }
16+
public string? Name { get; }
1717

1818
/// <summary>
1919
/// 初始化一个<see cref="BasicTenantInfo"/>类型的实例
2020
/// </summary>
2121
/// <param name="tenantId">租户ID</param>
2222
/// <param name="name">租户名称</param>
23-
public BasicTenantInfo(string tenantId, string name = null)
23+
public BasicTenantInfo(string? tenantId, string? name = null)
2424
{
2525
TenantId = tenantId;
2626
Name = name;

framework/src/Bing.MultiTenancy.Abstractions/Bing/MultiTenancy/BingTenantResolveOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Bing.MultiTenancy;
22

33
/// <summary>
4-
/// 租户解析器选项配置
4+
/// 租户解析选项
55
/// </summary>
66
public class BingTenantResolveOptions
77
{
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Bing.Helpers;
2+
3+
namespace Bing.MultiTenancy;
4+
5+
/// <summary>
6+
/// 当前租户(<see cref="ICurrentTenant"/>) 扩展
7+
/// </summary>
8+
public static class CurrentTenantExtensions
9+
{
10+
/// <summary>
11+
/// 获取当前租户的 ID,并转换为 <see cref="Guid"/> 类型。
12+
/// </summary>
13+
/// <param name="currentTenant">当前租户信息。</param>
14+
/// <returns>返回租户的 <see cref="Guid"/> ID。</returns>
15+
/// <exception cref="ArgumentNullException">如果 <paramref name="currentTenant"/> 为空,则抛出此异常。</exception>
16+
/// <exception cref="ArgumentException">如果租户 ID 为空或无效,则抛出此异常。</exception>
17+
public static Guid GetId(this ICurrentTenant currentTenant)
18+
{
19+
Check.NotNull(currentTenant, nameof(currentTenant));
20+
if (string.IsNullOrWhiteSpace(currentTenant.Id))
21+
throw new ArgumentException("Current Tenant Id is not available!");
22+
return Conv.ToGuid(currentTenant.Id);
23+
}
24+
25+
/// <summary>
26+
/// 获取当前租户的 ID,并转换为指定的类型 <typeparamref name="T"/>。
27+
/// </summary>
28+
/// <typeparam name="T">目标 ID 类型,例如 <see cref="Guid"/> 或 <see cref="int"/>。</typeparam>
29+
/// <param name="currentTenant">当前租户信息。</param>
30+
/// <returns>返回租户的 ID,转换为类型 <typeparamref name="T"/>。</returns>
31+
/// <exception cref="ArgumentNullException">如果 <paramref name="currentTenant"/> 为空,则抛出此异常。</exception>
32+
/// <exception cref="ArgumentException">如果租户 ID 为空或无效,则抛出此异常。</exception>
33+
public static T GetId<T>(this ICurrentTenant currentTenant)
34+
{
35+
Check.NotNull(currentTenant, nameof(currentTenant));
36+
if (string.IsNullOrWhiteSpace(currentTenant.Id))
37+
throw new ArgumentException("Current Tenant Id is not available!");
38+
return Conv.To<T>(currentTenant.Id);
39+
}
40+
}

framework/src/Bing.MultiTenancy.Abstractions/Bing/MultiTenancy/ICurrentTenant.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ public interface ICurrentTenant
1313
/// <summary>
1414
/// 租户标识
1515
/// </summary>
16-
string Id { get; }
16+
string? Id { get; }
1717

1818
/// <summary>
1919
/// 租户名称
2020
/// </summary>
21-
string Name { get; }
21+
string? Name { get; }
2222

2323
/// <summary>
2424
/// 变更
2525
/// </summary>
2626
/// <param name="id">租户标识</param>
2727
/// <param name="name">租户名称</param>
28-
IDisposable Change(string id, string name = null);
28+
IDisposable Change(string id, string? name = null);
2929
}

framework/src/Bing.MultiTenancy.Abstractions/Bing/MultiTenancy/ICurrentTenantAccessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ public interface ICurrentTenantAccessor
88
/// <summary>
99
/// 当前基本租户信息
1010
/// </summary>
11-
BasicTenantInfo Current { get; set; }
12-
}
11+
BasicTenantInfo? Current { get; set; }
12+
}

framework/src/Bing.MultiTenancy.Abstractions/Bing/MultiTenancy/ISwitchTenantManager.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)