Skip to content

Commit fbf99f4

Browse files
committed
fix: 调整多租户库
1 parent 7627757 commit fbf99f4

14 files changed

+186
-19
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
<Import Project="project.dependency.props" />
55

66
<Import Project="..\..\..\framework.props" />
7-
87
</Project>
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
using System.Threading;
2-
using Bing.DependencyInjection;
3-
4-
namespace Bing.MultiTenancy;
1+
namespace Bing.MultiTenancy;
52

63
/// <summary>
74
/// 基于 <see cref="AsyncLocal{T}"/> 实现的当前租户访问器
85
/// </summary>
9-
public class AsyncLocalCurrentTenantAccessor : ICurrentTenantAccessor, ISingletonDependency
6+
public class AsyncLocalCurrentTenantAccessor : ICurrentTenantAccessor
107
{
8+
/// <summary>
9+
/// 实例
10+
/// </summary>
11+
public static AsyncLocalCurrentTenantAccessor Instance { get; } = new();
12+
13+
/// <summary>
14+
/// 当前作用域的基本租户信息
15+
/// </summary>
16+
private readonly AsyncLocal<BasicTenantInfo> _currentScope;
17+
1118
/// <summary>
1219
/// 当前基本租户信息
1320
/// </summary>
@@ -17,13 +24,8 @@ public BasicTenantInfo Current
1724
set => _currentScope.Value = value;
1825
}
1926

20-
/// <summary>
21-
/// 当前作用域的基本租户信息
22-
/// </summary>
23-
private readonly AsyncLocal<BasicTenantInfo> _currentScope;
24-
2527
/// <summary>
2628
/// 初始化一个<see cref="AsyncLocalCurrentTenantAccessor"/>类型的实例
2729
/// </summary>
2830
public AsyncLocalCurrentTenantAccessor() => _currentScope = new AsyncLocal<BasicTenantInfo>();
29-
}
31+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public BasicTenantInfo(string tenantId, string code = null, string name = null)
3232
Code = code;
3333
Name = name;
3434
}
35-
}
35+
}

framework/src/Bing.MultiTenancy/Bing/MultiTenancy/CurrentTenant.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace Bing.MultiTenancy;
88
/// </summary>
99
public class CurrentTenant : ICurrentTenant, ITransientDependency
1010
{
11+
/// <summary>
12+
/// 当前租户访问器
13+
/// </summary>
14+
private readonly ICurrentTenantAccessor _currentTenantAccessor;
15+
1116
/// <summary>
1217
/// 是否可用的
1318
/// </summary>
@@ -28,11 +33,6 @@ public class CurrentTenant : ICurrentTenant, ITransientDependency
2833
/// </summary>
2934
public string Name => _currentTenantAccessor.Current?.Name;
3035

31-
/// <summary>
32-
/// 当前租户访问器
33-
/// </summary>
34-
private readonly ICurrentTenantAccessor _currentTenantAccessor;
35-
3636
/// <summary>
3737
/// 初始化一个<see cref="CurrentTenant"/>类型的实例
3838
/// </summary>
@@ -62,4 +62,4 @@ private IDisposable SetCurrent(string tenantId, string code = null, string name
6262
_currentTenantAccessor.Current = parentScope;
6363
});
6464
}
65-
}
65+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Bing.Users;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Bing.MultiTenancy;
5+
6+
/// <summary>
7+
/// 当前用户租户解析构造器
8+
/// </summary>
9+
public class CurrentUserTenantResolveContributor : TenantResolveContributorBase
10+
{
11+
/// <summary>
12+
/// 构造器名称
13+
/// </summary>
14+
public const string ContributorName = "CurrentUser";
15+
16+
/// <summary>
17+
/// 名称
18+
/// </summary>
19+
public override string Name => ContributorName;
20+
21+
/// <summary>
22+
/// 解析
23+
/// </summary>
24+
/// <param name="context">租户解析上下文</param>
25+
public override Task ResolveAsync(ITenantResolveContext context)
26+
{
27+
var currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();
28+
if (currentUser.IsAuthenticated)
29+
{
30+
context.Handled = true;
31+
context.TenantIdOrName = currentUser.TenantId;
32+
}
33+
return Task.CompletedTask;
34+
}
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Bing.DependencyInjection;
2+
3+
namespace Bing.MultiTenancy;
4+
5+
/// <summary>
6+
/// 租户解析上下文
7+
/// </summary>
8+
public interface ITenantResolveContext : IServiceProviderAccessor
9+
{
10+
/// <summary>
11+
/// 租户ID或者租户名称
12+
/// </summary>
13+
string TenantIdOrName { get; set; }
14+
15+
/// <summary>
16+
/// 是否已处理
17+
/// </summary>
18+
bool Handled { get; set; }
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Bing.MultiTenancy;
2+
3+
/// <summary>
4+
/// 租户解析构造器
5+
/// </summary>
6+
public interface ITenantResolveContributor
7+
{
8+
/// <summary>
9+
/// 名称
10+
/// </summary>
11+
string Name { get; }
12+
13+
/// <summary>
14+
/// 解析
15+
/// </summary>
16+
/// <param name="context">租户解析上下文</param>
17+
Task ResolveAsync(ITenantResolveContext context);
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Bing.MultiTenancy;
2+
3+
/// <summary>
4+
/// 租户解析结果访问器
5+
/// </summary>
6+
public interface ITenantResolveResultAccessor
7+
{
8+
/// <summary>
9+
/// 租户解析结果
10+
/// </summary>
11+
TenantResolveResult Result { get; set; }
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Bing.MultiTenancy;
2+
3+
/// <summary>
4+
/// 租户解析器
5+
/// </summary>
6+
public interface ITenantResolver
7+
{
8+
/// <summary>
9+
/// 解析当前租户
10+
/// </summary>
11+
/// <returns>
12+
/// 租户ID,租户名称,null(如果无法解析则返回空)
13+
/// </returns>
14+
Task<TenantResolveResult> ResolveTenantIdOrNameAsync();
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Bing.DependencyInjection;
2+
3+
namespace Bing.MultiTenancy;
4+
5+
/// <summary>
6+
/// 空的租户解析结果访问器
7+
/// </summary>
8+
public class NullTenantResolveResultAccessor : ITenantResolveResultAccessor, ISingletonDependency
9+
{
10+
/// <summary>
11+
/// 租户解析结果
12+
/// </summary>
13+
public TenantResolveResult Result { get => null; set { } }
14+
}

0 commit comments

Comments
 (0)