simplify typed settings configuration

This commit is contained in:
Collin M. Barrett 2019-07-04 14:21:48 -05:00
parent 3ed85260c5
commit 34f0d92eda

View file

@ -20,7 +20,7 @@ public static void RegisterAgentServices(this IServiceCollection services)
services.AddConfiguration();
services.AddLoggingCustom();
services.AddTransient<Parser>();
services.AddMediatR(typeof(Program).Assembly);
services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies());
services.AddAgentHttpClient();
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
services.AddSingleton<IAgentGitHubClient, AgentGitHubClient>();
@ -37,12 +37,15 @@ private static void AddConfiguration(this IServiceCollection services)
.AddJsonFile("appsettings.Development.json", true, true)
#endif
.Build();
services.Configure<ApplicationInsightsSettings>(
config.GetSection(nameof(ApplicationInsightsSettings).RemoveSettingsSuffix()));
services.Configure<ConnectionStringsSettings>(
config.GetSection(nameof(ConnectionStringsSettings).RemoveSettingsSuffix()));
services.Configure<GitHubSettings>(
config.GetSection(nameof(GitHubSettings).RemoveSettingsSuffix()));
services.ConfigureCustom<ApplicationInsightsSettings>(config);
services.ConfigureCustom<ConnectionStringsSettings>(config);
services.ConfigureCustom<GitHubSettings>(config);
}
private static void ConfigureCustom<TSettings>(this IServiceCollection services, IConfiguration configuration)
where TSettings : class
{
services.Configure<TSettings>(configuration.GetSection(typeof(TSettings).Name.RemoveSettingsSuffix()));
}
private static string RemoveSettingsSuffix(this string section)