Skip to content

Commit

Permalink
TenantConfiguration switch from tenant list to tenant map (#221)
Browse files Browse the repository at this point in the history
* TenantConfiguration switch from tenant list to tenant map

* Fix test
  • Loading branch information
fraliv13 authored Apr 20, 2022
1 parent 56a214d commit 3304d62
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
using NBB.MultiTenancy.Abstractions.Context;
using NBB.MultiTenancy.Abstractions.Options;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace NBB.MultiTenancy.Abstractions.Configuration;

Expand All @@ -21,7 +19,6 @@ public class TenantConfiguration : ITenantConfiguration
private readonly IConfiguration _globalConfiguration;
private readonly IOptions<TenancyHostingOptions> _tenancyHostingOptions;
private readonly ITenantContextAccessor _tenantContextAccessor;
private ConcurrentDictionary<Guid, string> _tenantMap;

public string this[string key] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

Expand All @@ -43,25 +40,6 @@ public TenantConfiguration(IConfiguration configuration, IOptions<TenancyHosting
_globalConfiguration = configuration;
_tenancyHostingOptions = tenancyHostingOptions;
_tenantContextAccessor = tenantContextAccessor;

if (tenancyHostingOptions.Value.TenancyType == TenancyType.MultiTenant)
{
LoadTenantsMap();
}
}

private void LoadTenantsMap()
{
var newMap = new ConcurrentDictionary<Guid, string>();
var tenants = _tenancyConfigurationSection.GetSection("Tenants").GetChildren().ToList();

for (int i = 0; i < tenants.Count; i++)
{
var tid = tenants[i].GetValue<Guid>("TenantId");
newMap.TryAdd(tid, $"Tenants:{i}");
}

_tenantMap = newMap;
}

private IConfiguration GetTenantConfiguration()
Expand All @@ -72,12 +50,13 @@ private IConfiguration GetTenantConfiguration()
}

var defaultSection = _tenancyConfigurationSection.GetSection("Defaults");
var tenantId = _tenantContextAccessor.TenantContext.GetTenantId();
if(_tenantMap.TryGetValue(tenantId, out var tenentSectionPath))
{
var tenantSection = _tenancyConfigurationSection.GetSection(tenentSectionPath);
var mergedSection = new MergedConfigurationSection(tenantSection, defaultSection);
return mergedSection;
var tenantCode = _tenantContextAccessor.TenantContext.GetTenantCode();

var tenantSection = _tenancyConfigurationSection.GetSection($"Tenants:{tenantCode}");
if (tenantSection.Exists())
{
var mergedSection = new MergedConfigurationSection(tenantSection, defaultSection);
return mergedSection;
}

return defaultSection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class TenantContextExtensions
{
public static Guid GetTenantId(this TenantContext tenantContext) => tenantContext.Tenant?.TenantId ?? throw new TenantNotFoundException();
public static Guid? TryGetTenantId(this TenantContext tenantContext) => tenantContext.Tenant?.TenantId;
public static string GetTenantCode(this TenantContext tenantContext) => tenantContext.Tenant?.Code ?? throw new TenantNotFoundException();

public static TenantContextFlow ChangeTenantContext(this ITenantContextAccessor tenantContextAccessor, Tenant tenant)
=> tenantContextAccessor.ChangeTenantContext(new TenantContext(tenant));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ public void ShouldBindOptionsFromMultiTenantConfiguration()
var myConfiguration = new Dictionary<string, string>
{
{"MultiTenancy:TenancyType","MultiTenant" },
{"MultiTenancy:Tenants:0:TenantId", myTenantId1},
{"MultiTenancy:Tenants:0:ConnectionStrings:EventStore", myConnectionString1},
{"MultiTenancy:Tenants:1:TenantId", myTenantId2},
{"MultiTenancy:Tenants:1:ConnectionStrings:EventStore", myConnectionString2},
{"MultiTenancy:Tenants:BCR:TenantId", myTenantId1},
{"MultiTenancy:Tenants:BCR:Code", "BCR"},
{"MultiTenancy:Tenants:BCR:ConnectionStrings:EventStore", myConnectionString1},
{"MultiTenancy:Tenants:MBFS:TenantId", myTenantId2},
{"MultiTenancy:Tenants:MBFS:Code", "MBFS"},
{"MultiTenancy:Tenants:MBFS:ConnectionStrings:EventStore", myConnectionString2},
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(myConfiguration)
Expand All @@ -117,22 +119,22 @@ public void ShouldBindOptionsFromMultiTenantConfiguration()

//Act
using var sp = services.BuildServiceProvider(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
IServiceScope CreateTenantScope(string tenantId)
IServiceScope CreateTenantScope(string tenantId, string tenantCode)
{
var scope = sp.CreateScope();

var tca = scope.ServiceProvider.GetRequiredService<ITenantContextAccessor>().TenantContext =
new TenantContext(new Tenant { TenantId = Guid.Parse(tenantId) });
new TenantContext(new Tenant { TenantId = Guid.Parse(tenantId), Code = tenantCode });
return scope;
}


//Assert
using var scope1 = CreateTenantScope(myTenantId1);
using var scope1 = CreateTenantScope(myTenantId1, "BCR");
var opts1 = scope1.ServiceProvider.GetRequiredService<IOptionsSnapshot<EventStoreAdoNetOptions>>();
opts1.Value.ConnectionString.Should().Be(myConnectionString1);

using var scope2 = CreateTenantScope(myTenantId2);
using var scope2 = CreateTenantScope(myTenantId2, "MBFS");
var opts2 = scope2.ServiceProvider.GetRequiredService<IOptionsSnapshot<EventStoreAdoNetOptions>>();
opts2.Value.ConnectionString.Should().Be(myConnectionString2);

Expand Down
Loading

0 comments on commit 3304d62

Please sign in to comment.