Skip to content

Commit bad7e3d

Browse files
committed
Add AspireCouchbaseClientBuilder for client registration extensibility
1 parent faef475 commit bad7e3d

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Couchbase.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace Couchbase.Aspire.Client;
5+
6+
/// <summary>
7+
/// Provides a builder for configuring Couchbase client services in an Aspire application.
8+
/// </summary>
9+
/// <param name="hostBuilder">The <see cref="IHostApplicationBuilder"/> with which services are being registered.</param>
10+
/// <param name="settings">The <see cref="CouchbaseClientSettings"/> to configure the Couchbase client.</param>
11+
/// <param name="serviceKey">The service key used to register the <see cref="IClusterProvider"/> service, if any.</param>
12+
public sealed class AspireCouchbaseClientBuilder(
13+
IHostApplicationBuilder hostBuilder,
14+
CouchbaseClientSettings settings,
15+
string? serviceKey)
16+
{
17+
/// <summary>
18+
/// Gets the <see cref="IHostApplicationBuilder"/> with which services are being registered.
19+
/// </summary>
20+
public IHostApplicationBuilder HostBuilder { get; } = hostBuilder ?? throw new ArgumentNullException(nameof(hostBuilder));
21+
22+
/// <summary>
23+
/// Gets the <see cref="CouchbaseClientSettings"/> used to configure the Couchbase client.
24+
/// </summary>
25+
public CouchbaseClientSettings Settings { get; } = settings;
26+
27+
/// <summary>
28+
/// Gets the service key used to register the <see cref="IClusterProvider"/> service, if any.
29+
/// </summary>
30+
public string? ServiceKey { get; } = serviceKey;
31+
}

src/Couchbase.Aspire.Client/AspireCouchbaseExtensions.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ public static class AspireCouchbaseExtensions
2929
/// <param name="configureClusterOptions">An optional method that can be used for customizing the <see cref="ClusterOptions"/>. It's invoked after the options are read from the configuration.</param>
3030
/// <remarks>Reads the configuration from "Aspire:Couchbase:Client" section.</remarks>
3131
public static void AddCouchbaseClient(
32+
this IHostApplicationBuilder builder,
33+
string connectionName,
34+
Action<CouchbaseClientSettings>? configureSettings = null,
35+
Action<ClusterOptions>? configureClusterOptions = null)
36+
=> AddCouchbaseClientBuilder(builder, connectionName, configureSettings, configureClusterOptions);
37+
38+
/// <summary>
39+
/// Registers <see cref="IClusterProvider"/> as a singleton in the services provided by the <paramref name="builder"/>.
40+
/// Enables retries, corresponding health check, logging, and telemetry.
41+
/// </summary>
42+
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
43+
/// <param name="connectionName">A name used to retrieve the connection string from the ConnectionStrings configuration section.</param>
44+
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="CouchbaseClientSettings"/>. It's invoked after the settings are read from the configuration.</param>
45+
/// <param name="configureClusterOptions">An optional method that can be used for customizing the <see cref="ClusterOptions"/>. It's invoked after the options are read from the configuration.</param>
46+
/// <remarks>Reads the configuration from "Aspire:Couchbase:Client" section.</remarks>
47+
public static AspireCouchbaseClientBuilder AddCouchbaseClientBuilder(
3248
this IHostApplicationBuilder builder,
3349
string connectionName,
3450
Action<CouchbaseClientSettings>? configureSettings = null,
@@ -49,13 +65,30 @@ public static void AddKeyedCouchbaseClient(
4965
string name,
5066
Action<CouchbaseClientSettings>? configureSettings = null,
5167
Action<ClusterOptions>? configureClusterOptions = null)
68+
=> AddKeyedCouchbaseClientBuilder(builder, name, configureSettings, configureClusterOptions);
69+
70+
71+
/// <summary>
72+
/// Registers <see cref="IClusterProvider"/> as a keyed singleton for the given <paramref name="name"/> in the services provided by the <paramref name="builder"/>.
73+
/// Enables retries, corresponding health check, logging, and telemetry.
74+
/// </summary>
75+
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
76+
/// <param name="name">The name of the component, which is used as the <see cref="ServiceDescriptor.ServiceKey"/> of the service and also to retrieve the connection string from the ConnectionStrings configuration section.</param>
77+
/// <param name="configureSettings">An optional method that can be used for customizing the <see cref="CouchbaseClientSettings"/>. It's invoked after the settings are read from the configuration.</param>
78+
/// <param name="configureClusterOptions">An optional method that can be used for customizing the <see cref="ClusterOptions"/>. It's invoked after the options are read from the configuration.</param>
79+
/// <remarks>Reads the configuration from "Aspire:Couchbase:Client:{name}" section.</remarks>
80+
public static AspireCouchbaseClientBuilder AddKeyedCouchbaseClientBuilder(
81+
this IHostApplicationBuilder builder,
82+
string name,
83+
Action<CouchbaseClientSettings>? configureSettings = null,
84+
Action<ClusterOptions>? configureClusterOptions = null)
5285
{
5386
ArgumentException.ThrowIfNullOrEmpty(name);
5487

55-
AddCouchbaseClient(builder, configureSettings, configureClusterOptions, connectionName: name, serviceKey: name);
88+
return AddCouchbaseClient(builder, configureSettings, configureClusterOptions, connectionName: name, serviceKey: name);
5689
}
5790

58-
private static void AddCouchbaseClient(
91+
private static AspireCouchbaseClientBuilder AddCouchbaseClient(
5992
IHostApplicationBuilder builder,
6093
Action<CouchbaseClientSettings>? configureSettings,
6194
Action<ClusterOptions>? configureClusterOptions,
@@ -210,6 +243,8 @@ ValueTask<ICluster> ClusterFactory(CancellationToken ct)
210243
failureStatus: default,
211244
tags: default));
212245
}
246+
247+
return new AspireCouchbaseClientBuilder(builder, settings, serviceKey);
213248
}
214249

215250
private static void ApplyNodeRequirement(

0 commit comments

Comments
 (0)