< Summary

Information
Class: dotnet_etcd.DependencyInjection.ServiceCollectionExtensions
Assembly: dotnet-etcd
File(s): /home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/DependencyInjection/ServiceCollectionExtensions.cs
Line coverage
100%
Covered lines: 97
Uncovered lines: 0
Coverable lines: 97
Total lines: 208
Line coverage: 100%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddEtcdClient(...)100%22100%
AddEtcdClient(...)100%11100%
AddEtcdClient(...)100%11100%
AddEtcdClient(...)100%11100%
AddEtcdClient(...)50%22100%
AddEtcdClient(...)83.33%66100%

File(s)

/home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/DependencyInjection/ServiceCollectionExtensions.cs

#LineLine coverage
 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
 4using System;
 5using dotnet_etcd.interfaces;
 6using Grpc.Core.Interceptors;
 7using Grpc.Net.Client;
 8using Microsoft.Extensions.DependencyInjection;
 9using Microsoft.Extensions.DependencyInjection.Extensions;
 10
 11namespace dotnet_etcd.DependencyInjection;
 12
 13/// <summary>
 14///     Extension methods for setting up etcd related services in an <see cref="IServiceCollection" />.
 15/// </summary>
 16public 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)
 635    {
 636        ArgumentNullException.ThrowIfNull(services);
 37
 538        if (string.IsNullOrWhiteSpace(connectionString))
 339        {
 340            throw new ArgumentNullException(nameof(connectionString));
 41        }
 42
 243        EtcdClientOptions options = new()
 244        {
 245            ConnectionString = connectionString,
 246            Port = port,
 247            ServerName = serverName,
 248            ConfigureChannel = configureChannel,
 249            Interceptors = interceptors
 250        };
 51
 52        // Validate the options
 253        EtcdClientOptionsValidator.ValidateOptions(options);
 54
 255        return AddEtcdClient(services, options);
 256    }
 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)
 367    {
 368        ArgumentNullException.ThrowIfNull(services);
 369        ArgumentNullException.ThrowIfNull(configureClient);
 70
 271        EtcdClientOptions options = new();
 272        configureClient(options);
 73
 274        return AddEtcdClient(services, options);
 275    }
 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)
 186    {
 187        ArgumentNullException.ThrowIfNull(services);
 188        ArgumentNullException.ThrowIfNull(configureClient);
 89
 190        return AddEtcdClient(
 191            services,
 192            sp =>
 193            {
 194                EtcdClientOptions options = new();
 195                configureClient(sp, options);
 196
 197                // Validate the options
 198                EtcdClientOptionsValidator.ValidateOptions(options);
 199
 1100                return options;
 2101            });
 1102    }
 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)
 5113    {
 114        // Validate the options
 5115        EtcdClientOptionsValidator.ValidateOptions(options);
 116
 10117        return AddEtcdClient(services, _ => options);
 5118    }
 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)
 7129    {
 7130        ArgumentNullException.ThrowIfNull(services);
 131
 132        // Register EtcdClient as a singleton
 7133        services.TryAddSingleton(sp =>
 7134        {
 7135            EtcdClientOptions options = optionsFunc(sp);
 7136
 7137            // Validate the options
 7138            EtcdClientOptionsValidator.ValidateOptions(options);
 7139
 7140            // Create a wrapper action that applies both the options and any custom channel configuration
 7141            Action<GrpcChannelOptions> channelConfiguration = options.ApplyTo;
 7142
 7143            return (IEtcdClient)new EtcdClient(
 7144                options.ConnectionString,
 7145                options.Port,
 7146                options.ServerName,
 7147                channelConfiguration,
 7148                options.Interceptors);
 14149        });
 150
 151        // Register IWatchManager for direct access if needed
 7152        services.TryAddTransient(serviceProvider =>
 2153        {
 2154            IEtcdClient etcdClient = serviceProvider.GetRequiredService<IEtcdClient>();
 2155            return (etcdClient as EtcdClient)?.GetWatchManager();
 9156        });
 157
 7158        return services;
 7159    }
 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)
 3173    {
 3174        ArgumentNullException.ThrowIfNull(services);
 3175        ArgumentNullException.ThrowIfNull(connectionFactory);
 176
 177        // Register EtcdClient as a singleton
 3178        services.TryAddSingleton(serviceProvider =>
 3179        {
 3180            IConnection connection = connectionFactory(serviceProvider);
 3181
 3182            if (watchManagerFactory == null)
 1183            {
 1184                return (IEtcdClient)new EtcdClient(connection);
 3185            }
 3186
 2187            IWatchManager watchManager = watchManagerFactory(serviceProvider);
 2188            return new EtcdClient(connection, watchManager);
 6189        });
 190
 191        // If a watch manager factory is provided, also register IWatchManager directly
 3192        if (watchManagerFactory != null)
 2193        {
 2194            services.TryAddSingleton(watchManagerFactory);
 2195        }
 196        else
 1197        {
 198            // Register IWatchManager for direct access if needed
 1199            services.TryAddTransient(serviceProvider =>
 1200            {
 1201                IEtcdClient etcdClient = serviceProvider.GetRequiredService<IEtcdClient>();
 1202                return (etcdClient as EtcdClient)?.GetWatchManager();
 2203            });
 1204        }
 205
 3206        return services;
 3207    }
 208}