Skip to content

Conversation

nefarius
Copy link
Contributor

@nefarius nefarius commented Jul 7, 2025

Added AddCouchContext overloads that supply the service provider to the caller. This allows for code like:

builder.Services.Configure<DatabaseSettings>(builder.Configuration.GetSection(nameof(DatabaseSettings)));

builder.Services.AddCouchContext<DocumentsContext>(sp =>
{
    DatabaseSettings settings = sp.GetRequiredService<IOptions<DatabaseSettings>>().Value;
    CouchOptionsBuilder<DocumentsContext> b = new();
    b.UseEndpoint(settings.ServerUrl);
    b.UseBasicAuthentication(settings.Username, settings.Password);
    return b;
});

So everything stays "within" DI and is consumed by it too.

@matteobortolazzo
Copy link
Owner

Hello! First of all, thanks for the PR!

However, I don't fully understand the goal. In the case of the example, why use DI in the first place when you can do it without? I implemented it like Entity Framework.

var settings = builder.Configuration.GetSection(nameof(DatabaseSettings)).GetValue<DatabaseSettings>();

builder.Services.AddCouchContext<DocumentsContext>(builder => builder
    .UseEndpoint(settings.ServerUrl);
    .UseBasicAuthentication(settings.Username, settings.Password));

Maybe a valid case is a service that resolves the server name or credentials. But then I can be easier to inject that in the context and use it in OnConfiguring probably.

@nefarius
Copy link
Contributor Author

nefarius commented Jul 12, 2025

Yes, it is not the zenith of clever code, I added it because it adheres to how other common DI fluent APIs are designed, like AddHttpClient and so on. My Web app has many more configuration parameters than just the DB so since I need the same info all over the place registering and consuming one IOptions is "cleaner". In my opinion ofc 😜

Ultimately I wanted to eliminate having to pass around configuration variables all across my app entry and keep everything within IoC paradigms, if that makes sense. It's probably very nitpicky but hey, welcome to my world 😁

@matteobortolazzo
Copy link
Owner

matteobortolazzo commented Jul 14, 2025

Got it. Still not sure about 100% to add it to the library, or at least it should be:

Action<IServiceProvider, CouchOptionsBuilder<TContext>>

The builder constructor was not meant to be public tbh.

BTW I see it's 1+ year I don't update this library. Maybe I take a break from my stuff and work on it this week.

I can release a 3.7 version with a few things in from issues and PRs, then check what I can do about System.Text.Json with a v4

@nefarius
Copy link
Contributor Author

The builder constructor was not meant to be public tbh.

Good call! Will modify it accordingly.

BTW I see it's 1+ year I don't update this library. Maybe I take a break from my stuff and work on it this week.

Do you need help with that? I am currently actively back in a project making heavy use of this library so I am still finding things I wanna see fixed 😉

@matteobortolazzo
Copy link
Owner

That would be great!

I can investigate System.Text.Json since it's now commonly used. And I can move to net8 finally

@nefarius
Copy link
Contributor Author

IDK how you prefer to communicate or what your availability is like but be my guest on my Discord if you want the most direct line to me for communication 🫡

@matteobortolazzo
Copy link
Owner

Cool, makes sense, joined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants