| | | 1 | | namespace Chronicis.Client.Extensions; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Extension methods for IServiceCollection to simplify API service registrations. |
| | | 5 | | /// </summary> |
| | | 6 | | public static class ServiceCollectionExtensions |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Registers a Chronicis API service with the standard dependencies (HttpClient via factory, ILogger). |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="TInterface">The service interface type.</typeparam> |
| | | 12 | | /// <typeparam name="TImplementation">The service implementation type.</typeparam> |
| | | 13 | | /// <param name="services">The service collection.</param> |
| | | 14 | | /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param> |
| | | 15 | | /// <returns>The service collection for chaining.</returns> |
| | | 16 | | public static IServiceCollection AddChronicisApiService<TInterface, TImplementation>( |
| | | 17 | | this IServiceCollection services, |
| | | 18 | | string httpClientName = "ChronicisApi") |
| | | 19 | | where TInterface : class |
| | | 20 | | where TImplementation : class, TInterface |
| | | 21 | | { |
| | 18 | 22 | | services.AddScoped<TInterface>(sp => |
| | 18 | 23 | | { |
| | 18 | 24 | | var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient(httpClientName); |
| | 18 | 25 | | return ActivatorUtilities.CreateInstance<TImplementation>(sp, httpClient); |
| | 18 | 26 | | }); |
| | | 27 | | |
| | 18 | 28 | | return services; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Registers a Chronicis API service with additional ISnackbar dependency. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <typeparam name="TInterface">The service interface type.</typeparam> |
| | | 35 | | /// <typeparam name="TImplementation">The service implementation type.</typeparam> |
| | | 36 | | /// <param name="services">The service collection.</param> |
| | | 37 | | /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param> |
| | | 38 | | /// <returns>The service collection for chaining.</returns> |
| | | 39 | | public static IServiceCollection AddChronicisApiServiceWithSnackbar<TInterface, TImplementation>( |
| | | 40 | | this IServiceCollection services, |
| | | 41 | | string httpClientName = "ChronicisApi") |
| | | 42 | | where TInterface : class |
| | | 43 | | where TImplementation : class, TInterface |
| | | 44 | | { |
| | 2 | 45 | | services.AddScoped<TInterface>(sp => |
| | 2 | 46 | | { |
| | 2 | 47 | | var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient(httpClientName); |
| | 2 | 48 | | return ActivatorUtilities.CreateInstance<TImplementation>(sp, httpClient); |
| | 2 | 49 | | }); |
| | | 50 | | |
| | 2 | 51 | | return services; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Registers a Chronicis API service with additional IJSRuntime dependency. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <typeparam name="TInterface">The service interface type.</typeparam> |
| | | 58 | | /// <typeparam name="TImplementation">The service implementation type.</typeparam> |
| | | 59 | | /// <param name="services">The service collection.</param> |
| | | 60 | | /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param> |
| | | 61 | | /// <returns>The service collection for chaining.</returns> |
| | | 62 | | public static IServiceCollection AddChronicisApiServiceWithJSRuntime<TInterface, TImplementation>( |
| | | 63 | | this IServiceCollection services, |
| | | 64 | | string httpClientName = "ChronicisApi") |
| | | 65 | | where TInterface : class |
| | | 66 | | where TImplementation : class, TInterface |
| | | 67 | | { |
| | 2 | 68 | | services.AddScoped<TInterface>(sp => |
| | 2 | 69 | | { |
| | 2 | 70 | | var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient(httpClientName); |
| | 2 | 71 | | return ActivatorUtilities.CreateInstance<TImplementation>(sp, httpClient); |
| | 2 | 72 | | }); |
| | | 73 | | |
| | 2 | 74 | | return services; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Registers a concrete Chronicis API service (no interface). |
| | | 79 | | /// </summary> |
| | | 80 | | /// <typeparam name="TImplementation">The service implementation type.</typeparam> |
| | | 81 | | /// <param name="services">The service collection.</param> |
| | | 82 | | /// <param name="httpClientName">The name of the configured HTTP client (default: "ChronicisApi").</param> |
| | | 83 | | /// <returns>The service collection for chaining.</returns> |
| | | 84 | | public static IServiceCollection AddChronicisApiServiceConcrete<TImplementation>( |
| | | 85 | | this IServiceCollection services, |
| | | 86 | | string httpClientName = "ChronicisApi") |
| | | 87 | | where TImplementation : class |
| | | 88 | | { |
| | 1 | 89 | | services.AddScoped<TImplementation>(sp => |
| | 1 | 90 | | { |
| | 1 | 91 | | var httpClient = sp.GetRequiredService<IHttpClientFactory>().CreateClient(httpClientName); |
| | 1 | 92 | | return ActivatorUtilities.CreateInstance<TImplementation>(sp, httpClient); |
| | 1 | 93 | | }); |
| | | 94 | | |
| | 1 | 95 | | return services; |
| | | 96 | | } |
| | | 97 | | } |