Adds option pattern for smart config
This commit is contained in:
parent
6839cd82e2
commit
f4f2fc47b1
6 changed files with 130 additions and 87 deletions
|
|
@ -1,10 +1,4 @@
|
|||
using Core.Configurations.JsonConfigProvider;
|
||||
using Core.Configurations.SmartConfigProvider;
|
||||
using Core.Exceptions;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Core.Configurations.SmartConfig
|
||||
namespace Core.Configurations.SmartConfig
|
||||
{
|
||||
public static class SmartConfigExtension
|
||||
{
|
||||
|
|
@ -24,78 +18,7 @@ namespace Core.Configurations.SmartConfig
|
|||
var options = new SmartConfigOptions();
|
||||
setupAction(options);
|
||||
|
||||
return builder.AddProvider(new SmartConfigProvider(options.GetRepository()));
|
||||
}
|
||||
}
|
||||
public class SmartConfigOptions
|
||||
{
|
||||
private SmartConfiguration.IConfigurationRepository _repository;
|
||||
|
||||
public SmartConfigOptions UseRepository(SmartConfiguration.IConfigurationRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal SmartConfiguration.IConfigurationRepository GetRepository() => _repository;
|
||||
}
|
||||
public class SmartConfigProvider : IConfigurationProvider
|
||||
{
|
||||
string _configKey;
|
||||
string _connectionString;
|
||||
string _path;
|
||||
IConfigurationBuilder _builder;
|
||||
|
||||
Newtonsoft.Json.Linq.JObject _configuration;
|
||||
|
||||
|
||||
public SmartConfigProvider() { }
|
||||
|
||||
public SmartConfigProvider(SmartConfiguration.IConfigurationRepository configurationProvider)
|
||||
{ }
|
||||
public SmartConfigProvider(IConfigurationBuilder builder, string configKey, string configurationFilePath)
|
||||
{
|
||||
_builder = builder;
|
||||
_configKey = configKey;
|
||||
_path = configurationFilePath;
|
||||
SetConnectionString();
|
||||
}
|
||||
|
||||
void SetConnectionString()
|
||||
{
|
||||
var carrier = _builder.ConfigurationProviders.OfType<IHasConfigurationFilePath>().SingleOrDefault();
|
||||
|
||||
if (carrier?.ConfigurationFilePath is null && _path is null)
|
||||
throw new ConfigurationException($"Expected a previous added ConfigurationProvider with IHasConfigurationFilePath or a configurationFilePath where to find the appsettingsfile");
|
||||
|
||||
_path ??= carrier.ConfigurationFilePath;
|
||||
|
||||
if (!File.Exists(_path))
|
||||
throw new ConfigurationException($"File not found, configurationFilePath: {_path}");
|
||||
|
||||
|
||||
using (StreamReader file = File.OpenText(_path))
|
||||
using (JsonTextReader reader = new JsonTextReader(file))
|
||||
{
|
||||
var jsonConfiguration = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.ReadFrom(reader);
|
||||
|
||||
_connectionString = jsonConfiguration.SelectToken($"ConnectionStrings.{_configKey}")?.ToString();
|
||||
}
|
||||
}
|
||||
public void Build()
|
||||
{
|
||||
var repository = new ConfigurationRepository(_connectionString);
|
||||
var configs = repository.GetActiveConfigurations();
|
||||
|
||||
var pairs = configs.Select(x => new KeyValuePair<string, JToken>(x.Key, JToken.Parse(x.Value.ToString())));
|
||||
|
||||
_configuration = Common.KeyValueToJson.Convert(pairs);
|
||||
|
||||
}
|
||||
|
||||
public Newtonsoft.Json.Linq.JObject Configuration()
|
||||
{
|
||||
return _configuration;
|
||||
return builder.AddProvider(new SmartConfigProvider(builder, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue