Initial commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using RobotNet10.CustomConfiguration.Services;
|
||||
using RobotNet10.StorageManager;
|
||||
|
||||
namespace RobotNet10.CustomConfiguration.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods cho dependency injection
|
||||
/// </summary>
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Đăng ký các services cho CustomConfiguration
|
||||
/// StorageConfig sẽ được inject từ project sử dụng thư viện này
|
||||
/// </summary>
|
||||
public static IServiceCollection AddCustomConfiguration(
|
||||
this IServiceCollection services,
|
||||
StorageConfig storageConfig)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(storageConfig);
|
||||
|
||||
// Register StorageConfig as named options (matching ConfigService constructor)
|
||||
services.Configure<StorageConfig>("StorageConfigs", options =>
|
||||
{
|
||||
options.UsingLocal = storageConfig.UsingLocal;
|
||||
options.LocalFolder = storageConfig.LocalFolder;
|
||||
options.Bucket = storageConfig.Bucket;
|
||||
options.MinioConfig = storageConfig.MinioConfig;
|
||||
options.RetryCount = storageConfig.RetryCount;
|
||||
});
|
||||
|
||||
// Register ConfigService (creates StorageManager internally)
|
||||
services.AddSingleton<IConfigService, ConfigService>();
|
||||
|
||||
// Register ConfigManager (depends on IConfigService)
|
||||
services.AddSingleton<IConfigManager, ConfigManager>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Đăng ký các services cho CustomConfiguration với StorageConfig từ IConfiguration
|
||||
/// </summary>
|
||||
public static IServiceCollection AddCustomConfiguration(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string configSectionName = "StorageConfig")
|
||||
{
|
||||
services.Configure<StorageConfig>("StorageConfigs", options =>
|
||||
{
|
||||
configuration.GetSection(configSectionName).Bind(options);
|
||||
});
|
||||
|
||||
// Register ConfigService (creates StorageManager internally)
|
||||
services.AddSingleton<IConfigService, ConfigService>();
|
||||
|
||||
// Register ConfigManager (depends on IConfigService)
|
||||
services.AddSingleton<IConfigManager, ConfigManager>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user