JsonSerializer.Deserialize for AppSettings

This commit is contained in:
2024-05-21 16:23:45 -07:00
parent cd88e340a3
commit b9ed5ee159
4 changed files with 77 additions and 29 deletions

View File

@ -1,9 +1,13 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using View_by_Distance.Shared.Models;
namespace View_by_Distance.Rename.Models;
public record AppSettings(string Company,
public record AppSettings(MetadataConfiguration MetadataConfiguration,
RenameConfiguration RenameConfiguration,
ResultConfiguration ResultConfiguration,
string Company,
int MaxDegreeOfParallelism,
bool RequireRootDirectoryExists)
{

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using System.Text.Json;
using System.Text.Json.Serialization;
using View_by_Distance.Shared.Models;
namespace View_by_Distance.Rename.Models.Binder;
@ -8,6 +9,9 @@ public class AppSettings
{
public string? Company { get; set; }
public string[]? ConfigurationDirectoryNames { get; set; }
public string? ConfigurationFileName { get; set; }
public int? ConfigurationSpecialFolder { get; set; }
public int? MaxDegreeOfParallelism { get; set; }
public bool? RequireRootDirectoryExists { get; set; }
@ -38,7 +42,10 @@ public class AppSettings
{
}
private static Models.AppSettings Get(AppSettings? appSettings)
private static Models.AppSettings Get(AppSettings? appSettings,
MetadataConfiguration metadataConfiguration,
Models.RenameConfiguration renameConfiguration,
ResultConfiguration resultConfiguration)
{
Models.AppSettings result;
if (appSettings is null) throw new NullReferenceException(nameof(appSettings));
@ -46,12 +53,46 @@ public class AppSettings
if (appSettings.MaxDegreeOfParallelism is null) throw new NullReferenceException(nameof(appSettings.MaxDegreeOfParallelism));
if (appSettings.RequireRootDirectoryExists is null) throw new NullReferenceException(nameof(appSettings.RequireRootDirectoryExists));
Verify(appSettings);
result = new(appSettings.Company,
result = new(metadataConfiguration,
renameConfiguration,
resultConfiguration,
appSettings.Company,
appSettings.MaxDegreeOfParallelism.Value,
appSettings.RequireRootDirectoryExists.Value);
return result;
}
private static Models.AppSettings Get(AppSettings? appSettings)
{
Models.AppSettings? results;
string? json;
if (appSettings is null || appSettings.ConfigurationFileName is null)
throw new NotSupportedException($"{nameof(appSettings.ConfigurationFileName)} must be set!");
string jsonFile = Path.Combine(AppContext.BaseDirectory, appSettings.ConfigurationFileName);
if (File.Exists(jsonFile))
json = File.ReadAllText(jsonFile);
else
{
json = null;
string applicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
List<string> collection = [applicationData];
if (appSettings?.ConfigurationDirectoryNames is not null)
collection.AddRange(appSettings.ConfigurationDirectoryNames);
if (appSettings?.ConfigurationFileName is not null)
collection.Add(appSettings.ConfigurationFileName);
jsonFile = Path.Combine(collection.ToArray());
}
if (string.IsNullOrEmpty(json) && File.Exists(jsonFile))
json = File.ReadAllText(jsonFile);
results = (string.IsNullOrEmpty(json) ? null : results = JsonSerializer.Deserialize(json, AppSettingsSourceGenerationContext.Default.AppSettings)) ??
throw new NullReferenceException(nameof(Models.AppSettings));
results = Get(appSettings,
results.MetadataConfiguration,
results.RenameConfiguration,
results.ResultConfiguration);
return results;
}
public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
{
Models.AppSettings result;