refactor(ClientOptions): move to inf layer
This commit is contained in:
parent
c21e4a93ef
commit
3af571ea37
@ -9,31 +9,13 @@ namespace Leanetec.EConnect.Client;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Client
|
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>
|
/// <summary>
|
||||||
/// Lazily initializes the <see cref="IServiceProvider"/> that registers and builds the EConnect client services.
|
/// Lazily initializes the <see cref="IServiceProvider"/> that registers and builds the EConnect client services.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Lazy<IServiceProvider> _serviceProvider = new(() =>
|
private static readonly Lazy<IServiceProvider> _serviceProvider = new(() =>
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddEConnectClient(Options);
|
services.AddEConnectClient();
|
||||||
return services.BuildServiceProvider();
|
return services.BuildServiceProvider();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -5,32 +5,9 @@ namespace Leanetec.EConnect.Client;
|
|||||||
|
|
||||||
public static class DependencyInjection
|
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));
|
services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly));
|
||||||
return services;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
namespace Leanetec.EConnect.Client;
|
namespace Leanetec.EConnect.Infrastructure;
|
||||||
|
|
||||||
public class ClientOptions
|
public class ClientOptions
|
||||||
{
|
{
|
||||||
@ -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.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
internal static readonly string EConnectClientName = Guid.NewGuid().ToString();
|
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) => {
|
services.AddHttpClient(EConnectClientName, (provider, client) => {
|
||||||
var opt = provider.GetRequiredService<IOptions<ClientOptions>>().Value;
|
var opt = provider.GetRequiredService<IOptions<ClientOptions>>().Value;
|
||||||
@ -20,9 +20,6 @@ public static class DependencyInjection
|
|||||||
|
|
||||||
if (opt.Timeout is TimeSpan timeout)
|
if (opt.Timeout is TimeSpan timeout)
|
||||||
client.Timeout = timeout;
|
client.Timeout = timeout;
|
||||||
|
|
||||||
// add spesific (library based) parameters
|
|
||||||
configureClient(client);
|
|
||||||
});
|
});
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
@ -31,7 +28,7 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
Config config = new(services);
|
Config config = new(services);
|
||||||
options?.Invoke(config);
|
options?.Invoke(config);
|
||||||
services.ConfigureEConnectClient(config.EConnectClient);
|
services.ConfigureEConnectClient();
|
||||||
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
|
services.AddScoped(typeof(IEConnectClient<>), typeof(EConnectClient<>));
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
@ -45,6 +42,14 @@ public static class DependencyInjection
|
|||||||
_services = services;
|
_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ var config = builder.Configuration;
|
|||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
var clientOpt = config.GetSection("EConnect");
|
var clientOpt = config.GetSection("EConnect");
|
||||||
builder.Services.AddEConnectClient(opt => opt.ConfigureClient(clientOpt)).AddInfrastructureServices();
|
builder.Services.AddEConnectClient().AddInfrastructureServices(opt => opt.ConfigureClient(clientOpt));
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user