From 4de8e917491680e5140a2143f03a57dcf8c50dec Mon Sep 17 00:00:00 2001 From: vugar Date: Thu, 1 Aug 2024 00:54:47 +0400 Subject: [PATCH 1/3] Added keyed services support --- Minio/ServiceCollectionExtensions.cs | 61 +++++++++++++++++++++------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/Minio/ServiceCollectionExtensions.cs b/Minio/ServiceCollectionExtensions.cs index fcf0ac97..9d72a76f 100644 --- a/Minio/ServiceCollectionExtensions.cs +++ b/Minio/ServiceCollectionExtensions.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using System.Collections.ObjectModel; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -29,16 +30,54 @@ 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; } - public static IServiceCollection AddMinio( this IServiceCollection services, Action configureClient, 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; } From 7076d6f067242b471329164b7c82a37ab8162133 Mon Sep 17 00:00:00 2001 From: vugar Date: Tue, 6 Aug 2024 11:03:14 +0400 Subject: [PATCH 2/3] fixedformatting --- Minio/ServiceCollectionExtensions.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Minio/ServiceCollectionExtensions.cs b/Minio/ServiceCollectionExtensions.cs index 9d72a76f..469265cc 100644 --- a/Minio/ServiceCollectionExtensions.cs +++ b/Minio/ServiceCollectionExtensions.cs @@ -14,7 +14,6 @@ * limitations under the License. */ -using System.Collections.ObjectModel; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -30,9 +29,11 @@ public static IServiceCollection AddMinio( { if (services is null) throw new ArgumentNullException(nameof(services)); - _ = services.AddMinioInternal(configureClient => configureClient.WithCredentials(accessKey, secretKey), lifetime); + _ = services.AddMinioInternal(configureClient => configureClient.WithCredentials(accessKey, secretKey), + lifetime); return services; } + public static IServiceCollection AddMinio( this IServiceCollection services, Action configureClient, @@ -44,7 +45,6 @@ public static IServiceCollection AddMinio( return services; } - public static IServiceCollection AddKeyedMinio( this IServiceCollection services, Action configureClient, @@ -57,7 +57,6 @@ public static IServiceCollection AddKeyedMinio( return services; } - public static IServiceCollection AddKeyedMinio( this IServiceCollection services, string accessKey, @@ -67,7 +66,8 @@ public static IServiceCollection AddKeyedMinio( { if (services is null) throw new ArgumentNullException(nameof(services)); - _ = services.AddMinioInternal(configureClient => configureClient.WithCredentials(accessKey, secretKey), lifetime, serviceKey); + _ = services.AddMinioInternal(configureClient => configureClient.WithCredentials(accessKey, secretKey), + lifetime, serviceKey); return services; } From b13043158f6b51dcc49305166bbd28ab2011caba Mon Sep 17 00:00:00 2001 From: vugar Date: Sat, 24 Aug 2024 15:16:11 +0400 Subject: [PATCH 3/3] addedtests --- .../ServiceCollectionExtensionsTests.cs | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Minio.Tests/ServiceCollectionExtensionsTests.cs 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)); + } +}