Skip to content

Conversation

@AdamSendible
Copy link
Contributor

Description

This pull request introduces an enhancement to the NATS DI configuration framework by adopting the options pattern. The changes are designed to improve the readability, maintainability, and flexibility of options configuration in NATS projects.

Key Changes

  • Added Microsoft.Extensions.Options package reference to support options pattern.
  • Introduced NatsOptsBuilder to encapsulate configuration for NatsOpts and connection.
  • Deprecated direct options configuration methods in favor of options pattern using OptionsBuilder<NatsOptsBuilder>.
  • Refactored existing configuration methods to leverage OptionsBuilder for greater clarity and flexibility.
  • Removed manual options retrieval and defaults adjustment logic, instead utilizing dependency injection for IOptions<NatsOptsBuilder>.
  • Updated unit tests to leverage the newly introduced methods

Related Issue

@AdamSendible AdamSendible marked this pull request as ready for review July 1, 2025 09:30
@mtmk mtmk requested a review from Copilot July 2, 2025 10:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modernizes NATS DI configuration by adopting the Microsoft options pattern for NatsOpts, centralizing defaults and deprecating the older direct‐configuration methods.

  • Introduces a new NatsOptsBuilder type and wires it into the DI container via IOptions<NatsOptsBuilder>.
  • Refactors all ConfigureOptions and ConfigureConnection overloads to use an OptionsBuilder<NatsOptsBuilder> approach and marks the old APIs obsolete.
  • Updates the project file to reference Microsoft.Extensions.Options and expands the test suite to cover the new options‐builder patterns and backward compatibility.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/NATS.Extensions.Microsoft.DependencyInjection.Tests/NatsHostingExtensionsTests.cs Added tests for the new ConfigureOptions overloads and options‐builder variants, including backward compatibility checks.
src/NATS.Extensions.Microsoft.DependencyInjection/NatsBuilder.cs Introduced NatsOptsBuilder and rewrote ConfigureOptions/ConfigureConnection to use OptionsBuilder<NatsOptsBuilder>.
src/NATS.Extensions.Microsoft.DependencyInjection/NATS.Extensions.Microsoft.DependencyInjection.csproj Added Microsoft.Extensions.Options package references for .NET6/8 targets.
Comments suppressed due to low confidence (1)

src/NATS.Extensions.Microsoft.DependencyInjection/NatsBuilder.cs:13

  • Add XML documentation for NatsOptsBuilder and its public members to explain their role in capturing and configuring NATS options via the options pattern.
public class NatsOptsBuilder


return optsFactory(serviceProvider, opts);
};
builder(_services.AddOptions<NatsOptsBuilder>());
Copy link

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each call to ConfigureOptions invokes AddOptions<NatsOptsBuilder>() again, registering duplicate configuration actions. Consider storing the initial OptionsBuilder<NatsOptsBuilder> in a private field and reusing it for subsequent Configure calls to avoid redundant registrations.

Suggested change
builder(_services.AddOptions<NatsOptsBuilder>());
builder(_optionsBuilder);

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this should be done build time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ive updated to cache the initial options builder in a field.

private Func<IServiceProvider, int> _poolSizeConfigurer = _ => 1;
private Func<IServiceProvider, NatsOpts, NatsOpts>? _configureOpts;
private Action<IServiceProvider, NatsConnection>? _configureConnection;
private object? _diKey = null;
Copy link

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The private field _diKey is no longer referenced in the builder implementation. Remove it to clean up dead code and reduce maintenance overhead.

Suggested change
private object? _diKey = null;

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must be missing something. this is still used, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is still used. I didn't touch the usage around the DI key as it is a builder concern rather than a service configuration one

};
public NatsBuilder ConfigureConnection(Action<IServiceProvider, NatsConnection> configureConnection) =>
ConfigureOptions(builder => builder.Configure<IServiceProvider>((opts, provider) =>
opts.ConfigureConnection = configureConnection));
Copy link

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This method overwrites any existing ConfigureConnection action rather than chaining multiple calls. If users may call this multiple times, consider merging the new delegate with the existing one to avoid losing previously configured behaviors.

Suggested change
opts.ConfigureConnection = configureConnection));
{
opts.ConfigureConnection = opts.ConfigureConnection == null
? configureConnection
: (sp, connection) =>
{
opts.ConfigureConnection?.Invoke(sp, connection);
configureConnection(sp, connection);
};
}));

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AdamSendible this one has some merit. do you mind if we include this change as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've applied a change to match the existing behaviour

@mtmk
Copy link
Member

mtmk commented Jul 2, 2025

thanks @AdamSendible. looks good. could you GPG sign your commits please? it's a project requirement we need to follow

@AdamSendible AdamSendible force-pushed the use-options-builder-di branch from 80fd6fb to f2d2a83 Compare July 3, 2025 17:24
@AdamSendible AdamSendible force-pushed the use-options-builder-di branch from f2d2a83 to 4b1ece3 Compare July 3, 2025 17:25
@AdamSendible
Copy link
Contributor Author

thanks @AdamSendible. looks good. could you GPG sign your commits please? it's a project requirement we need to follow

That's done

@AdamSendible
Copy link
Contributor Author

Hi @mtmk ,

Just checking in on the status of this PR. Seems that all the checks have passed now. Let me know if you need anything else from my side. Thanks!

@mtmk
Copy link
Member

mtmk commented Jul 16, 2025

thanks for the nudge @AdamSendible, looks good to me actually. @rickdotnet do you have any objections?

@rickdotnet
Copy link
Collaborator

Looks good to me. Thanks @AdamSendible !

Copy link
Member

@mtmk mtmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @AdamSendible

@mtmk mtmk merged commit 4dfb362 into nats-io:main Jul 18, 2025
21 of 22 checks passed
mtmk added a commit that referenced this pull request Jul 18, 2025
* Fix kv wrong last sequence exception (#895)
* add ttl Update/TryUpdate to INatsKvStore (#852)
* [UPDATED] Add NATS Options Pattern DI Support (#888)
@mtmk mtmk mentioned this pull request Jul 18, 2025
mtmk added a commit that referenced this pull request Jul 21, 2025
* Fix kv wrong last sequence exception (#895)
* add ttl Update/TryUpdate to INatsKvStore (#852)
* [UPDATED] Add NATS Options Pattern DI Support (#888)
@mtmk mtmk mentioned this pull request Sep 22, 2025
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.

Use conventional approach for Dependency Injection

3 participants