Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added keyed services support #1148

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions Minio.Tests/ServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<IMinioClient>();

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<IMinioClient>(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<IMinioClient>(serviceKey);
var client2 = serviceProvider.GetService<IMinioClient>();

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<ArgumentNullException>(() => services.AddMinio(configureClient));
}

[TestMethod]
public void RegisterServiceFailsWhenPassedNullConfigureClient()
{
IServiceCollection services = new ServiceCollection();
Action<IMinioClient> configureClient = null;

_ = Assert.ThrowsException<ArgumentNullException>(() => services.AddMinio(configureClient));
}

[TestMethod]
public void RegisterKeyedServiceFailsWhenPassedNullServiceCollection()
{
IServiceCollection services = null;
static void configureClient(IMinioClient client) { }
object key = new();

_ = Assert.ThrowsException<ArgumentNullException>(() => services.AddKeyedMinio(configureClient, key));
}

[TestMethod]
public void RegisterKeyedServiceFailsWhenPassedNullConfigureClient()
{
IServiceCollection services = new ServiceCollection();
Action<IMinioClient> configureClient = null;
object key = new();

_ = Assert.ThrowsException<ArgumentNullException>(() => services.AddKeyedMinio(configureClient, key));
}
}
59 changes: 46 additions & 13 deletions Minio/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -39,25 +40,57 @@ 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<IMinioClient> 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<IMinioClient> 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);
services.TryAddSingleton<IMinioClientFactory>(minioClientFactory);

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;
}
Expand Down
Loading