From bb07aec74920502a805d69fd2f50b5c0503745d2 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 2 May 2021 13:33:08 -0500 Subject: [PATCH] refactor(config): make IServiceConfiguration available via DI --- .../Config/ConfigurationLoaderTest.cs | 4 ++-- .../Config/ServiceConfigurationTest.cs | 4 ++-- .../Radarr/RadarrConfigurationTest.cs | 4 ++-- .../Sonarr/SonarrConfigurationTest.cs | 2 +- .../ProfileDataValueFormatter.cs | 19 ------------------- src/Trash/CompositionRoot.cs | 15 +++++++++++---- src/Trash/Config/ConfigurationLoader.cs | 4 ++-- src/Trash/Config/ConfigurationProvider.cs | 15 +++++++++++---- src/Trash/Config/IConfigurationProvider.cs | 5 ++--- src/Trash/Config/IServiceConfiguration.cs | 9 +++++++++ src/Trash/Config/ServiceConfiguration.cs | 2 +- src/Trash/Radarr/Api/RadarrApi.cs | 16 +++++----------- src/Trash/Sonarr/Api/SonarrApi.cs | 11 +++-------- 13 files changed, 51 insertions(+), 59 deletions(-) delete mode 100644 src/Trash.Tests/ValueFormatters/ProfileDataValueFormatter.cs create mode 100644 src/Trash/Config/IServiceConfiguration.cs diff --git a/src/Trash.Tests/Config/ConfigurationLoaderTest.cs b/src/Trash.Tests/Config/ConfigurationLoaderTest.cs index c8766b8c..e3919e31 100644 --- a/src/Trash.Tests/Config/ConfigurationLoaderTest.cs +++ b/src/Trash.Tests/Config/ConfigurationLoaderTest.cs @@ -30,7 +30,7 @@ namespace Trash.Tests.Config public void Load_UsingStream_CorrectParsing() { var configLoader = new ConfigurationLoader( - Substitute.For>(), + Substitute.For(), Substitute.For(), new DefaultObjectFactory()); @@ -81,7 +81,7 @@ namespace Trash.Tests.Config fs.File.OpenText(Arg.Any()) .Returns(MockYaml(1, 2), MockYaml(3)); - var provider = Substitute.For>(); + var provider = Substitute.For(); // var objectFactory = Substitute.For(); // objectFactory.Create(Arg.Any()) // .Returns(t => Substitute.For(new[] {(Type)t[0]}, Array.Empty())); diff --git a/src/Trash.Tests/Config/ServiceConfigurationTest.cs b/src/Trash.Tests/Config/ServiceConfigurationTest.cs index 70a059db..fe043dc0 100644 --- a/src/Trash.Tests/Config/ServiceConfigurationTest.cs +++ b/src/Trash.Tests/Config/ServiceConfigurationTest.cs @@ -35,7 +35,7 @@ test_service: - api_key: b "; var loader = new ConfigurationLoader( - Substitute.For>(), + Substitute.For(), Substitute.For(), new DefaultObjectFactory()); @@ -53,7 +53,7 @@ test_service: - base_url: a "; var loader = new ConfigurationLoader( - Substitute.For>(), + Substitute.For(), Substitute.For(), new DefaultObjectFactory()); diff --git a/src/Trash.Tests/Radarr/RadarrConfigurationTest.cs b/src/Trash.Tests/Radarr/RadarrConfigurationTest.cs index e071eb86..cd42ef88 100644 --- a/src/Trash.Tests/Radarr/RadarrConfigurationTest.cs +++ b/src/Trash.Tests/Radarr/RadarrConfigurationTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.IO.Abstractions; using FluentAssertions; @@ -26,7 +26,7 @@ radarr: preferred_ratio: 0.5 "; var loader = new ConfigurationLoader( - Substitute.For>(), + Substitute.For(), Substitute.For(), new DefaultObjectFactory()); diff --git a/src/Trash.Tests/Sonarr/SonarrConfigurationTest.cs b/src/Trash.Tests/Sonarr/SonarrConfigurationTest.cs index af86a8db..95ada17c 100644 --- a/src/Trash.Tests/Sonarr/SonarrConfigurationTest.cs +++ b/src/Trash.Tests/Sonarr/SonarrConfigurationTest.cs @@ -26,7 +26,7 @@ sonarr: - strict_negative_scores: true "; var loader = new ConfigurationLoader( - Substitute.For>(), + Substitute.For(), Substitute.For(), new DefaultObjectFactory()); diff --git a/src/Trash.Tests/ValueFormatters/ProfileDataValueFormatter.cs b/src/Trash.Tests/ValueFormatters/ProfileDataValueFormatter.cs deleted file mode 100644 index 4d2dfa4d..00000000 --- a/src/Trash.Tests/ValueFormatters/ProfileDataValueFormatter.cs +++ /dev/null @@ -1,19 +0,0 @@ -using FluentAssertions.Formatting; -using Trash.Sonarr.ReleaseProfile; - -namespace Trash.Tests.ValueFormatters -{ - public class ProfileDataValueFormatter : IValueFormatter - { - public bool CanHandle(object value) - { - return value is ProfileData; - } - - public string Format(object value, FormattingContext context, FormatChild formatChild) - { - var profileData = (ProfileData) value; - return $"{profileData.Ignored}"; - } - } -} diff --git a/src/Trash/CompositionRoot.cs b/src/Trash/CompositionRoot.cs index db45406f..0a7dcf71 100644 --- a/src/Trash/CompositionRoot.cs +++ b/src/Trash/CompositionRoot.cs @@ -56,11 +56,18 @@ namespace Trash private static void ConfigurationRegistrations(ContainerBuilder builder) { - builder.RegisterType().As(); - builder.RegisterGeneric(typeof(ConfigurationLoader<>)).As(typeof(IConfigurationLoader<>)); - builder.RegisterGeneric(typeof(ConfigurationProvider<>)) - .As(typeof(IConfigurationProvider<>)) + builder.RegisterType() + .As(); + + builder.RegisterGeneric(typeof(ConfigurationLoader<>)) + .As(typeof(IConfigurationLoader<>)); + + builder.RegisterType() + .As() .SingleInstance(); + + builder.Register(c => c.Resolve().ActiveConfiguration) + .As(); } private static void CommandRegistrations(ContainerBuilder builder) diff --git a/src/Trash/Config/ConfigurationLoader.cs b/src/Trash/Config/ConfigurationLoader.cs index 4c08be22..2685abea 100644 --- a/src/Trash/Config/ConfigurationLoader.cs +++ b/src/Trash/Config/ConfigurationLoader.cs @@ -13,11 +13,11 @@ namespace Trash.Config public class ConfigurationLoader : IConfigurationLoader where T : ServiceConfiguration { - private readonly IConfigurationProvider _configProvider; + private readonly IConfigurationProvider _configProvider; private readonly IDeserializer _deserializer; private readonly IFileSystem _fileSystem; - public ConfigurationLoader(IConfigurationProvider configProvider, IFileSystem fileSystem, + public ConfigurationLoader(IConfigurationProvider configProvider, IFileSystem fileSystem, IObjectFactory objectFactory) { _configProvider = configProvider; diff --git a/src/Trash/Config/ConfigurationProvider.cs b/src/Trash/Config/ConfigurationProvider.cs index 85ed9586..99f07e9e 100644 --- a/src/Trash/Config/ConfigurationProvider.cs +++ b/src/Trash/Config/ConfigurationProvider.cs @@ -1,8 +1,15 @@ -namespace Trash.Config +using System; + +namespace Trash.Config { - internal class ConfigurationProvider : IConfigurationProvider - where T : ServiceConfiguration + internal class ConfigurationProvider : IConfigurationProvider { - public T? ActiveConfiguration { get; set; } + private IServiceConfiguration? _activeConfiguration; + + public IServiceConfiguration ActiveConfiguration + { + get => _activeConfiguration ?? throw new NullReferenceException("Active configuration has not been set"); + set => _activeConfiguration = value; + } } } diff --git a/src/Trash/Config/IConfigurationProvider.cs b/src/Trash/Config/IConfigurationProvider.cs index d48b38c0..94cf6181 100644 --- a/src/Trash/Config/IConfigurationProvider.cs +++ b/src/Trash/Config/IConfigurationProvider.cs @@ -1,8 +1,7 @@ namespace Trash.Config { - public interface IConfigurationProvider - where T : ServiceConfiguration + public interface IConfigurationProvider { - T? ActiveConfiguration { get; set; } + IServiceConfiguration ActiveConfiguration { get; set; } } } diff --git a/src/Trash/Config/IServiceConfiguration.cs b/src/Trash/Config/IServiceConfiguration.cs new file mode 100644 index 00000000..2df53058 --- /dev/null +++ b/src/Trash/Config/IServiceConfiguration.cs @@ -0,0 +1,9 @@ +namespace Trash.Config +{ + public interface IServiceConfiguration + { + string BaseUrl { get; init; } + string ApiKey { get; init; } + string BuildUrl(); + } +} diff --git a/src/Trash/Config/ServiceConfiguration.cs b/src/Trash/Config/ServiceConfiguration.cs index 6cd3d0a3..cce47bf1 100644 --- a/src/Trash/Config/ServiceConfiguration.cs +++ b/src/Trash/Config/ServiceConfiguration.cs @@ -2,7 +2,7 @@ namespace Trash.Config { - public abstract class ServiceConfiguration + public abstract class ServiceConfiguration : IServiceConfiguration { [Required(ErrorMessage = "Property 'base_url' is required")] public string BaseUrl { get; init; } = ""; diff --git a/src/Trash/Radarr/Api/RadarrApi.cs b/src/Trash/Radarr/Api/RadarrApi.cs index 272aa886..3b2ccfbe 100644 --- a/src/Trash/Radarr/Api/RadarrApi.cs +++ b/src/Trash/Radarr/Api/RadarrApi.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using Flurl; using Flurl.Http; @@ -10,11 +9,11 @@ namespace Trash.Radarr.Api { public class RadarrApi : IRadarrApi { - private readonly IConfigurationProvider _config; + private readonly IServiceConfiguration _serviceConfig; - public RadarrApi(IConfigurationProvider config) + public RadarrApi(IServiceConfiguration serviceConfig) { - _config = config; + _serviceConfig = serviceConfig; } public async Task> GetQualityDefinition() @@ -35,12 +34,7 @@ namespace Trash.Radarr.Api private string BaseUrl() { - if (_config.ActiveConfiguration == null) - { - throw new InvalidOperationException("No active configuration available for API method"); - } - - return _config.ActiveConfiguration.BuildUrl(); + return _serviceConfig.BuildUrl(); } } } diff --git a/src/Trash/Sonarr/Api/SonarrApi.cs b/src/Trash/Sonarr/Api/SonarrApi.cs index c24de268..17e45769 100644 --- a/src/Trash/Sonarr/Api/SonarrApi.cs +++ b/src/Trash/Sonarr/Api/SonarrApi.cs @@ -10,9 +10,9 @@ namespace Trash.Sonarr.Api { public class SonarrApi : ISonarrApi { - private readonly IConfigurationProvider _config; + private readonly IServiceConfiguration _config; - public SonarrApi(IConfigurationProvider config) + public SonarrApi(IServiceConfiguration config) { _config = config; } @@ -80,12 +80,7 @@ namespace Trash.Sonarr.Api private string BaseUrl() { - if (_config.ActiveConfiguration == null) - { - throw new InvalidOperationException("No active configuration available for API method"); - } - - return _config.ActiveConfiguration.BuildUrl(); + return _config.BuildUrl(); } } }