Skip to content

Commit

Permalink
[ksqlDB.RestApi.Client]: extracted RegisterEndpointDependencies servi…
Browse files Browse the repository at this point in the history
…ces collection extension from KSqlDBContext
  • Loading branch information
tomasfabian committed Apr 27, 2024
1 parent 2b9d45f commit 6219390
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +86,36 @@ internal static void RegisterKSqlDbContextDependencies(this IServiceCollection s
serviceCollection.TryAddScoped<IKSqlDbRestApiClient, KSqlDbRestApiClient>();
}

internal static void RegisterEndpointDependencies(this IServiceCollection serviceCollection, KSqlDBContextOptions contextOptions)
{
switch (contextOptions.EndpointType)
{
case EndpointType.Query:
serviceCollection.TryAddScoped<IKSqlDbProvider, KSqlDbQueryProvider>();

var queryParameters = new QueryParameters();
queryParameters.FillPushQueryParametersFrom(contextOptions.QueryStreamParameters);
serviceCollection.TryAddSingleton<IKSqlDbParameters>(queryParameters);

var pullQueryParameters = new PullQueryParameters();
pullQueryParameters.FillFrom(contextOptions.PullQueryParameters);
serviceCollection.TryAddSingleton<IPullQueryParameters>(pullQueryParameters);
break;
#if !NETSTANDARD
case EndpointType.QueryStream:
serviceCollection.TryAddScoped<IKSqlDbProvider, KSqlDbQueryStreamProvider>();
serviceCollection.TryAddSingleton<IKSqlDbParameters>(contextOptions.QueryStreamParameters);

var queryStreamParameters = new PullQueryStreamParameters();
queryStreamParameters.FillFrom(contextOptions.PullQueryParameters);
serviceCollection.TryAddSingleton<IPullQueryParameters>(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)
Expand Down
30 changes: 2 additions & 28 deletions ksqlDb.RestApi.Client/KSql/Query/Context/KSqlDBContext.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -73,32 +72,7 @@ protected override void OnConfigureServices(IServiceCollection serviceCollection
{
base.OnConfigureServices(serviceCollection, contextOptions);

switch (contextOptions.EndpointType)
{
case EndpointType.Query:
serviceCollection.TryAddScoped<IKSqlDbProvider, KSqlDbQueryProvider>();

var queryParameters = new QueryParameters();
queryParameters.FillPushQueryParametersFrom(contextOptions.QueryStreamParameters);
serviceCollection.TryAddSingleton<IKSqlDbParameters>(queryParameters);

var pullQueryParameters = new PullQueryParameters();
pullQueryParameters.FillFrom(contextOptions.PullQueryParameters);
serviceCollection.TryAddSingleton<IPullQueryParameters>(pullQueryParameters);
break;
#if !NETSTANDARD
case EndpointType.QueryStream:
serviceCollection.TryAddScoped<IKSqlDbProvider, KSqlDbQueryStreamProvider>();
serviceCollection.TryAddSingleton<IKSqlDbParameters>(contextOptions.QueryStreamParameters);

var queryStreamParameters = new PullQueryStreamParameters();
queryStreamParameters.FillFrom(contextOptions.PullQueryParameters);
serviceCollection.TryAddSingleton<IPullQueryParameters>(queryStreamParameters);
break;
#endif
default:
throw new ArgumentOutOfRangeException(nameof(contextOptions.EndpointType), contextOptions.EndpointType, "Non-exhaustive match");
}
serviceCollection.RegisterEndpointDependencies(contextOptions);
}

/// <summary>
Expand Down

0 comments on commit 6219390

Please sign in to comment.