tidy DI registration

This commit is contained in:
Collin M. Barrett 2019-07-02 12:48:30 -05:00
parent aef399ef2d
commit 6505d203c6

View file

@ -16,25 +16,11 @@ public static class ServiceCollectionExtensions
public static void RegisterAgentServices(this IServiceCollection services)
{
services.AddConfiguration();
services.AddLogging(b =>
{
b.AddConsole();
var appInsightsKey = b.Services.BuildServiceProvider().GetService<IOptions<ApplicationInsights>>().Value
.InstrumentationKey;
b.AddApplicationInsights(appInsightsKey);
});
services.AddLoggingCustom();
services.AddMediatR(typeof(Program).Assembly);
services.AddHttpClient<AgentHttpClient>();
services.AddSingleton<IFilterListsApiClient, FilterListsApiClient>();
services.AddSingleton<IGitHubClient>(s =>
{
var gitHubConfig = s.GetService<IOptions<GitHub>>();
var client = new GitHubClient(new ProductHeaderValue(gitHubConfig.Value.ProductHeaderValue))
{
Credentials = new Credentials(gitHubConfig.Value.OauthToken)
};
return client;
});
services.AddGitHubClient();
services.AddTransient<IListInfoRepository, ListInfoRepository>();
services.AddTransient<IUrlRepository, UrlRepository>();
}
@ -52,5 +38,27 @@ private static void AddConfiguration(this IServiceCollection services)
services.Configure<ConnectionStrings>(config.GetSection(nameof(ConnectionStrings)));
services.Configure<GitHub>(config.GetSection(nameof(GitHub)));
}
private static void AddLoggingCustom(this IServiceCollection services)
{
services.AddLogging(b =>
{
b.AddConsole();
var appInsightsConfig = b.Services.BuildServiceProvider().GetService<IOptions<ApplicationInsights>>();
b.AddApplicationInsights(appInsightsConfig.Value.InstrumentationKey);
});
}
private static void AddGitHubClient(this IServiceCollection services)
{
services.AddSingleton<IGitHubClient>(s =>
{
var gitHubConfig = s.GetService<IOptions<GitHub>>();
return new GitHubClient(new ProductHeaderValue(gitHubConfig.Value.ProductHeaderValue))
{
Credentials = new Credentials(gitHubConfig.Value.OauthToken)
};
});
}
}
}