Skip to content

Commit

Permalink
refactor configuration (#205)
Browse files Browse the repository at this point in the history
change configuration source, to support both single string and complex object connectionStrings
  • Loading branch information
lghinet authored Feb 8, 2022
1 parent e7fe8ef commit c7af16c
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
namespace NBB.MultiTenancy.Abstractions.Configuration;
public interface ITenantConfiguration
{
/// <summary>
/// Extracts the value with the specified key and converts it to type T.
/// or
/// Attempts to bind the configuration instance to a new instance of type T.
/// If this configuration section has a value, that will be used.
/// Otherwise binding by matching property names against configuration keys recursively.
/// </summary>
public T GetValue<T>(string key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using NBB.MultiTenancy.Abstractions.Options;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Linq;

namespace NBB.MultiTenancy.Abstractions.Configuration;

Expand Down Expand Up @@ -63,7 +65,7 @@ public T GetValue<T>(string key)
{
if (_tenancyHostingOptions.Value.TenancyType == TenancyType.MonoTenant)
{
return _globalConfiguration.GetValue<T>(key);
return getValueOrComplexObject<T>(_globalConfiguration, key);
}

var tenantId = _tenantContextAccessor.TenantContext.GetTenantId();
Expand All @@ -72,6 +74,22 @@ public T GetValue<T>(string key)
? result
: throw new Exception($"Configuration not found for tenant {tenantId}");

return section.GetValue<T>(key, defaultSection.GetValue<T>(key));

return getValueOrComplexObject<T>(section, key, defaultSection);
}

private static T getValueOrComplexObject<T>(IConfiguration config, string key, IConfigurationSection defaultSection = null)
{
//section.GetSection is never null
if (config.GetSection(key).GetChildren().Any())
{
//complex type is present
return config.GetSection(key).Get<T>();
}

if (config.GetSection(key).Value != null)
return config.GetValue<T>(key);

return defaultSection == null ? default : getValueOrComplexObject<T>(defaultSection, key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,28 @@
using System.Text;
using System.Threading.Tasks;

namespace NBB.MultiTenancy.Abstractions.Configuration
namespace NBB.MultiTenancy.Abstractions.Configuration;

public static class TenantConfigurationExtensions
{
public static class TenantConfigurationExtensions
public static string GetConnectionString(this ITenantConfiguration config, string name)
{
public static string GetConnectionString(this ITenantConfiguration config, string name)
var splitted = config.GetValue<ConnectionStringDetails>($"ConnectionStrings:{name}");
if (splitted != null)
{
return config.GetValue<string>($"ConnectionStrings:{name}");
return
$"Server={splitted.Server};Database={splitted.Database};User Id={splitted.UserName};Password={splitted.Password};{splitted.OtherParams}";
}

return config.GetValue<string>($"ConnectionStrings:{name}");
}

private class ConnectionStringDetails
{
public string Server { get; set; }
public string Database { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string OtherParams { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsPackagesVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsPackagesVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackagesVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkPackageVersion)" />
<PackageReference Include="Moq" Version="$(MoqPackageVersion)" />
<PackageReference Include="xunit" Version="$(XunitPackageVersion)" />
Expand Down
Loading

0 comments on commit c7af16c

Please sign in to comment.