using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using New.Application.Ports;
using New.Infrastructure.Legacy.Options;
namespace New.Infrastructure.Legacy;
///
/// Composition-root entry point for this project - see
/// New.Infrastructure.Persistence.ServiceCollectionExtensions for why
/// Program.cs only ever calls extension methods like this one, never names
/// LegacyCaseSource/OwnedApplicationSource directly itself.
///
/// Every service below that depends on an `internal` type (LegacyBackendClient,
/// LegacyDetailsWriteThroughTranslator - both internal because their APIs are
/// shaped by legacy DTOs, see Architecture.Tests rule 3) is registered via an
/// explicit factory delegate rather than `services.AddScoped<T>()`'s
/// automatic constructor discovery. That auto-discovery goes through
/// reflection in a different assembly and is not guaranteed to see
/// non-public constructors; a factory delegate compiled here, in the same
/// assembly, calls the constructor directly under ordinary C# accessibility
/// rules - no reflection involved, so there's nothing to be uncertain about.
///
public static class ServiceCollectionExtensions
{
private const string HttpClientName = "LegacyBackend";
public static IServiceCollection AddLegacyInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
services.Configure(configuration.GetSection(LegacyBackendOptions.SectionName));
services.AddSingleton();
services.AddTransient();
services.AddHttpClient(HttpClientName, (sp, http) =>
{
var options = sp.GetRequiredService>().Value;
http.BaseAddress = new Uri(options.BaseUrl);
}).AddHttpMessageHandler();
services.AddScoped(sp =>
new LegacyBackendClient(sp.GetRequiredService().CreateClient(HttpClientName)));
services.AddScoped(sp =>
new LegacyDetailsWriteThroughTranslator(sp.GetRequiredService>()));
services.AddScoped(sp =>
new LegacyCaseSource(sp.GetRequiredService()));
services.AddScoped(sp =>
new LegacyWorklistReader(sp.GetRequiredService()));
services.AddScoped(sp =>
new LegacyCaseGateway(
sp.GetRequiredService(),
sp.GetRequiredService()));
return services;
}
}