diff --git a/Minio.Tests/ServiceCollectionExtensionsTests.cs b/Minio.Tests/ServiceCollectionExtensionsTests.cs new file mode 100644 index 00000000..ff1fc2f8 --- /dev/null +++ b/Minio.Tests/ServiceCollectionExtensionsTests.cs @@ -0,0 +1,102 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Minio.Tests; + +[TestClass] +public class ServiceCollectionExtensionsTests +{ + [TestMethod] + public void RegistersService() + { + var services = new ServiceCollection(); + var accessKey = Guid.NewGuid().ToString(); + var secretKey = Guid.NewGuid().ToString(); + + _ = services.AddMinio(accessKey, secretKey); + using var serviceProvider = services.BuildServiceProvider(); + var client = serviceProvider.GetService(); + + Assert.IsNotNull(client); + Assert.IsInstanceOfType(client, typeof(MinioClient)); + Assert.AreEqual(client.Config.AccessKey, accessKey); + Assert.AreEqual(client.Config.SecretKey, secretKey); + } + + [TestMethod] + public void RegistersKeyedService() + { + var services = new ServiceCollection(); + var serviceKey = new object(); + var accessKey = Guid.NewGuid().ToString(); + var secretKey = Guid.NewGuid().ToString(); + + _ = services.AddKeyedMinio(accessKey, secretKey, serviceKey); + using var serviceProvider = services.BuildServiceProvider(); + var client = serviceProvider.GetKeyedService(serviceKey); + + Assert.IsNotNull(client); + Assert.IsInstanceOfType(client, typeof(MinioClient)); + Assert.AreEqual(client.Config.AccessKey, accessKey); + Assert.AreEqual(client.Config.SecretKey, secretKey); + } + + [TestMethod] + public void RegistersKeyedServiceWithNullKey() + { + var services = new ServiceCollection(); + object serviceKey = null; + var accessKey = Guid.NewGuid().ToString(); + var secretKey = Guid.NewGuid().ToString(); + + _ = services.AddKeyedMinio(accessKey, secretKey, serviceKey); + using var serviceProvider = services.BuildServiceProvider(); + var client = serviceProvider.GetKeyedService(serviceKey); + var client2 = serviceProvider.GetService(); + + Assert.IsNotNull(client); + Assert.IsNotNull(client2); + Assert.AreEqual(client, client2); + Assert.IsInstanceOfType(client, typeof(MinioClient)); + Assert.AreEqual(client.Config.AccessKey, accessKey); + Assert.AreEqual(client.Config.SecretKey, secretKey); + } + + [TestMethod] + public void RegisterServiceFailsWhenPassedNullServiceCollection() + { + IServiceCollection services = null; + static void configureClient(IMinioClient client) { } + + _ = Assert.ThrowsException(() => services.AddMinio(configureClient)); + } + + [TestMethod] + public void RegisterServiceFailsWhenPassedNullConfigureClient() + { + IServiceCollection services = new ServiceCollection(); + Action configureClient = null; + + _ = Assert.ThrowsException(() => services.AddMinio(configureClient)); + } + + [TestMethod] + public void RegisterKeyedServiceFailsWhenPassedNullServiceCollection() + { + IServiceCollection services = null; + static void configureClient(IMinioClient client) { } + object key = new(); + + _ = Assert.ThrowsException(() => services.AddKeyedMinio(configureClient, key)); + } + + [TestMethod] + public void RegisterKeyedServiceFailsWhenPassedNullConfigureClient() + { + IServiceCollection services = new ServiceCollection(); + Action configureClient = null; + object key = new(); + + _ = Assert.ThrowsException(() => services.AddKeyedMinio(configureClient, key)); + } +} diff --git a/Minio/ServiceCollectionExtensions.cs b/Minio/ServiceCollectionExtensions.cs index fcf0ac97..469265cc 100644 --- a/Minio/ServiceCollectionExtensions.cs +++ b/Minio/ServiceCollectionExtensions.cs @@ -29,7 +29,8 @@ public static IServiceCollection AddMinio( { if (services is null) throw new ArgumentNullException(nameof(services)); - _ = services.AddMinio(configureClient => configureClient.WithCredentials(accessKey, secretKey), lifetime); + _ = services.AddMinioInternal(configureClient => configureClient.WithCredentials(accessKey, secretKey), + lifetime); return services; } @@ -39,6 +40,44 @@ public static IServiceCollection AddMinio( ServiceLifetime lifetime = ServiceLifetime.Singleton) { if (services is null) throw new ArgumentNullException(nameof(services)); + + _ = services.AddMinioInternal(configureClient, lifetime); + return services; + } + + public static IServiceCollection AddKeyedMinio( + this IServiceCollection services, + Action configureClient, + object serviceKey, + ServiceLifetime lifetime = ServiceLifetime.Singleton) + { + if (services is null) throw new ArgumentNullException(nameof(services)); + + _ = services.AddMinioInternal(configureClient, lifetime, serviceKey); + return services; + } + + public static IServiceCollection AddKeyedMinio( + this IServiceCollection services, + string accessKey, + string secretKey, + object serviceKey, + ServiceLifetime lifetime = ServiceLifetime.Singleton) + { + if (services is null) throw new ArgumentNullException(nameof(services)); + + _ = services.AddMinioInternal(configureClient => configureClient.WithCredentials(accessKey, secretKey), + lifetime, serviceKey); + return services; + } + + private static IServiceCollection AddMinioInternal( + this IServiceCollection services, + Action configureClient, + ServiceLifetime lifetime, + object serviceKey = null) + { + if (services is null) throw new ArgumentNullException(nameof(services)); if (configureClient == null) throw new ArgumentNullException(nameof(configureClient)); var minioClientFactory = new MinioClientFactory(configureClient); @@ -46,18 +85,12 @@ public static IServiceCollection AddMinio( var client = minioClientFactory.CreateClient(); client.Config.ServiceProvider = services.BuildServiceProvider(); - switch (lifetime) - { - case ServiceLifetime.Singleton: - services.TryAddSingleton(_ => client); - break; - case ServiceLifetime.Scoped: - services.TryAddScoped(_ => client); - break; - case ServiceLifetime.Transient: - services.TryAddTransient(_ => client); - break; - } + + var descriptor = serviceKey is null + ? new ServiceDescriptor(typeof(IMinioClient), _ => client, lifetime) + : new ServiceDescriptor(typeof(IMinioClient), serviceKey, (_, _) => client, lifetime); + + services.TryAdd(descriptor); return services; }