Skip to content

.Net MEVD: DI for relational DBs #12043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ public sealed class PostgresCollectionOptions
{
internal static readonly PostgresCollectionOptions Default = new();

/// <summary>
/// Initializes a new instance of the <see cref="PostgresCollectionOptions"/> class.
/// </summary>
public PostgresCollectionOptions()
{
}

internal PostgresCollectionOptions(PostgresCollectionOptions? source)
{
this.Schema = source?.Schema ?? PostgresVectorStoreOptions.Default.Schema;
this.VectorStoreRecordDefinition = source?.VectorStoreRecordDefinition;
this.EmbeddingGenerator = source?.EmbeddingGenerator;
}

/// <summary>
/// Gets or sets the database schema.
/// </summary>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ public sealed class PostgresVectorStoreOptions
{
internal static readonly PostgresVectorStoreOptions Default = new();

/// <summary>
/// Initializes a new instance of the <see cref="PostgresVectorStoreOptions"/> class.
/// </summary>
public PostgresVectorStoreOptions()
{
}

internal PostgresVectorStoreOptions(PostgresVectorStoreOptions? source)
{
this.Schema = source?.Schema ?? Default.Schema;
this.EmbeddingGenerator = source?.EmbeddingGenerator;
}

/// <summary>
/// Gets or sets the database schema.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public SqlServerCollectionOptions()
{
}

internal SqlServerCollectionOptions(SqlServerCollectionOptions? source, IEmbeddingGenerator embeddingGenerator)
internal SqlServerCollectionOptions(SqlServerCollectionOptions? source)
{
this.Schema = source?.Schema;
this.RecordDefinition = source?.RecordDefinition;
this.EmbeddingGenerator = embeddingGenerator;
this.EmbeddingGenerator = source?.EmbeddingGenerator;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,15 @@ public static class SqlServerServiceCollectionExtensions
/// <summary>
/// Registers a <see cref="SqlServerVectorStore"/> as <see cref="VectorStore"/>, with the specified connection string and service lifetime.
/// </summary>
/// <inheritdoc cref="AddVectorStore"/>
/// <inheritdoc cref="AddKeyedSqlServerVectorStore"/>
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddSqlServerVectorStore(
this IServiceCollection services,
Func<IServiceProvider, string> connectionStringProvider,
Func<IServiceProvider, SqlServerVectorStoreOptions>? optionsProvider = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
=> AddVectorStore(services, serviceKey: null, connectionStringProvider, optionsProvider, lifetime);

/// <inheritdoc cref="AddVectorStore"/>
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddKeyedSqlServerVectorStore(
this IServiceCollection services,
object serviceKey,
Func<IServiceProvider, string> connectionStringProvider,
Func<IServiceProvider, SqlServerVectorStoreOptions>? optionsProvider = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
Verify.NotNull(serviceKey);

return AddVectorStore(services, serviceKey, connectionStringProvider, optionsProvider, lifetime);
}
=> AddKeyedSqlServerVectorStore(services, serviceKey: null, connectionStringProvider, optionsProvider, lifetime);

/// <summary>
/// Registers a keyed <see cref="SqlServerVectorStore"/> as <see cref="VectorStore"/>, with the specified connection string and service lifetime.
Expand All @@ -51,12 +36,14 @@ public static IServiceCollection AddKeyedSqlServerVectorStore(
/// <param name="optionsProvider">Options provider to further configure the vector store.</param>
/// <param name="lifetime">The service lifetime for the store. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param>
/// <returns>The service collection.</returns>
private static IServiceCollection AddVectorStore(
IServiceCollection services,
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddKeyedSqlServerVectorStore(
this IServiceCollection services,
object? serviceKey,
Func<IServiceProvider, string> connectionStringProvider,
Func<IServiceProvider, SqlServerVectorStoreOptions>? optionsProvider,
ServiceLifetime lifetime)
Func<IServiceProvider, SqlServerVectorStoreOptions>? optionsProvider = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
Verify.NotNull(services);
Verify.NotNull(connectionStringProvider);
Expand All @@ -77,66 +64,50 @@ private static IServiceCollection AddVectorStore(
/// <summary>
/// Registers a <see cref="SqlServerCollection{TKey, TRecord}"/> as <see cref="VectorStoreCollection{TKey, TRecord}"/>, with the specified connection string and service lifetime.
/// </summary>
/// <inheritdoc cref="AddCollection{TKey, TRecord}(IServiceCollection, object?, string, Func{IServiceProvider, string}, Func{IServiceProvider, SqlServerCollectionOptions}?, ServiceLifetime)"/>
/// <inheritdoc cref="AddKeyedSqlServerCollection{TKey, TRecord}(IServiceCollection, object?, string, Func{IServiceProvider, string}, Func{IServiceProvider, SqlServerCollectionOptions}?, ServiceLifetime)"/>
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddSqlServerCollection<TKey, TRecord>(
this IServiceCollection services,
string collectionName,
Func<IServiceProvider, string> connectionStringProvider,
Func<IServiceProvider, SqlServerCollectionOptions>? optionsProvider = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TKey : notnull
where TRecord : class
=> AddCollection<TKey, TRecord>(services, serviceKey: null, collectionName, connectionStringProvider, optionsProvider, lifetime);

/// <inheritdoc cref="AddCollection{TKey, TRecord}(IServiceCollection, object?, string, Func{IServiceProvider, string}, Func{IServiceProvider, SqlServerCollectionOptions}?, ServiceLifetime)"/>
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddKeyedSqlServerCollection<TKey, TRecord>(
this IServiceCollection services,
object serviceKey,
string collectionName,
string name,
Func<IServiceProvider, string> connectionStringProvider,
Func<IServiceProvider, SqlServerCollectionOptions>? optionsProvider = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TKey : notnull
where TRecord : class
{
Verify.NotNull(serviceKey);

return AddCollection<TKey, TRecord>(services, serviceKey, collectionName, connectionStringProvider, optionsProvider, lifetime);
}
=> AddKeyedSqlServerCollection<TKey, TRecord>(services, serviceKey: null, name, connectionStringProvider, optionsProvider, lifetime);

/// <summary>
/// Registers a keyed <see cref="SqlServerCollection{TKey, TRecord}"/> as <see cref="VectorStoreCollection{TKey, TRecord}"/>, with the specified connection string and service lifetime.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="VectorStoreCollection{TKey, TRecord}"/> on.</param>
/// <param name="serviceKey">The key with which to associate the collection.</param>
/// <param name="collectionName">The name of the collection.</param>
/// <param name="name">The name of the collection.</param>
/// <param name="connectionStringProvider">The connection string provider.</param>
/// <param name="optionsProvider">Options provider to further configure the collection.</param>
/// <param name="lifetime">The service lifetime for the store. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param>
/// <returns>The service collection.</returns>
private static IServiceCollection AddCollection<TKey, TRecord>(
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddKeyedSqlServerCollection<TKey, TRecord>(
this IServiceCollection services,
object? serviceKey,
string collectionName,
string name,
Func<IServiceProvider, string> connectionStringProvider,
Func<IServiceProvider, SqlServerCollectionOptions?>? optionsProvider = null,
Func<IServiceProvider, SqlServerCollectionOptions>? optionsProvider = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TKey : notnull
where TRecord : class
{
Verify.NotNull(services);
Verify.NotNullOrWhiteSpace(collectionName);
Verify.NotNullOrWhiteSpace(name);
Verify.NotNull(connectionStringProvider);

services.Add(new ServiceDescriptor(typeof(SqlServerCollection<TKey, TRecord>), serviceKey, (sp, _) =>
{
var connectionString = connectionStringProvider(sp);
var options = GetCollectionOptions(sp, optionsProvider);
return new SqlServerCollection<TKey, TRecord>(connectionString, collectionName, options);
return new SqlServerCollection<TKey, TRecord>(connectionString, name, options);
}, lifetime));

services.Add(new ServiceDescriptor(typeof(VectorStoreCollection<TKey, TRecord>), serviceKey,
Expand All @@ -154,51 +125,35 @@ private static IServiceCollection AddCollection<TKey, TRecord>(
/// <summary>
/// Registers a <see cref="SqlServerCollection{TKey, TRecord}"/> as <see cref="VectorStoreCollection{TKey, TRecord}"/>, with the specified connection string and service lifetime.
/// </summary>
/// <inheritdoc cref="AddCollection{TKey, TRecord}(IServiceCollection, object?, string, string, SqlServerCollectionOptions?, ServiceLifetime)"/>/>
/// <inheritdoc cref="AddKeyedSqlServerCollection{TKey, TRecord}(IServiceCollection, object?, string, string, SqlServerCollectionOptions?, ServiceLifetime)"/>/>
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddSqlServerCollection<TKey, TRecord>(
this IServiceCollection services,
string collectionName,
string name,
string connectionString,
SqlServerCollectionOptions? options = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TKey : notnull
where TRecord : class
=> AddCollection<TKey, TRecord>(services, serviceKey: null, collectionName, connectionString, options, lifetime);

/// <inheritdoc cref="AddCollection{TKey, TRecord}(IServiceCollection, object?, string, string, SqlServerCollectionOptions?, ServiceLifetime)"/>/>
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddKeyedSqlServerCollection<TKey, TRecord>(
this IServiceCollection services,
object serviceKey,
string collectionName,
string connectionString,
SqlServerCollectionOptions? options = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TKey : notnull
where TRecord : class
{
Verify.NotNull(serviceKey);

return AddCollection<TKey, TRecord>(services, serviceKey, collectionName, connectionString, options, lifetime);
}
=> AddKeyedSqlServerCollection<TKey, TRecord>(services, serviceKey: null, name, connectionString, options, lifetime);

/// <summary>
/// Registers a keyed <see cref="SqlServerCollection{TKey, TRecord}"/> as <see cref="VectorStoreCollection{TKey, TRecord}"/>, with the specified connection string and service lifetime.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to register the <see cref="VectorStoreCollection{TKey, TRecord}"/> on.</param>
/// <param name="serviceKey">The key with which to associate the collection.</param>
/// <param name="collectionName">The name of the collection.</param>
/// <param name="name">The name of the collection.</param>
/// <param name="connectionString">The connection string.</param>
/// <param name="options">Options to further configure the collection.</param>
/// <param name="lifetime">The service lifetime for the store. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param>
/// <returns>The service collection.</returns>
private static IServiceCollection AddCollection<TKey, TRecord>(
[RequiresUnreferencedCode("The SQL Server provider is currently incompatible with trimming.")]
[RequiresDynamicCode("The SQL Server provider is currently incompatible with NativeAOT.")]
public static IServiceCollection AddKeyedSqlServerCollection<TKey, TRecord>(
this IServiceCollection services,
object? serviceKey,
string collectionName,
string name,
string connectionString,
SqlServerCollectionOptions? options = null,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
Expand All @@ -207,7 +162,7 @@ private static IServiceCollection AddCollection<TKey, TRecord>(
{
Verify.NotNullOrWhiteSpace(connectionString);

return AddCollection<TKey, TRecord>(services, serviceKey, collectionName, _ => connectionString, _ => options, lifetime);
return AddKeyedSqlServerCollection<TKey, TRecord>(services, serviceKey, name, _ => connectionString, _ => options!, lifetime);
}

private static SqlServerVectorStoreOptions? GetStoreOptions(IServiceProvider sp, Func<IServiceProvider, SqlServerVectorStoreOptions?>? optionsProvider)
Expand All @@ -221,7 +176,7 @@ private static IServiceCollection AddCollection<TKey, TRecord>(
var embeddingGenerator = sp.GetService<IEmbeddingGenerator>();
return embeddingGenerator is null
? options // There is nothing to change.
: new(options, embeddingGenerator); // Create a brand new copy in order to avoid modifying the original options.
: new(options) { EmbeddingGenerator = embeddingGenerator }; // Create a brand new copy in order to avoid modifying the original options.
}

private static SqlServerCollectionOptions? GetCollectionOptions(IServiceProvider sp, Func<IServiceProvider, SqlServerCollectionOptions?>? optionsProvider)
Expand All @@ -235,6 +190,6 @@ private static IServiceCollection AddCollection<TKey, TRecord>(
var embeddingGenerator = sp.GetService<IEmbeddingGenerator>();
return embeddingGenerator is null
? options // There is nothing to change.
: new(options, embeddingGenerator); // Create a brand new copy in order to avoid modifying the original options.
: new(options) { EmbeddingGenerator = embeddingGenerator }; // Create a brand new copy in order to avoid modifying the original options.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public SqlServerVectorStoreOptions()
{
}

internal SqlServerVectorStoreOptions(SqlServerVectorStoreOptions? source, IEmbeddingGenerator embeddingGenerator)
internal SqlServerVectorStoreOptions(SqlServerVectorStoreOptions? source)
{
this.Schema = source?.Schema;
this.EmbeddingGenerator = embeddingGenerator;
this.EmbeddingGenerator = source?.EmbeddingGenerator;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ public sealed class SqliteCollectionOptions
{
internal static readonly SqliteCollectionOptions Default = new();

/// <summary>
/// Initializes a new instance of the <see cref="SqliteCollectionOptions"/> class.
/// </summary>
public SqliteCollectionOptions()
{
}

internal SqliteCollectionOptions(SqliteCollectionOptions? source)
{
this.VectorStoreRecordDefinition = source?.VectorStoreRecordDefinition;
this.VectorVirtualTableName = source?.VectorVirtualTableName;
this.VectorSearchExtensionName = source?.VectorSearchExtensionName;
this.EmbeddingGenerator = source?.EmbeddingGenerator;
}

/// <summary>
/// Gets or sets an optional record definition that defines the schema of the record type.
/// </summary>
Expand Down
Loading
Loading