< 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
85%
Covered lines: 83
Uncovered lines: 14
Coverable lines: 97
Total lines: 208
Line coverage: 85.5%
Branch coverage
30%
Covered branches: 3
Total branches: 10
Branch coverage: 30%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddEtcdClient(...)50%2287.5%
AddEtcdClient(...)100%11100%
AddEtcdClient(...)100%11100%
AddEtcdClient(...)100%11100%
AddEtcdClient(...)0%2288.46%
AddEtcdClient(...)33.33%7667.85%

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)
 235    {
 236        ArgumentNullException.ThrowIfNull(services);
 37
 138        if (string.IsNullOrWhiteSpace(connectionString))
 039        {
 040            throw new ArgumentNullException(nameof(connectionString));
 41        }
 42
 143        EtcdClientOptions options = new()
 144        {
 145            ConnectionString = connectionString,
 146            Port = port,
 147            ServerName = serverName,
 148            ConfigureChannel = configureChannel,
 149            Interceptors = interceptors
 150        };
 51
 52        // Validate the options
 153        EtcdClientOptionsValidator.ValidateOptions(options);
 54
 155        return AddEtcdClient(services, options);
 156    }
 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)
 267    {
 268        ArgumentNullException.ThrowIfNull(services);
 269        ArgumentNullException.ThrowIfNull(configureClient);
 70
 171        EtcdClientOptions options = new();
 172        configureClient(options);
 73
 174        return AddEtcdClient(services, options);
 175    }
 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)
 3113    {
 114        // Validate the options
 3115        EtcdClientOptionsValidator.ValidateOptions(options);
 116
 6117        return AddEtcdClient(services, _ => options);
 3118    }
 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)
 5129    {
 5130        ArgumentNullException.ThrowIfNull(services);
 131
 132        // Register EtcdClient as a singleton
 5133        services.TryAddSingleton(sp =>
 5134        {
 5135            EtcdClientOptions options = optionsFunc(sp);
 5136
 5137            // Validate the options
 5138            EtcdClientOptionsValidator.ValidateOptions(options);
 5139
 5140            // Create a wrapper action that applies both the options and any custom channel configuration
 5141            Action<GrpcChannelOptions> channelConfiguration = options.ApplyTo;
 5142
 5143            return (IEtcdClient)new EtcdClient(
 5144                options.ConnectionString,
 5145                options.Port,
 5146                options.ServerName,
 5147                channelConfiguration,
 5148                options.Interceptors);
 10149        });
 150
 151        // Register IWatchManager for direct access if needed
 5152        services.TryAddTransient(serviceProvider =>
 0153        {
 0154            IEtcdClient etcdClient = serviceProvider.GetRequiredService<IEtcdClient>();
 0155            return (etcdClient as EtcdClient)?.GetWatchManager();
 5156        });
 157
 5158        return services;
 5159    }
 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)
 1173    {
 1174        ArgumentNullException.ThrowIfNull(services);
 1175        ArgumentNullException.ThrowIfNull(connectionFactory);
 176
 177        // Register EtcdClient as a singleton
 1178        services.TryAddSingleton(serviceProvider =>
 1179        {
 1180            IConnection connection = connectionFactory(serviceProvider);
 1181
 1182            if (watchManagerFactory == null)
 0183            {
 0184                return (IEtcdClient)new EtcdClient(connection);
 1185            }
 1186
 1187            IWatchManager watchManager = watchManagerFactory(serviceProvider);
 1188            return new EtcdClient(connection, watchManager);
 2189        });
 190
 191        // If a watch manager factory is provided, also register IWatchManager directly
 1192        if (watchManagerFactory != null)
 1193        {
 1194            services.TryAddSingleton(watchManagerFactory);
 1195        }
 196        else
 0197        {
 198            // Register IWatchManager for direct access if needed
 0199            services.TryAddTransient(serviceProvider =>
 0200            {
 0201                IEtcdClient etcdClient = serviceProvider.GetRequiredService<IEtcdClient>();
 0202                return (etcdClient as EtcdClient)?.GetWatchManager();
 0203            });
 0204        }
 205
 1206        return services;
 1207    }
 208}