-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tenant configuration re-design (#213)
* Tenant configuration re-design * fix some tests Co-authored-by: Radu Popovici <[email protected]>
- Loading branch information
1 parent
026a25b
commit 5f6247e
Showing
11 changed files
with
291 additions
and
42 deletions.
There are no files selected for viewing
12 changes: 3 additions & 9 deletions
12
src/MultiTenancy/NBB.MultiTenancy.Abstractions/Configuration/ITenantConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,9 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace NBB.MultiTenancy.Abstractions.Configuration; | ||
public interface ITenantConfiguration | ||
public interface ITenantConfiguration : IConfiguration | ||
{ | ||
/// <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); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/MultiTenancy/NBB.MultiTenancy.Abstractions/Configuration/MergedConfigurationSection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Primitives; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace NBB.MultiTenancy.Abstractions.Configuration | ||
{ | ||
public class MergedConfigurationSection : IConfigurationSection | ||
{ | ||
private readonly IConfigurationSection _innerConfigurationSection; | ||
private readonly IConfigurationSection _defaultConfigurationSection; | ||
|
||
public MergedConfigurationSection(IConfigurationSection innerConfigurationSection, IConfigurationSection defaultConfigurationSection) | ||
{ | ||
_innerConfigurationSection = innerConfigurationSection ?? throw new ArgumentNullException(nameof(innerConfigurationSection)); | ||
_defaultConfigurationSection = defaultConfigurationSection ?? throw new ArgumentNullException(nameof(defaultConfigurationSection)); | ||
|
||
} | ||
public string this[string key] | ||
{ | ||
get => _innerConfigurationSection[key] ?? _defaultConfigurationSection[key]; | ||
set => _innerConfigurationSection[key] = value; | ||
} | ||
|
||
public string Key => _innerConfigurationSection.Key; | ||
|
||
public string Path => _innerConfigurationSection.Path; | ||
|
||
public string Value | ||
{ | ||
get => _innerConfigurationSection.Value ?? (!_innerConfigurationSection.GetChildren().Any() ? _defaultConfigurationSection.Value : null); | ||
set => _innerConfigurationSection.Value = value; | ||
} | ||
|
||
public IEnumerable<IConfigurationSection> GetChildren() | ||
{ | ||
var innerChildren = _innerConfigurationSection.GetChildren().ToDictionary(s => s.Key); | ||
if(innerChildren.Count == 0 && _innerConfigurationSection.Value is not null) | ||
{ | ||
return innerChildren.Values; | ||
} | ||
|
||
foreach (var c in _defaultConfigurationSection.GetChildren()) | ||
{ | ||
if (innerChildren.ContainsKey(c.Key)) | ||
{ | ||
innerChildren[c.Key] = new MergedConfigurationSection(innerChildren[c.Key], c); | ||
} | ||
else | ||
{ | ||
innerChildren[c.Key] = c; | ||
} | ||
} | ||
return innerChildren.Values; | ||
} | ||
|
||
public IChangeToken GetReloadToken() | ||
{ | ||
return _innerConfigurationSection.GetReloadToken(); | ||
} | ||
|
||
public IConfigurationSection GetSection(string key) | ||
{ | ||
//config.GetSection is never null | ||
var innerCfg = _innerConfigurationSection.GetSection(key); | ||
var innerCfgIsEmpty = innerCfg.Value is null && !innerCfg.GetChildren().Any(); | ||
var defaultCfg = _defaultConfigurationSection.GetSection(key); | ||
var defaultCfgIsEmpty = defaultCfg.Value is null && !defaultCfg.GetChildren().Any(); | ||
var result = (innerCfgIsEmpty, defaultCfgIsEmpty) switch | ||
{ | ||
(true, false) => defaultCfg, | ||
(_, true) => innerCfg, | ||
_ => new MergedConfigurationSection(innerCfg, defaultCfg) | ||
}; | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.