| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | using MudBlazor; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extension methods for IServiceCollection to simplify API service registrations. |
| | | 8 | | /// </summary> |
| | | 9 | | public 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 | | { |
| | 0 | 25 | | services.AddScoped<TInterface>(sp => |
| | 0 | 26 | | { |
| | 0 | 27 | | var factory = sp.GetRequiredService<IHttpClientFactory>(); |
| | 0 | 28 | | var logger = sp.GetRequiredService<ILogger<TImplementation>>(); |
| | 0 | 29 | | var httpClient = factory.CreateClient(httpClientName); |
| | 0 | 30 | | |
| | 0 | 31 | | return (TInterface)Activator.CreateInstance(typeof(TImplementation), httpClient, logger)!; |
| | 0 | 32 | | }); |
| | | 33 | | |
| | 0 | 34 | | 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 | | { |
| | 0 | 51 | | services.AddScoped<TInterface>(sp => |
| | 0 | 52 | | { |
| | 0 | 53 | | var factory = sp.GetRequiredService<IHttpClientFactory>(); |
| | 0 | 54 | | var logger = sp.GetRequiredService<ILogger<TImplementation>>(); |
| | 0 | 55 | | var snackbar = sp.GetRequiredService<ISnackbar>(); |
| | 0 | 56 | | var httpClient = factory.CreateClient(httpClientName); |
| | 0 | 57 | | |
| | 0 | 58 | | return (TInterface)Activator.CreateInstance(typeof(TImplementation), httpClient, logger, snackbar)!; |
| | 0 | 59 | | }); |
| | | 60 | | |
| | 0 | 61 | | 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 | | { |
| | 0 | 78 | | services.AddScoped<TInterface>(sp => |
| | 0 | 79 | | { |
| | 0 | 80 | | var factory = sp.GetRequiredService<IHttpClientFactory>(); |
| | 0 | 81 | | var jsRuntime = sp.GetRequiredService<IJSRuntime>(); |
| | 0 | 82 | | var logger = sp.GetRequiredService<ILogger<TImplementation>>(); |
| | 0 | 83 | | var httpClient = factory.CreateClient(httpClientName); |
| | 0 | 84 | | |
| | 0 | 85 | | return (TInterface)Activator.CreateInstance(typeof(TImplementation), httpClient, jsRuntime, logger)!; |
| | 0 | 86 | | }); |
| | | 87 | | |
| | 0 | 88 | | 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 | | { |
| | 0 | 103 | | services.AddScoped<TImplementation>(sp => |
| | 0 | 104 | | { |
| | 0 | 105 | | var factory = sp.GetRequiredService<IHttpClientFactory>(); |
| | 0 | 106 | | var logger = sp.GetRequiredService<ILogger<TImplementation>>(); |
| | 0 | 107 | | var httpClient = factory.CreateClient(httpClientName); |
| | 0 | 108 | | |
| | 0 | 109 | | return (TImplementation)Activator.CreateInstance(typeof(TImplementation), httpClient, logger)!; |
| | 0 | 110 | | }); |
| | | 111 | | |
| | 0 | 112 | | return services; |
| | | 113 | | } |
| | | 114 | | } |