refactor(ClientOptions): move to inf layer

This commit is contained in:
tekh 2025-08-15 10:39:25 +02:00
parent c21e4a93ef
commit 3af571ea37
5 changed files with 17 additions and 53 deletions

View File

@ -9,31 +9,13 @@ namespace Leanetec.EConnect.Client;
/// </summary>
public static class Client
{
private static Action<ClientOptions>? _options = null;
/// <summary>
/// Gets or sets the configuration options for the EConnect client.
/// Can only be set once; subsequent attempts to set will result in an exception.
/// </summary>
public static Action<ClientOptions> Options
{
get => _options ?? throw new InvalidOperationException("EConnect Client options have not been configured. Please set the 'Client.Options' property before accessing client services.");
set
{
if (_options is null)
_options = value;
else
throw new InvalidOperationException("EConnect Client options have already been configured. Reassignment is not allowed.");
}
}
/// <summary>
/// Lazily initializes the <see cref="IServiceProvider"/> that registers and builds the EConnect client services.
/// </summary>
private static readonly Lazy<IServiceProvider> _serviceProvider = new(() =>
{
var services = new ServiceCollection();
services.AddEConnectClient(Options);
services.AddEConnectClient();
return services.BuildServiceProvider();
});

View File

@ -5,32 +5,9 @@ namespace Leanetec.EConnect.Client;
public static class DependencyInjection
{
public static IServiceCollection AddEConnectClient(this IServiceCollection services, Action<Config>? options = null)
public static IServiceCollection AddEConnectClient(this IServiceCollection services)
{
var config = new Config(services);
options?.Invoke(config);
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly));
return services;
}
public sealed class Config
{
private readonly IServiceCollection _services;
internal Config(IServiceCollection services)
{
_services = services;
}
public void ConfigureClient(IConfiguration config)
{
_services.Configure<ClientOptions>(config);
}
public void ConfigureClient(Action<ClientOptions> options)
{
_services.Configure(options);
}
}
}

View File

@ -1,4 +1,4 @@
namespace Leanetec.EConnect.Client;
namespace Leanetec.EConnect.Infrastructure;
public class ClientOptions
{

View File

@ -1,5 +1,5 @@
using Leanetec.EConnect.Client;
using Leanetec.EConnect.Client.Interface;
using Leanetec.EConnect.Client.Interface;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
@ -9,7 +9,7 @@ public static class DependencyInjection
{
internal static readonly string EConnectClientName = Guid.NewGuid().ToString();
internal static IServiceCollection ConfigureEConnectClient(this IServiceCollection services, Action<HttpClient> configureClient)
internal static IServiceCollection ConfigureEConnectClient(this IServiceCollection services)
{
services.AddHttpClient(EConnectClientName, (provider, client) => {
var opt = provider.GetRequiredService<IOptions<ClientOptions>>().Value;
@ -20,9 +20,6 @@ public static class DependencyInjection
if (opt.Timeout is TimeSpan timeout)
client.Timeout = timeout;
// add spesific (library based) parameters
configureClient(client);
});
return services;
}
@ -31,7 +28,7 @@ public static class DependencyInjection
{
Config config = new(services);
options?.Invoke(config);
services.ConfigureEConnectClient(config.EConnectClient);
services.ConfigureEConnectClient();
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
return services;
}
@ -45,6 +42,14 @@ public static class DependencyInjection
_services = services;
}
public Action<HttpClient> EConnectClient { get; set; } = _ => { };
public void ConfigureClient(IConfiguration config)
{
_services.Configure<ClientOptions>(config);
}
public void ConfigureClient(Action<ClientOptions> options)
{
_services.Configure(options);
}
}
}

View File

@ -6,7 +6,7 @@ var config = builder.Configuration;
// Add services to the container.
var clientOpt = config.GetSection("EConnect");
builder.Services.AddEConnectClient(opt => opt.ConfigureClient(clientOpt)).AddInfrastructureServices();
builder.Services.AddEConnectClient().AddInfrastructureServices(opt => opt.ConfigureClient(clientOpt));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle