From 6219390a0e27d19722cecb51c01685a9902b2e8a Mon Sep 17 00:00:00 2001 From: Tomas Fabian Date: Sat, 27 Apr 2024 08:46:44 +0200 Subject: [PATCH] [ksqlDB.RestApi.Client]: extracted RegisterEndpointDependencies services collection extension from KSqlDBContext --- .../Extensions/ServiceCollectionExtensions.cs | 33 +++++++++++++++++++ .../KSql/Query/Context/KSqlDBContext.cs | 30 ++--------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/ksqlDb.RestApi.Client/Infrastructure/Extensions/ServiceCollectionExtensions.cs b/ksqlDb.RestApi.Client/Infrastructure/Extensions/ServiceCollectionExtensions.cs index 899704b4..23fa1dc0 100644 --- a/ksqlDb.RestApi.Client/Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/ksqlDb.RestApi.Client/Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -3,8 +3,11 @@ using ksqlDB.RestApi.Client.KSql.Query; using ksqlDB.RestApi.Client.KSql.Query.Context; using ksqlDb.RestApi.Client.KSql.Query.Context.Options; +using ksqlDB.RestApi.Client.KSql.Query.Options; using ksqlDB.RestApi.Client.KSql.RestApi; using ksqlDB.RestApi.Client.KSql.RestApi.Http; +using ksqlDB.RestApi.Client.KSql.RestApi.Parameters; +using ksqlDB.RestApi.Client.KSql.RestApi.Query; using IHttpClientFactory = ksqlDB.RestApi.Client.KSql.RestApi.Http.IHttpClientFactory; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -83,6 +86,36 @@ internal static void RegisterKSqlDbContextDependencies(this IServiceCollection s serviceCollection.TryAddScoped(); } + internal static void RegisterEndpointDependencies(this IServiceCollection serviceCollection, KSqlDBContextOptions contextOptions) + { + switch (contextOptions.EndpointType) + { + case EndpointType.Query: + serviceCollection.TryAddScoped(); + + var queryParameters = new QueryParameters(); + queryParameters.FillPushQueryParametersFrom(contextOptions.QueryStreamParameters); + serviceCollection.TryAddSingleton(queryParameters); + + var pullQueryParameters = new PullQueryParameters(); + pullQueryParameters.FillFrom(contextOptions.PullQueryParameters); + serviceCollection.TryAddSingleton(pullQueryParameters); + break; +#if !NETSTANDARD + case EndpointType.QueryStream: + serviceCollection.TryAddScoped(); + serviceCollection.TryAddSingleton(contextOptions.QueryStreamParameters); + + var queryStreamParameters = new PullQueryStreamParameters(); + queryStreamParameters.FillFrom(contextOptions.PullQueryParameters); + serviceCollection.TryAddSingleton(queryStreamParameters); + break; +#endif + default: + throw new ArgumentOutOfRangeException(nameof(contextOptions.EndpointType), contextOptions.EndpointType, "Non-exhaustive match"); + } + } + internal static void ApplyTo(this IServiceCollection externalServicesCollection, IServiceCollection servicesCollection) { foreach (var service in externalServicesCollection) diff --git a/ksqlDb.RestApi.Client/KSql/Query/Context/KSqlDBContext.cs b/ksqlDb.RestApi.Client/KSql/Query/Context/KSqlDBContext.cs index c35600d0..276e2925 100644 --- a/ksqlDb.RestApi.Client/KSql/Query/Context/KSqlDBContext.cs +++ b/ksqlDb.RestApi.Client/KSql/Query/Context/KSqlDBContext.cs @@ -1,4 +1,5 @@ using ksqlDb.RestApi.Client.FluentAPI.Builders; +using ksqlDB.RestApi.Client.Infrastructure.Extensions; using ksqlDB.RestApi.Client.KSql.Linq; using ksqlDB.RestApi.Client.KSql.Linq.PullQueries; using ksqlDB.RestApi.Client.KSql.Query.Options; @@ -11,8 +12,6 @@ using ksqlDB.RestApi.Client.KSql.RestApi.Statements.Properties; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using ksqlDB.RestApi.Client.KSql.RestApi.Query; -using Microsoft.Extensions.DependencyInjection.Extensions; namespace ksqlDB.RestApi.Client.KSql.Query.Context; @@ -73,32 +72,7 @@ protected override void OnConfigureServices(IServiceCollection serviceCollection { base.OnConfigureServices(serviceCollection, contextOptions); - switch (contextOptions.EndpointType) - { - case EndpointType.Query: - serviceCollection.TryAddScoped(); - - var queryParameters = new QueryParameters(); - queryParameters.FillPushQueryParametersFrom(contextOptions.QueryStreamParameters); - serviceCollection.TryAddSingleton(queryParameters); - - var pullQueryParameters = new PullQueryParameters(); - pullQueryParameters.FillFrom(contextOptions.PullQueryParameters); - serviceCollection.TryAddSingleton(pullQueryParameters); - break; -#if !NETSTANDARD - case EndpointType.QueryStream: - serviceCollection.TryAddScoped(); - serviceCollection.TryAddSingleton(contextOptions.QueryStreamParameters); - - var queryStreamParameters = new PullQueryStreamParameters(); - queryStreamParameters.FillFrom(contextOptions.PullQueryParameters); - serviceCollection.TryAddSingleton(queryStreamParameters); - break; -#endif - default: - throw new ArgumentOutOfRangeException(nameof(contextOptions.EndpointType), contextOptions.EndpointType, "Non-exhaustive match"); - } + serviceCollection.RegisterEndpointDependencies(contextOptions); } ///