| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using dotnet_etcd.interfaces; |
| | | 6 | | using Grpc.Core.Interceptors; |
| | | 7 | | using Grpc.Net.Client; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | |
| | | 11 | | namespace dotnet_etcd.DependencyInjection; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Extension methods for setting up etcd related services in an <see cref="IServiceCollection" />. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class ServiceCollectionExtensions |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Adds etcd client services to the specified <see cref="IServiceCollection" />. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> |
| | | 22 | | /// <param name="connectionString">The connection string for etcd.</param> |
| | | 23 | | /// <param name="port">The port to connect to.</param> |
| | | 24 | | /// <param name="serverName">The server name.</param> |
| | | 25 | | /// <param name="configureChannel">Optional action to configure channel options.</param> |
| | | 26 | | /// <param name="interceptors">Optional interceptors to apply to calls.</param> |
| | | 27 | | /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> |
| | | 28 | | public static IServiceCollection AddEtcdClient( |
| | | 29 | | this IServiceCollection services, |
| | | 30 | | string connectionString, |
| | | 31 | | int port = 2379, |
| | | 32 | | string serverName = "my-etcd-server", |
| | | 33 | | Action<GrpcChannelOptions> configureChannel = null, |
| | | 34 | | Interceptor[] interceptors = null) |
| | 2 | 35 | | { |
| | 2 | 36 | | ArgumentNullException.ThrowIfNull(services); |
| | | 37 | | |
| | 1 | 38 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| | 0 | 39 | | { |
| | 0 | 40 | | throw new ArgumentNullException(nameof(connectionString)); |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | EtcdClientOptions options = new() |
| | 1 | 44 | | { |
| | 1 | 45 | | ConnectionString = connectionString, |
| | 1 | 46 | | Port = port, |
| | 1 | 47 | | ServerName = serverName, |
| | 1 | 48 | | ConfigureChannel = configureChannel, |
| | 1 | 49 | | Interceptors = interceptors |
| | 1 | 50 | | }; |
| | | 51 | | |
| | | 52 | | // Validate the options |
| | 1 | 53 | | EtcdClientOptionsValidator.ValidateOptions(options); |
| | | 54 | | |
| | 1 | 55 | | return AddEtcdClient(services, options); |
| | 1 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Adds etcd client services to the specified <see cref="IServiceCollection" /> with the specified configuratio |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> |
| | | 62 | | /// <param name="configureClient">An action to configure the etcd client options.</param> |
| | | 63 | | /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> |
| | | 64 | | public static IServiceCollection AddEtcdClient( |
| | | 65 | | this IServiceCollection services, |
| | | 66 | | Action<EtcdClientOptions> configureClient) |
| | 2 | 67 | | { |
| | 2 | 68 | | ArgumentNullException.ThrowIfNull(services); |
| | 2 | 69 | | ArgumentNullException.ThrowIfNull(configureClient); |
| | | 70 | | |
| | 1 | 71 | | EtcdClientOptions options = new(); |
| | 1 | 72 | | configureClient(options); |
| | | 73 | | |
| | 1 | 74 | | return AddEtcdClient(services, options); |
| | 1 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Adds etcd client services to the specified <see cref="IServiceCollection" /> with the specified configuratio |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> |
| | | 81 | | /// <param name="configureClient">An action to configure the etcd client options.</param> |
| | | 82 | | /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> |
| | | 83 | | public static IServiceCollection AddEtcdClient( |
| | | 84 | | this IServiceCollection services, |
| | | 85 | | Action<IServiceProvider, EtcdClientOptions> configureClient) |
| | 1 | 86 | | { |
| | 1 | 87 | | ArgumentNullException.ThrowIfNull(services); |
| | 1 | 88 | | ArgumentNullException.ThrowIfNull(configureClient); |
| | | 89 | | |
| | 1 | 90 | | return AddEtcdClient( |
| | 1 | 91 | | services, |
| | 1 | 92 | | sp => |
| | 1 | 93 | | { |
| | 1 | 94 | | EtcdClientOptions options = new(); |
| | 1 | 95 | | configureClient(sp, options); |
| | 1 | 96 | | |
| | 1 | 97 | | // Validate the options |
| | 1 | 98 | | EtcdClientOptionsValidator.ValidateOptions(options); |
| | 1 | 99 | | |
| | 1 | 100 | | return options; |
| | 2 | 101 | | }); |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Adds etcd client services to the specified <see cref="IServiceCollection" /> with the specified options. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> |
| | | 108 | | /// <param name="options">The options to configure the etcd client.</param> |
| | | 109 | | /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> |
| | | 110 | | public static IServiceCollection AddEtcdClient( |
| | | 111 | | this IServiceCollection services, |
| | | 112 | | EtcdClientOptions options) |
| | 3 | 113 | | { |
| | | 114 | | // Validate the options |
| | 3 | 115 | | EtcdClientOptionsValidator.ValidateOptions(options); |
| | | 116 | | |
| | 6 | 117 | | return AddEtcdClient(services, _ => options); |
| | 3 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Adds etcd client services to the specified <see cref="IServiceCollection" /> with the specified options. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> |
| | | 124 | | /// <param name="optionsFunc">A func that returns the options to configure the etcd client.</param> |
| | | 125 | | /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> |
| | | 126 | | public static IServiceCollection AddEtcdClient( |
| | | 127 | | this IServiceCollection services, |
| | | 128 | | Func<IServiceProvider, EtcdClientOptions> optionsFunc) |
| | 5 | 129 | | { |
| | 5 | 130 | | ArgumentNullException.ThrowIfNull(services); |
| | | 131 | | |
| | | 132 | | // Register EtcdClient as a singleton |
| | 5 | 133 | | services.TryAddSingleton(sp => |
| | 5 | 134 | | { |
| | 5 | 135 | | EtcdClientOptions options = optionsFunc(sp); |
| | 5 | 136 | | |
| | 5 | 137 | | // Validate the options |
| | 5 | 138 | | EtcdClientOptionsValidator.ValidateOptions(options); |
| | 5 | 139 | | |
| | 5 | 140 | | // Create a wrapper action that applies both the options and any custom channel configuration |
| | 5 | 141 | | Action<GrpcChannelOptions> channelConfiguration = options.ApplyTo; |
| | 5 | 142 | | |
| | 5 | 143 | | return (IEtcdClient)new EtcdClient( |
| | 5 | 144 | | options.ConnectionString, |
| | 5 | 145 | | options.Port, |
| | 5 | 146 | | options.ServerName, |
| | 5 | 147 | | channelConfiguration, |
| | 5 | 148 | | options.Interceptors); |
| | 10 | 149 | | }); |
| | | 150 | | |
| | | 151 | | // Register IWatchManager for direct access if needed |
| | 5 | 152 | | services.TryAddTransient(serviceProvider => |
| | 0 | 153 | | { |
| | 0 | 154 | | IEtcdClient etcdClient = serviceProvider.GetRequiredService<IEtcdClient>(); |
| | 0 | 155 | | return (etcdClient as EtcdClient)?.GetWatchManager(); |
| | 5 | 156 | | }); |
| | | 157 | | |
| | 5 | 158 | | return services; |
| | 5 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Adds etcd client services to the specified <see cref="IServiceCollection" /> using custom factory functions. |
| | | 163 | | /// This allows for more advanced configuration and testing scenarios. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> |
| | | 166 | | /// <param name="connectionFactory">Factory function to create a connection.</param> |
| | | 167 | | /// <param name="watchManagerFactory">Optional factory function to create a watch manager.</param> |
| | | 168 | | /// <returns>The <see cref="IServiceCollection" /> so that additional calls can be chained.</returns> |
| | | 169 | | public static IServiceCollection AddEtcdClient( |
| | | 170 | | this IServiceCollection services, |
| | | 171 | | Func<IServiceProvider, IConnection> connectionFactory, |
| | | 172 | | Func<IServiceProvider, IWatchManager> watchManagerFactory = null) |
| | 1 | 173 | | { |
| | 1 | 174 | | ArgumentNullException.ThrowIfNull(services); |
| | 1 | 175 | | ArgumentNullException.ThrowIfNull(connectionFactory); |
| | | 176 | | |
| | | 177 | | // Register EtcdClient as a singleton |
| | 1 | 178 | | services.TryAddSingleton(serviceProvider => |
| | 1 | 179 | | { |
| | 1 | 180 | | IConnection connection = connectionFactory(serviceProvider); |
| | 1 | 181 | | |
| | 1 | 182 | | if (watchManagerFactory == null) |
| | 0 | 183 | | { |
| | 0 | 184 | | return (IEtcdClient)new EtcdClient(connection); |
| | 1 | 185 | | } |
| | 1 | 186 | | |
| | 1 | 187 | | IWatchManager watchManager = watchManagerFactory(serviceProvider); |
| | 1 | 188 | | return new EtcdClient(connection, watchManager); |
| | 2 | 189 | | }); |
| | | 190 | | |
| | | 191 | | // If a watch manager factory is provided, also register IWatchManager directly |
| | 1 | 192 | | if (watchManagerFactory != null) |
| | 1 | 193 | | { |
| | 1 | 194 | | services.TryAddSingleton(watchManagerFactory); |
| | 1 | 195 | | } |
| | | 196 | | else |
| | 0 | 197 | | { |
| | | 198 | | // Register IWatchManager for direct access if needed |
| | 0 | 199 | | services.TryAddTransient(serviceProvider => |
| | 0 | 200 | | { |
| | 0 | 201 | | IEtcdClient etcdClient = serviceProvider.GetRequiredService<IEtcdClient>(); |
| | 0 | 202 | | return (etcdClient as EtcdClient)?.GetWatchManager(); |
| | 0 | 203 | | }); |
| | 0 | 204 | | } |
| | | 205 | | |
| | 1 | 206 | | return services; |
| | 1 | 207 | | } |
| | | 208 | | } |