-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
TenantStore.cs
143 lines (122 loc) · 5.14 KB
/
TenantStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.ObjectMapping;
namespace Volo.Abp.TenantManagement;
public class TenantStore : ITenantStore, ITransientDependency
{
protected ITenantRepository TenantRepository { get; }
protected IObjectMapper<AbpTenantManagementDomainModule> ObjectMapper { get; }
protected ICurrentTenant CurrentTenant { get; }
protected IDistributedCache<TenantConfigurationCacheItem> Cache { get; }
public TenantStore(
ITenantRepository tenantRepository,
IObjectMapper<AbpTenantManagementDomainModule> objectMapper,
ICurrentTenant currentTenant,
IDistributedCache<TenantConfigurationCacheItem> cache)
{
TenantRepository = tenantRepository;
ObjectMapper = objectMapper;
CurrentTenant = currentTenant;
Cache = cache;
}
public virtual async Task<TenantConfiguration> FindAsync(string normalizedName)
{
return (await GetCacheItemAsync(null, normalizedName)).Value;
}
public virtual async Task<TenantConfiguration> FindAsync(Guid id)
{
return (await GetCacheItemAsync(id, null)).Value;
}
public virtual async Task<IReadOnlyList<TenantConfiguration>> GetListAsync(bool includeDetails = false)
{
return ObjectMapper.Map<List<Tenant>, List<TenantConfiguration>>(
await TenantRepository.GetListAsync(includeDetails));
}
[Obsolete("Use FindAsync method.")]
public virtual TenantConfiguration Find(string normalizedName)
{
return (GetCacheItem(null, normalizedName)).Value;
}
[Obsolete("Use FindAsync method.")]
public virtual TenantConfiguration Find(Guid id)
{
return (GetCacheItem(id, null)).Value;
}
protected virtual async Task<TenantConfigurationCacheItem> GetCacheItemAsync(Guid? id, string normalizedName)
{
var cacheKey = CalculateCacheKey(id, normalizedName);
var cacheItem = await Cache.GetAsync(cacheKey, considerUow: true);
if (cacheItem?.Value != 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 (!normalizedName.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(normalizedName);
return await SetCacheAsync(cacheKey, tenant);
}
}
throw new AbpException("Both id and normalizedName can't be invalid.");
}
protected virtual async Task<TenantConfigurationCacheItem> SetCacheAsync(string cacheKey, [CanBeNull] Tenant tenant)
{
var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null;
var cacheItem = new TenantConfigurationCacheItem(tenantConfiguration);
await Cache.SetAsync(cacheKey, cacheItem, considerUow: true);
return cacheItem;
}
[Obsolete("Use GetCacheItemAsync method.")]
protected virtual TenantConfigurationCacheItem GetCacheItem(Guid? id, string normalizedName)
{
var cacheKey = CalculateCacheKey(id, normalizedName);
var cacheItem = Cache.Get(cacheKey, considerUow: true);
if (cacheItem?.Value != 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 = TenantRepository.FindById(id.Value);
return SetCache(cacheKey, tenant);
}
}
if (!normalizedName.IsNullOrWhiteSpace())
{
using (CurrentTenant.Change(null)) //TODO: No need this if we can implement to define host side (or tenant-independent) entities!
{
var tenant = TenantRepository.FindByName(normalizedName);
return SetCache(cacheKey, tenant);
}
}
throw new AbpException("Both id and normalizedName can't be invalid.");
}
[Obsolete("Use SetCacheAsync method.")]
protected virtual TenantConfigurationCacheItem SetCache(string cacheKey, [CanBeNull] Tenant tenant)
{
var tenantConfiguration = tenant != null ? ObjectMapper.Map<Tenant, TenantConfiguration>(tenant) : null;
var cacheItem = new TenantConfigurationCacheItem(tenantConfiguration);
Cache.Set(cacheKey, cacheItem, considerUow: true);
return cacheItem;
}
protected virtual string CalculateCacheKey(Guid? id, string normalizedName)
{
return TenantConfigurationCacheItem.CalculateCacheKey(id, normalizedName);
}
}