Skip to content

Commit

Permalink
Fix minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
woksin committed Jan 25, 2024
1 parent 476e4c7 commit af9523b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Woksin.Extensions.Configurations.Tenancy;
public class CannotResolveTenantConfigurationWhenTenantContextIsNotResolved : Exception
{
public CannotResolveTenantConfigurationWhenTenantContextIsNotResolved(Type tenantConfigurationType)
: base($"Cannot create tenant configuration '{tenantConfigurationType}' from root container")
: base($"Cannot create tenant configuration '{tenantConfigurationType}' when tenant context is not resolved")
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
},
"Tenancy": {
"Strict": true,
"Tenants": [
{
"Id": "id_1",
Expand Down
26 changes: 19 additions & 7 deletions Packages/DotNET/Tenancy/Source/Base/Context/TenantResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ public async Task<ITenantContext<TTenant>> Resolve(object context)
{
var config = options.CurrentValue;
foreach (var strategy in Strategies)
{
var (resolvedIdentifier, identifier) = await TryResolveIdentifier(config, strategy, context);
{
var wrappedStrategy = new SafeStrategyWrapper(strategy, loggerFactory?.CreateLogger(strategy.GetType()) ?? NullLogger.Instance);
if (!wrappedStrategy.CanResolveFromContext(context, out var cannotResolveReason))
{
LogCannotNotResolveFromContext(_logger, strategy.GetType(), cannotResolveReason);
continue;
}
LogTryingToResolve(_logger, strategy.GetType());
var (resolvedIdentifier, identifier) = await TryResolveIdentifier(config, wrappedStrategy, context);
if (resolvedIdentifier && TryGetTenantContext(config, identifier!, strategy, out var tenantContext))
{
return tenantContext;
Expand All @@ -41,10 +48,9 @@ public async Task<ITenantContext<TTenant>> Resolve(object context)
return TenantContext<TTenant>.Unresolved();
}

async Task<(bool, string?)> TryResolveIdentifier(TenancyOptions<TTenant> config, ITenantResolutionStrategy strategy, object context)
async Task<(bool, string?)> TryResolveIdentifier(TenancyOptions<TTenant> config, SafeStrategyWrapper strategy, object context)
{
var wrappedStrategy = new SafeStrategyWrapper(strategy, loggerFactory?.CreateLogger(strategy.GetType()) ?? NullLogger.Instance);
var identifier = await wrappedStrategy.Resolve(context);
var identifier = await strategy.Resolve(context);
if (!config.Ignored.Contains(identifier, StringComparer.OrdinalIgnoreCase))
{
return string.IsNullOrWhiteSpace(identifier)
Expand Down Expand Up @@ -86,7 +92,7 @@ async Task<ITenantContext> IResolveTenant.Resolve(object context)
[LoggerMessage(0, LogLevel.Debug, "Resolved tenant identifier {Identifier} is configured to be ignored")]
static partial void LogIgnoreTenant(ILogger logger, string identifier);

[LoggerMessage(1, LogLevel.Debug, "Resolved tenant identifier {Identifier} is not configured and should not be used")]
[LoggerMessage(1, LogLevel.Warning, "Resolved tenant identifier {Identifier} is not configured and should not be used")]
static partial void LogTenantNotConfigured(ILogger logger, string identifier);

[LoggerMessage(2, LogLevel.Debug, "Resolved tenant identifier {Identifier} is not configured but will be used")]
Expand All @@ -95,6 +101,12 @@ async Task<ITenantContext> IResolveTenant.Resolve(object context)
[LoggerMessage(3, LogLevel.Debug, "Resolved tenant identifier {Identifier} with name {TenantName} is configured")]
static partial void LogUsingConfiguredTenant(ILogger logger, string identifier, string? tenantName);

[LoggerMessage(4, LogLevel.Warning, "Could not resolve tenant from any strategy")]
[LoggerMessage(4, LogLevel.Debug, "Could not resolve tenant from any strategy")]
static partial void LogCouldNotResolveTenant(ILogger logger);

[LoggerMessage(5, LogLevel.Debug, "Could not resolve tenant from context using strategy {StrategyType}. {Reason}")]
static partial void LogCannotNotResolveFromContext(ILogger logger, Type strategyType, string reason);

[LoggerMessage(6, LogLevel.Debug, "Trying to resolve tenant from context using strategy {StrategyType}")]
static partial void LogTryingToResolve(ILogger logger, Type strategyType);
}

0 comments on commit af9523b

Please sign in to comment.