From c37771da6151b424bdc94a27d055deb3973738e7 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Wed, 11 Dec 2024 10:14:45 -0700 Subject: [PATCH] Removed AppSettings layer --- Models/AppSettings.cs | 32 +++++++++++++- Models/Binder/.editorconfig | 2 - Models/Binder/AppSettings.cs | 81 ------------------------------------ Program.cs | 2 +- 4 files changed, 32 insertions(+), 85 deletions(-) delete mode 100644 Models/Binder/.editorconfig delete mode 100644 Models/Binder/AppSettings.cs diff --git a/Models/AppSettings.cs b/Models/AppSettings.cs index 422b977..42361f9 100644 --- a/Models/AppSettings.cs +++ b/Models/AppSettings.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Configuration; using System.Text.Json; using System.Text.Json.Serialization; @@ -8,7 +9,7 @@ public record AppSettings(string Company, string[] ExcludeDirectoryNames, string[] ExcludeSchemes, string PersonBirthdayFormat, - char[] PersonTitleFilters, + string PersonTitleFilters, string[] ValidImageFormatExtensions, string WorkingDirectoryName) { @@ -19,6 +20,35 @@ public record AppSettings(string Company, return result; } + private static void PreVerify(IConfigurationRoot configurationRoot, AppSettings? appSettings) + { + if (appSettings?.Company is null) + { + List 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(); +#pragma warning restore IL3050, IL2026 + PreVerify(configurationRoot, result); + if (result is null) + throw new Exception("Not set!"); + return result; + } + } [JsonSourceGenerationOptions(WriteIndented = true)] diff --git a/Models/Binder/.editorconfig b/Models/Binder/.editorconfig deleted file mode 100644 index 1c444cd..0000000 --- a/Models/Binder/.editorconfig +++ /dev/null @@ -1,2 +0,0 @@ -[*.cs] -csharp_preserve_single_line_statements = true \ No newline at end of file diff --git a/Models/Binder/AppSettings.cs b/Models/Binder/AppSettings.cs deleted file mode 100644 index c69bde5..0000000 --- a/Models/Binder/AppSettings.cs +++ /dev/null @@ -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 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(); -#pragma warning restore IL3050, IL2026 - PreVerify(configurationRoot, appSettings); - result = Get(appSettings); - return result; - } - -} - -[JsonSourceGenerationOptions(WriteIndented = true)] -[JsonSerializable(typeof(AppSettings))] -internal partial class BinderAppSettingsSourceGenerationContext : JsonSerializerContext -{ -} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 67b2c9e..2afbc66 100644 --- a/Program.cs +++ b/Program.cs @@ -17,7 +17,7 @@ internal class Program _ = builder.Configuration.AddEnvironmentVariables(); _ = builder.Configuration.AddUserSecrets(); _ = 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.AddHostedService(); using IHost host = builder.Build();