Files
ReC/src/ReC.Application/DependencyInjection.cs
TekH bc700e2cd2 Refactor HTTP client name constant usage
Replaced the old HttpClientName constant in Constants.cs with a new Http.ClientName in the ReC.Application.Common.Constants namespace. Updated all references and using directives accordingly to improve code organization and maintainability.
2025-12-12 12:54:57 +01:00

121 lines
4.1 KiB
C#

using FluentValidation;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ReC.Application.Common.Behaviors;
using ReC.Application.Common.Constants;
using ReC.Application.Common.Options;
using System.Reflection;
namespace ReC.Application;
public static class DependencyInjection
{
public static IServiceCollection AddRecServices(this IServiceCollection services, Action<ConfigurationOptions> options)
{
var configOpt = new ConfigurationOptions();
options.Invoke(configOpt);
configOpt.EnsureRequiredServices();
configOpt.ApplyConfigurations(services);
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
services.AddAutoMapper(cfg =>
{
cfg.AddMaps(Assembly.GetExecutingAssembly());
cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey;
});
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
cfg.AddOpenBehaviors([typeof(BodyQueryBehavior<,>), typeof(HeaderQueryBehavior<,>)]);
cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey;
});
services.AddHttpClient(Http.ClientName)
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
UseDefaultCredentials = false
});
return services;
}
public class ConfigurationOptions
{
#region Required Services
private readonly Dictionary<string, bool> _requiredServices = new()
{
{ nameof(ConfigureRecActions), false },
{ nameof(LuckyPennySoftwareLicenseKey), false }
};
internal void EnsureRequiredServices()
{
var missingServices = _requiredServices
.Where(kvp => !kvp.Value)
.Select(kvp => kvp.Key.Replace("Configure", string.Empty));
if (missingServices.Any())
throw new InvalidOperationException($"The following required services were not configured: {string.Join(", ", missingServices)}");
}
#endregion Required Services
#region Configuration
private readonly Queue<Action<IServiceCollection>> _configActions = new();
internal void ApplyConfigurations(IServiceCollection services)
{
while (_configActions.Count > 0)
{
var action = _configActions.Dequeue();
action(services);
}
}
#endregion Configuration
#region LuckyPennySoftwareLicenseKey
private string? _luckyPennySoftwareLicenseKey;
public string? LuckyPennySoftwareLicenseKey
{
get => _luckyPennySoftwareLicenseKey;
set
{
_luckyPennySoftwareLicenseKey = value;
if (value is not null)
_requiredServices[nameof(LuckyPennySoftwareLicenseKey)] = true;
}
}
#endregion LuckyPennySoftwareLicenseKey
#region ConfigureRecActions
public ConfigurationOptions ConfigureRecActions(Action<RecActionOptions> configure)
{
_configActions.Enqueue(services => services.Configure(configure));
if(_requiredServices[nameof(ConfigureRecActions)])
throw new InvalidOperationException("RecActionOptions have already been configured.");
_requiredServices[nameof(ConfigureRecActions)] = true;
return this;
}
public ConfigurationOptions ConfigureRecActions(IConfiguration configuration)
{
_configActions.Enqueue(services => services.Configure<RecActionOptions>(configuration));
if (_requiredServices[nameof(ConfigureRecActions)])
throw new InvalidOperationException("RecActionOptions have already been configured.");
_requiredServices[nameof(ConfigureRecActions)] = true;
return this;
}
#endregion ConfigureRecActions
}
}