Removed AppSettings layer
This commit is contained in:
parent
09c37aed14
commit
c37771da61
@ -1,3 +1,4 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
@ -8,7 +9,7 @@ public record AppSettings(string Company,
|
|||||||
string[] ExcludeDirectoryNames,
|
string[] ExcludeDirectoryNames,
|
||||||
string[] ExcludeSchemes,
|
string[] ExcludeSchemes,
|
||||||
string PersonBirthdayFormat,
|
string PersonBirthdayFormat,
|
||||||
char[] PersonTitleFilters,
|
string PersonTitleFilters,
|
||||||
string[] ValidImageFormatExtensions,
|
string[] ValidImageFormatExtensions,
|
||||||
string WorkingDirectoryName)
|
string WorkingDirectoryName)
|
||||||
{
|
{
|
||||||
@ -19,6 +20,35 @@ public record AppSettings(string Company,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings)
|
||||||
|
{
|
||||||
|
if (appSettings?.Company is null)
|
||||||
|
{
|
||||||
|
List<string> paths = [];
|
||||||
|
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
|
||||||
|
{
|
||||||
|
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
|
||||||
|
continue;
|
||||||
|
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
|
||||||
|
continue;
|
||||||
|
paths.Add(physicalFileProvider.Root);
|
||||||
|
}
|
||||||
|
throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AppSettings Get(IConfigurationRoot configurationRoot)
|
||||||
|
{
|
||||||
|
AppSettings? result;
|
||||||
|
#pragma warning disable IL3050, IL2026
|
||||||
|
result = configurationRoot.Get<AppSettings>();
|
||||||
|
#pragma warning restore IL3050, IL2026
|
||||||
|
PreVerify(configurationRoot, result);
|
||||||
|
if (result is null)
|
||||||
|
throw new Exception("Not set!");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
[*.cs]
|
|
||||||
csharp_preserve_single_line_statements = true
|
|
@ -1,81 +0,0 @@
|
|||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace File_Folder_Helper.Models.Binder;
|
|
||||||
|
|
||||||
public class AppSettings
|
|
||||||
{
|
|
||||||
|
|
||||||
public string? Company { get; set; }
|
|
||||||
public string? DefaultNoteType { get; set; }
|
|
||||||
public string[]? ExcludeDirectoryNames { get; set; }
|
|
||||||
public string[]? ExcludeSchemes { get; set; }
|
|
||||||
public string? PersonBirthdayFormat { get; set; }
|
|
||||||
public string? PersonTitleFilters { get; set; }
|
|
||||||
public string[]? ValidImageFormatExtensions { get; set; }
|
|
||||||
public string? WorkingDirectoryName { get; set; }
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
string result = JsonSerializer.Serialize(this, BinderAppSettingsSourceGenerationContext.Default.AppSettings);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings)
|
|
||||||
{
|
|
||||||
if (appSettings?.Company is null)
|
|
||||||
{
|
|
||||||
List<string> paths = [];
|
|
||||||
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers)
|
|
||||||
{
|
|
||||||
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider)
|
|
||||||
continue;
|
|
||||||
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider)
|
|
||||||
continue;
|
|
||||||
paths.Add(physicalFileProvider.Root);
|
|
||||||
}
|
|
||||||
throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Models.AppSettings Get(AppSettings? appSettings)
|
|
||||||
{
|
|
||||||
Models.AppSettings result;
|
|
||||||
if (appSettings?.Company is null) throw new NullReferenceException(nameof(appSettings.Company));
|
|
||||||
if (appSettings?.DefaultNoteType is null) throw new NullReferenceException(nameof(appSettings.DefaultNoteType));
|
|
||||||
if (appSettings?.ExcludeDirectoryNames is null) throw new NullReferenceException(nameof(appSettings.ExcludeDirectoryNames));
|
|
||||||
if (appSettings?.ExcludeSchemes is null) throw new NullReferenceException(nameof(appSettings.ExcludeSchemes));
|
|
||||||
if (appSettings?.PersonBirthdayFormat is null) throw new NullReferenceException(nameof(appSettings.PersonBirthdayFormat));
|
|
||||||
if (appSettings?.PersonTitleFilters is null) throw new NullReferenceException(nameof(appSettings.PersonTitleFilters));
|
|
||||||
if (appSettings?.ValidImageFormatExtensions is null) throw new NullReferenceException(nameof(appSettings.ValidImageFormatExtensions));
|
|
||||||
if (appSettings?.WorkingDirectoryName is null) throw new NullReferenceException(nameof(appSettings.WorkingDirectoryName));
|
|
||||||
result = new(appSettings.Company,
|
|
||||||
appSettings.DefaultNoteType,
|
|
||||||
appSettings.ExcludeDirectoryNames,
|
|
||||||
appSettings.ExcludeSchemes,
|
|
||||||
appSettings.PersonBirthdayFormat,
|
|
||||||
appSettings.PersonTitleFilters.ToArray(),
|
|
||||||
appSettings.ValidImageFormatExtensions,
|
|
||||||
appSettings.WorkingDirectoryName);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Models.AppSettings Get(IConfigurationRoot configurationRoot)
|
|
||||||
{
|
|
||||||
Models.AppSettings result;
|
|
||||||
#pragma warning disable IL3050, IL2026
|
|
||||||
AppSettings? appSettings = configurationRoot.Get<AppSettings>();
|
|
||||||
#pragma warning restore IL3050, IL2026
|
|
||||||
PreVerify(configurationRoot, appSettings);
|
|
||||||
result = Get(appSettings);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
||||||
[JsonSerializable(typeof(AppSettings))]
|
|
||||||
internal partial class BinderAppSettingsSourceGenerationContext : JsonSerializerContext
|
|
||||||
{
|
|
||||||
}
|
|
@ -17,7 +17,7 @@ internal class Program
|
|||||||
_ = builder.Configuration.AddEnvironmentVariables();
|
_ = builder.Configuration.AddEnvironmentVariables();
|
||||||
_ = builder.Configuration.AddUserSecrets<Program>();
|
_ = builder.Configuration.AddUserSecrets<Program>();
|
||||||
_ = builder.Services.AddSingleton(args.ToList());
|
_ = builder.Services.AddSingleton(args.ToList());
|
||||||
AppSettings appSettings = Models.Binder.AppSettings.Get(builder.Configuration);
|
AppSettings appSettings = AppSettings.Get(builder.Configuration);
|
||||||
_ = builder.Services.AddSingleton(appSettings);
|
_ = builder.Services.AddSingleton(appSettings);
|
||||||
_ = builder.Services.AddHostedService<Worker>();
|
_ = builder.Services.AddHostedService<Worker>();
|
||||||
using IHost host = builder.Build();
|
using IHost host = builder.Build();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user