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
Changes from 2 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
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