tomasfabian / ksqlDB.RestApi.Client-DotNet

ksqlDb.RestApi.Client is a C# LINQ-enabled client API for issuing and consuming ksqlDB push and pull queries and executing statements.
MIT License
97 stars 26 forks source link

How can I use the modelbuilder when IKSqlDbRestApiClient is injected from DI? #88

Closed mrt181 closed 1 month ago

mrt181 commented 1 month ago

I have an asp.net minimal API service that wires KSqlDbRestApiClient using the below config in Program.cs

  builder.Services.ConfigureKSqlDb(
    builder.Configuration.GetConnectionString("KafkaConnection")!,
    parameters =>
    {
      parameters
        .SetAutoOffsetReset(AutoOffsetReset.Latest)
        .SetIdentifierEscaping(IdentifierEscaping.Always)
        .SetJsonSerializerOptions(options =>
        {
          options.PropertyNameCaseInsensitive = true;
        }
    }
  );

I would like to use ModelBuilder to configure my entities but the only documented way I see is to provide the builder when creating a new KSqlDbRestApiClient. Is there a way to build my model using the ModelBuilder and add it to the config for any instance that is created?

tomasfabian commented 1 month ago

Hi @mrt181, could you please confirm if the solution below works for you?

using ksqlDb.RestApi.Client.DependencyInjection;
using ksqlDb.RestApi.Client.FluentAPI.Builders;
using ksqlDB.RestApi.Client.KSql.Query.Options;
using ksqlDB.RestApi.Client.KSql.RestApi.Enums;

var builder = WebApplication.CreateBuilder(args);

ModelBuilder modelBuilder = new();

builder.Services.ConfigureKSqlDb(
  builder.Configuration.GetConnectionString("KafkaConnection")!,
  parameters =>
  {
    parameters
      .SetAutoOffsetReset(AutoOffsetReset.Latest)
      .SetIdentifierEscaping(IdentifierEscaping.Always)
      .SetJsonSerializerOptions(options =>
      {
        options.PropertyNameCaseInsensitive = true;
      });
  }
).AddSingleton(modelBuilder);
mrt181 commented 1 month ago

Yes, that works.