zeroframework/Services/DeviceCenter/ZeroFramework.DeviceCenter.Infrastructure/ConnectionStrings/TenantConnectionStringProvider.cs

32 lines
1.3 KiB
C#
Raw Permalink Normal View History

2023-12-05 09:22:48 +00:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using ZeroFramework.DeviceCenter.Domain.Aggregates.TenantAggregate;
using ZeroFramework.DeviceCenter.Infrastructure.Constants;
namespace ZeroFramework.DeviceCenter.Infrastructure.ConnectionStrings
{
public class TenantConnectionStringProvider(IOptions<TenantStoreOptions> options, ICurrentTenant currentTenant, IConfiguration configuration) : DefaultConnectionStringProvider(configuration)
{
private readonly ICurrentTenant _currentTenant = currentTenant;
private readonly TenantStoreOptions _tenantStoreOptions = options.Value;
public override Task<string> GetAsync(string? connectionStringName = null)
{
connectionStringName ??= DbConstants.DefaultConnectionStringName;
if (_currentTenant.IsAvailable)
{
var tenantConfig = _tenantStoreOptions.Tenants?.SingleOrDefault(t => t.TenantId == _currentTenant.Id);
string? connectionString = tenantConfig?.ConnectionStrings?[connectionStringName];
if (connectionString is not null)
{
return Task.FromResult<string>(connectionString);
}
}
return base.GetAsync(connectionStringName);
}
}
}