2018-03-18 21:25:11 -05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Newtonsoft.Json;
|
2019-01-03 14:39:22 -06:00
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2018-04-08 01:44:42 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-08 01:44:42 -05:00
|
|
|
|
namespace SharedLibraryCore.Configuration
|
2018-03-18 21:25:11 -05:00
|
|
|
|
{
|
|
|
|
|
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
|
|
|
|
|
{
|
2018-09-06 13:25:58 -05:00
|
|
|
|
readonly string Filename;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
IConfigurationRoot ConfigurationRoot { get; set; }
|
|
|
|
|
T _configuration;
|
|
|
|
|
|
|
|
|
|
public BaseConfigurationHandler(string fn)
|
|
|
|
|
{
|
|
|
|
|
Filename = fn;
|
|
|
|
|
Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Build()
|
|
|
|
|
{
|
|
|
|
|
ConfigurationRoot = new ConfigurationBuilder()
|
2018-10-05 22:10:39 -05:00
|
|
|
|
.AddJsonFile(Path.Join(Utilities.OperatingDirectory, "Configuration", $"{Filename}.json"), true)
|
2018-03-18 21:25:11 -05: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 15:31:05 -05:00
|
|
|
|
return File.WriteAllTextAsync(Path.Join(Utilities.OperatingDirectory, "Configuration", $"{Filename}.json"), appConfigJSON);
|
2018-03-18 21:25:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-27 19:45:35 -06:00
|
|
|
|
public T Configuration() => _configuration;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
|
|
|
|
|
public void Set(T config)
|
|
|
|
|
{
|
|
|
|
|
_configuration = config;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|