Skip to content

Commit

Permalink
Added keyed services support
Browse files Browse the repository at this point in the history
  • Loading branch information
vugarli committed Jul 31, 2024
1 parent 0c334d1 commit 4de8e91
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions Minio/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

using System.Collections.ObjectModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand All @@ -29,35 +30,67 @@ 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<IMinioClient> 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<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

0 comments on commit 4de8e91

Please sign in to comment.