Skip to content

Conversation

@adamsitnik
Copy link
Member

@adamsitnik adamsitnik commented May 6, 2025

It's a resurrected and updated #11594.

Please keep in mind that the main point is establishing a pattern that is going to be applied to all other connectors:

  • we extend IServiceCollection services by creating a new static class called '{$ConnectorName}ServiceCollectionExtensions', part of the Microsoft.SemanticKernel namespace
  • we provide extension methods to register both the vector store and the collection: Add{$ConnectorName}VectorStore and Add{$ConnectorName}Collection. And a Keyed variant with AddKeyed prefix.
  • for both the vector store and the collection, we register factory methods for the concrete type and try to register them for the abstractions as well (in case user uses multiple connectors, they are all registered, but only the first one can be resolved via the abstraction)
  • ServiceLifetime.Singleton is the default, but users can customize it
  • We provide overloads that take Func which accepts IServiceProvider to obtain every setting (to be able to read from the config or resolve from the DI)
  • We provide convenience overloads for things that are expected to be frequently used like string connectionString

fixes #10948

@adamsitnik adamsitnik requested review from roji and westey-m May 6, 2025 15:08
@adamsitnik adamsitnik requested a review from a team as a code owner May 6, 2025 15:08
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel memory labels May 6, 2025
@github-actions github-actions bot changed the title SqlServer dependency injection extension methods .Net: SqlServer dependency injection extension methods May 6, 2025

protected abstract void RegisterVectorStore(IServiceCollection services, ServiceLifetime lifetime, object? serviceKey = null);

protected abstract void RegisterCollection(IServiceCollection services, ServiceLifetime lifetime, string collectionName = "name", object? serviceKey = null);
Copy link
Member Author

Choose a reason for hiding this comment

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

@roji regarding your comment about exposing a list of delegates from https://github.com/microsoft/semantic-kernel/pull/11594/files#r2049050430, I propose to take what I have now and let me shape the final form of the base DI test class in the next PR, once I apply the same pattern to all the connectors and have the full picture

Copy link
Member

Choose a reason for hiding this comment

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

Absolutely no problem to finalize the tests later! I do think we'll ideally want to cover more than just one register method (which is what this allows), but definitely ok for now.

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

@adamsitnik looks great! Here's a first round of feedback.


[Theory]
[MemberData(nameof(LiftetimesAndKeys))]
public virtual void CanRegisterConcreteTypeVectorStoreAfterSomeAbstractionHasBeenRegistered(ServiceLifetime lifetime, object? serviceKey)
Copy link
Member

Choose a reason for hiding this comment

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

I personally very much use snake_case (see filter tests) for test names precisely for this kind of super long test name, which I personally find really hard to read (there's a reason humans put spaces between words 🤣 ).

But definitely not blocking, we're already inconsistent on this - we'll clean up afterwards.

Co-authored-by: Shay Rojansky <roji@roji.org>
- move the extension methods to Microsoft.Extensions.DependencyInjection namespace as they extend IServiceCollection from this namespace
- resolve IEmbeddingGenerator and set it for the option bag
- use Add rather than TryAdd
- add note about HybridSearch
- remove the need to use Hosting
- renames and simplification
@adamsitnik adamsitnik requested review from roji and westey-m May 7, 2025 10:38
- reduce code duplication
- reduce XML docs duplication
- create a copy of the option bag to avoid modifying user provided input
Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

See mainly comment about refactoring out the options copying logic to an internal copy constructors, otherwise LGTM!

/// Registers a <see cref="SqlServerVectorStore"/> as <see cref="VectorStore"/>, with the specified connection string and service lifetime.
/// </summary>
/// <inheritdoc cref="AddVectorStore"/>
public static IServiceCollection AddSqlServerVectorStore(
Copy link
Member

Choose a reason for hiding this comment

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

Just a thought - would be helpful to use #region to group together methods which belong together (e.g. the keyed/non-keyed pair plus the private implementation they delegate to). Not critical.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ahahahaha yeah I thought when I proposed this that it might be a bit... controversial ;)

…ance they are going to get updated when a new property is introduced
@adamsitnik adamsitnik merged commit d5d52f8 into microsoft:feature-vector-data-preb3 May 7, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Issues or pull requests impacting the core kernel memory .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants