From 21e3c6c3e87b4277121adcb3cbf5f01076a0d343 Mon Sep 17 00:00:00 2001 From: Brant Burnett Date: Wed, 25 Jan 2017 16:20:21 -0500 Subject: [PATCH] Add TryAddCouchbaseBucket extension (#2) Motivation ---------- Need a way to registered a Couchbase bucket which won't register the bucket again if it is already registered. Modifications ------------- Added TryAddCouchbaseBucket to IServiceCollection extensions, which uses TryAddSingleton instead of AddSingleton to register the bucket. Results ------- Consumer can optionally use TryAddCouchbaseBucket, and if the bucket is already registered it will be ignored. --- .../ServiceCollectionExtensions.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/Couchbase.Extensions.DependencyInjection/ServiceCollectionExtensions.cs b/src/Couchbase.Extensions.DependencyInjection/ServiceCollectionExtensions.cs index 00cf705..63044c5 100644 --- a/src/Couchbase.Extensions.DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Couchbase.Extensions.DependencyInjection/ServiceCollectionExtensions.cs @@ -53,12 +53,29 @@ public static IServiceCollection AddCouchbase(this IServiceCollection services, return services; } + /// + /// Register an interface based on which will be injected + /// with a specific bucket name. + /// + /// Interface inherited from . Must not add any members. + /// The . + /// The name of the Couchbase bucket. + /// The . public static IServiceCollection AddCouchbaseBucket(this IServiceCollection services, string bucketName) where T: class, INamedBucketProvider { return services.AddCouchbaseBucket(bucketName, null); } + /// + /// Register an interface based on which will be injected + /// with a specific bucket name. + /// + /// Interface inherited from . Must not add any members. + /// The . + /// The name of the Couchbase bucket. + /// The bucket password. + /// The . public static IServiceCollection AddCouchbaseBucket(this IServiceCollection services, string bucketName, string password) where T : class, INamedBucketProvider @@ -77,5 +94,47 @@ public static IServiceCollection AddCouchbaseBucket(this IServiceCollection s return services; } + + /// + /// Register an interface based on which will be injected + /// with a specific bucket name if the interface hasn't already been added. + /// + /// Interface inherited from . Must not add any members. + /// The . + /// The name of the Couchbase bucket. + /// The . + public static IServiceCollection TryAddCouchbaseBucket(this IServiceCollection services, string bucketName) + where T : class, INamedBucketProvider + { + return services.AddCouchbaseBucket(bucketName, null); + } + + /// + /// Register an interface based on which will be injected + /// with a specific bucket name if the interface hasn't already been added. + /// + /// Interface inherited from . Must not add any members. + /// The . + /// The name of the Couchbase bucket. + /// The bucket password. + /// The . + public static IServiceCollection TryAddCouchbaseBucket(this IServiceCollection services, string bucketName, + string password) + where T : class, INamedBucketProvider + { + if (bucketName == null) + { + throw new ArgumentNullException(nameof(bucketName)); + } + + services.TryAddSingleton(serviceProvider => + { + var generator = serviceProvider.GetRequiredService(); + + return generator.GetProxy(serviceProvider.GetRequiredService(), bucketName, password); + }); + + return services; + } } }