< Summary

Information
Class: Chronicis.Client.Extensions.ServiceCollectionExtensions
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Extensions/ServiceCollectionExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 38
Coverable lines: 38
Total lines: 114
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddChronicisApiService(...)100%210%
AddChronicisApiServiceWithSnackbar(...)100%210%
AddChronicisApiServiceWithJSRuntime(...)100%210%
AddChronicisApiServiceConcrete(...)100%210%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Extensions/ServiceCollectionExtensions.cs

#LineLine coverage
 1using Microsoft.JSInterop;
 2using MudBlazor;
 3
 4namespace Chronicis.Client.Extensions;
 5
 6/// <summary>
 7/// Extension methods for IServiceCollection to simplify API service registrations.
 8/// </summary>
 9public static class ServiceCollectionExtensions
 10{
 11    /// <summary>
 12    /// Registers a Chronicis API service with the standard dependencies (HttpClient via factory, ILogger).
 13    /// </summary>
 14    /// <typeparam name="TInterface">The service interface type.</typeparam>
 15    /// <typeparam name="TImplementation">The service implementation type.</typeparam>
 16    /// <param name="services">The service collection.</param>
 17    /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param>
 18    /// <returns>The service collection for chaining.</returns>
 19    public static IServiceCollection AddChronicisApiService<TInterface, TImplementation>(
 20        this IServiceCollection services,
 21        string httpClientName = "ChronicisApi")
 22        where TInterface : class
 23        where TImplementation : class, TInterface
 24    {
 025        services.AddScoped<TInterface>(sp =>
 026        {
 027            var factory = sp.GetRequiredService<IHttpClientFactory>();
 028            var logger = sp.GetRequiredService<ILogger<TImplementation>>();
 029            var httpClient = factory.CreateClient(httpClientName);
 030
 031            return (TInterface)Activator.CreateInstance(typeof(TImplementation), httpClient, logger)!;
 032        });
 33
 034        return services;
 35    }
 36
 37    /// <summary>
 38    /// Registers a Chronicis API service with additional ISnackbar dependency.
 39    /// </summary>
 40    /// <typeparam name="TInterface">The service interface type.</typeparam>
 41    /// <typeparam name="TImplementation">The service implementation type.</typeparam>
 42    /// <param name="services">The service collection.</param>
 43    /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param>
 44    /// <returns>The service collection for chaining.</returns>
 45    public static IServiceCollection AddChronicisApiServiceWithSnackbar<TInterface, TImplementation>(
 46        this IServiceCollection services,
 47        string httpClientName = "ChronicisApi")
 48        where TInterface : class
 49        where TImplementation : class, TInterface
 50    {
 051        services.AddScoped<TInterface>(sp =>
 052        {
 053            var factory = sp.GetRequiredService<IHttpClientFactory>();
 054            var logger = sp.GetRequiredService<ILogger<TImplementation>>();
 055            var snackbar = sp.GetRequiredService<ISnackbar>();
 056            var httpClient = factory.CreateClient(httpClientName);
 057
 058            return (TInterface)Activator.CreateInstance(typeof(TImplementation), httpClient, logger, snackbar)!;
 059        });
 60
 061        return services;
 62    }
 63
 64    /// <summary>
 65    /// Registers a Chronicis API service with additional IJSRuntime dependency.
 66    /// </summary>
 67    /// <typeparam name="TInterface">The service interface type.</typeparam>
 68    /// <typeparam name="TImplementation">The service implementation type.</typeparam>
 69    /// <param name="services">The service collection.</param>
 70    /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param>
 71    /// <returns>The service collection for chaining.</returns>
 72    public static IServiceCollection AddChronicisApiServiceWithJSRuntime<TInterface, TImplementation>(
 73        this IServiceCollection services,
 74        string httpClientName = "ChronicisApi")
 75        where TInterface : class
 76        where TImplementation : class, TInterface
 77    {
 078        services.AddScoped<TInterface>(sp =>
 079        {
 080            var factory = sp.GetRequiredService<IHttpClientFactory>();
 081            var jsRuntime = sp.GetRequiredService<IJSRuntime>();
 082            var logger = sp.GetRequiredService<ILogger<TImplementation>>();
 083            var httpClient = factory.CreateClient(httpClientName);
 084
 085            return (TInterface)Activator.CreateInstance(typeof(TImplementation), httpClient, jsRuntime, logger)!;
 086        });
 87
 088        return services;
 89    }
 90
 91    /// <summary>
 92    /// Registers a concrete Chronicis API service (no interface).
 93    /// </summary>
 94    /// <typeparam name="TImplementation">The service implementation type.</typeparam>
 95    /// <param name="services">The service collection.</param>
 96    /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param>
 97    /// <returns>The service collection for chaining.</returns>
 98    public static IServiceCollection AddChronicisApiServiceConcrete<TImplementation>(
 99        this IServiceCollection services,
 100        string httpClientName = "ChronicisApi")
 101        where TImplementation : class
 102    {
 0103        services.AddScoped<TImplementation>(sp =>
 0104        {
 0105            var factory = sp.GetRequiredService<IHttpClientFactory>();
 0106            var logger = sp.GetRequiredService<ILogger<TImplementation>>();
 0107            var httpClient = factory.CreateClient(httpClientName);
 0108
 0109            return (TImplementation)Activator.CreateInstance(typeof(TImplementation), httpClient, logger)!;
 0110        });
 111
 0112        return services;
 113    }
 114}