refactor(config): make IServiceConfiguration available via DI

recyclarr
Robert Dailey 4 years ago
parent b3ab16b75b
commit bb07aec749

@ -30,7 +30,7 @@ namespace Trash.Tests.Config
public void Load_UsingStream_CorrectParsing()
{
var configLoader = new ConfigurationLoader<SonarrConfiguration>(
Substitute.For<IConfigurationProvider<SonarrConfiguration>>(),
Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());
@ -81,7 +81,7 @@ namespace Trash.Tests.Config
fs.File.OpenText(Arg.Any<string>())
.Returns(MockYaml(1, 2), MockYaml(3));
var provider = Substitute.For<IConfigurationProvider<SonarrConfiguration>>();
var provider = Substitute.For<IConfigurationProvider>();
// var objectFactory = Substitute.For<IObjectFactory>();
// objectFactory.Create(Arg.Any<Type>())
// .Returns(t => Substitute.For(new[] {(Type)t[0]}, Array.Empty<object>()));

@ -35,7 +35,7 @@ test_service:
- api_key: b
";
var loader = new ConfigurationLoader<TestServiceConfiguration>(
Substitute.For<IConfigurationProvider<TestServiceConfiguration>>(),
Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());
@ -53,7 +53,7 @@ test_service:
- base_url: a
";
var loader = new ConfigurationLoader<TestServiceConfiguration>(
Substitute.For<IConfigurationProvider<TestServiceConfiguration>>(),
Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());

@ -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<RadarrConfiguration>(
Substitute.For<IConfigurationProvider<RadarrConfiguration>>(),
Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());

@ -26,7 +26,7 @@ sonarr:
- strict_negative_scores: true
";
var loader = new ConfigurationLoader<SonarrConfiguration>(
Substitute.For<IConfigurationProvider<SonarrConfiguration>>(),
Substitute.For<IConfigurationProvider>(),
Substitute.For<IFileSystem>(),
new DefaultObjectFactory());

@ -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}";
}
}
}

@ -56,11 +56,18 @@ namespace Trash
private static void ConfigurationRegistrations(ContainerBuilder builder)
{
builder.RegisterType<ObjectFactory>().As<IObjectFactory>();
builder.RegisterGeneric(typeof(ConfigurationLoader<>)).As(typeof(IConfigurationLoader<>));
builder.RegisterGeneric(typeof(ConfigurationProvider<>))
.As(typeof(IConfigurationProvider<>))
builder.RegisterType<ObjectFactory>()
.As<IObjectFactory>();
builder.RegisterGeneric(typeof(ConfigurationLoader<>))
.As(typeof(IConfigurationLoader<>));
builder.RegisterType<ConfigurationProvider>()
.As<IConfigurationProvider>()
.SingleInstance();
builder.Register(c => c.Resolve<IConfigurationProvider>().ActiveConfiguration)
.As<IServiceConfiguration>();
}
private static void CommandRegistrations(ContainerBuilder builder)

@ -13,11 +13,11 @@ namespace Trash.Config
public class ConfigurationLoader<T> : IConfigurationLoader<T>
where T : ServiceConfiguration
{
private readonly IConfigurationProvider<T> _configProvider;
private readonly IConfigurationProvider _configProvider;
private readonly IDeserializer _deserializer;
private readonly IFileSystem _fileSystem;
public ConfigurationLoader(IConfigurationProvider<T> configProvider, IFileSystem fileSystem,
public ConfigurationLoader(IConfigurationProvider configProvider, IFileSystem fileSystem,
IObjectFactory objectFactory)
{
_configProvider = configProvider;

@ -1,8 +1,15 @@
namespace Trash.Config
using System;
namespace Trash.Config
{
internal class ConfigurationProvider<T> : IConfigurationProvider<T>
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;
}
}
}

@ -1,8 +1,7 @@
namespace Trash.Config
{
public interface IConfigurationProvider<T>
where T : ServiceConfiguration
public interface IConfigurationProvider
{
T? ActiveConfiguration { get; set; }
IServiceConfiguration ActiveConfiguration { get; set; }
}
}

@ -0,0 +1,9 @@
namespace Trash.Config
{
public interface IServiceConfiguration
{
string BaseUrl { get; init; }
string ApiKey { get; init; }
string BuildUrl();
}
}

@ -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; } = "";

@ -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<RadarrConfiguration> _config;
private readonly IServiceConfiguration _serviceConfig;
public RadarrApi(IConfigurationProvider<RadarrConfiguration> config)
public RadarrApi(IServiceConfiguration serviceConfig)
{
_config = config;
_serviceConfig = serviceConfig;
}
public async Task<List<RadarrQualityDefinitionItem>> 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();
}
}
}

@ -10,9 +10,9 @@ namespace Trash.Sonarr.Api
{
public class SonarrApi : ISonarrApi
{
private readonly IConfigurationProvider<SonarrConfiguration> _config;
private readonly IServiceConfiguration _config;
public SonarrApi(IConfigurationProvider<SonarrConfiguration> 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();
}
}
}

Loading…
Cancel
Save