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
94 stars 25 forks source link

Register KSqlDBContext and KSqlDbRestApiClient through DI with custom options #85

Closed fsimonovskiii closed 1 month ago

fsimonovskiii commented 2 months ago

Hello,

I have two questions regarding registering through DI:

  1. If I use the services.ConfigureKSqlDb(config.KSqlDbUrl) to register using dependency injection, I can see that in the context calls I am getting the topic name pluralized, most probably because I didn't specify any options and it gets the default set as true like this

    var contextOptions = new KSqlDBContextOptions(ksqlDbUrl) { ShouldPluralizeFromItemName = true };

    How can I go about registering both the KSqlDbContext and KSqlDbRestApiClient through DI and adding some options along the way while registering them? I saw some example in the config.md file

    serviceCollection.AddDbContext<IKSqlDBContext, KSqlDBContext>(c =>
    {
    c.UseKSqlDb(ksqlDbUrl); //
    
    c.ReplaceHttpClient<IHttpClientFactory, HttpClientFactory>(_ => {}) // do i need to set baseAddress here?
     .ConfigurePrimaryHttpMessageHandler(sp => { ... })
    });

    Does this example register both the KSqlDbContext and the KSqlDbRestApiClient to use the given ksqlDbUrl? I couldn't manage to make it work so that I can provide that I want ShouldPluralizeFromItemName=false. Is this possible through DI or should we make these kind of changes through new() KSqlDbContext all the time, like the following?

    
    var contextOptions = new KSqlDBContextOptions(ksqlDbUrl)
    {
    ShouldPluralizeFromItemName = false
    };

await using var context = new KSqlDBContext(contextOptions);



2. I see in the examples most of the code is written by 'new-ing' up the KSqlDbContext and KSqlDbRestApiClient -- is there a reason for not using DI? My use case is in a API controller so DI would be the best options for that use case personally.

I am using `<PackageReference Include="ksqlDb.RestApi.Client" Version="6.1.0" />`
This if my first time posting, I am still learning and getting to know this package, thank you in advance.
tomasfabian commented 2 months ago

Hello @fsimonovskiii, 1) you can configure ShouldPluralizeFromItemName with dependency injection (DI) in the following way:

serviceCollection.AddDbContext<IKSqlDBContext, KSqlDBContext>(c =>
  c =>
  {
    var setupParameters = c.UseKSqlDb(ksqlDbUrl);
    setupParameters.Options.ShouldPluralizeFromItemName = false;
  });
});

ReplaceHttpClient uses the URL provided in c.UseKSqlDb(ksqlDbUrl); by default, so you don't need to specify the baseAddress. If you don’t need any additional configuration for IHttpClientFactory, using AddDbContext will automatically set up a default HttpClientFactory.

2) Using DI is the preferred approach. The samples use new only for demonstration purposes.

fsimonovskiii commented 2 months ago

Hi @tomasfabian

Thank you for the very quick response, it is working now. Much appreciated.