2018-03-18 22:25:11 -04:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Newtonsoft.Json;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Configuration
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
|
|
|
|
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
|
|
|
|
|
{
|
2018-09-06 14:25:58 -04:00
|
|
|
|
readonly string Filename;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
IConfigurationRoot ConfigurationRoot { get; set; }
|
|
|
|
|
T _configuration;
|
|
|
|
|
|
|
|
|
|
public BaseConfigurationHandler(string fn)
|
|
|
|
|
{
|
|
|
|
|
Filename = fn;
|
|
|
|
|
Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Build()
|
|
|
|
|
{
|
|
|
|
|
ConfigurationRoot = new ConfigurationBuilder()
|
2018-10-05 23:10:39 -04:00
|
|
|
|
.AddJsonFile(Path.Join(Utilities.OperatingDirectory, "Configuration", $"{Filename}.json"), true)
|
2018-03-18 22:25:11 -04:00
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
_configuration = ConfigurationRoot.Get<T>();
|
|
|
|
|
|
|
|
|
|
if (_configuration == null)
|
|
|
|
|
_configuration = default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task Save()
|
|
|
|
|
{
|
|
|
|
|
var appConfigJSON = JsonConvert.SerializeObject(_configuration, Formatting.Indented);
|
2018-10-06 16:31:05 -04:00
|
|
|
|
return File.WriteAllTextAsync(Path.Join(Utilities.OperatingDirectory, "Configuration", $"{Filename}.json"), appConfigJSON);
|
2018-03-18 22:25:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Configuration() => _configuration;
|
|
|
|
|
|
|
|
|
|
public void Set(T config)
|
|
|
|
|
{
|
|
|
|
|
_configuration = config;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|