Skip to content

Commit

Permalink
Add overload with an IServiceProvider parameter. Fixes #1316
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Mar 17, 2024
1 parent e855216 commit 1d508bc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IServiceCollection AddMySqlDataSource(
string connectionString,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton) =>
DoAddMySqlDataSource(serviceCollection, connectionString, dataSourceBuilderAction: null, connectionLifetime, dataSourceLifetime);
DoAddMySqlDataSource(serviceCollection, connectionString, dataSourceBuilderAction: null, connectionLifetime, dataSourceLifetime, builderActionState: null);

/// <summary>
/// Registers a <see cref="MySqlDataSource" /> and a <see cref="MySqlConnection" /> in the <see cref="IServiceCollection" />.
Expand All @@ -40,7 +40,24 @@ public static IServiceCollection AddMySqlDataSource(
Action<MySqlDataSourceBuilder> dataSourceBuilderAction,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton) =>
DoAddMySqlDataSource(serviceCollection, connectionString, dataSourceBuilderAction, connectionLifetime, dataSourceLifetime);
DoAddMySqlDataSource(serviceCollection, connectionString, DataSourceBuilderThunk, connectionLifetime, dataSourceLifetime, builderActionState: dataSourceBuilderAction);

/// <summary>
/// Registers a <see cref="MySqlDataSource" /> and a <see cref="MySqlConnection" /> in the <see cref="IServiceCollection" />.
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="connectionString">A MySQL connection string.</param>
/// <param name="dataSourceBuilderAction">An action to configure the <see cref="MySqlDataSourceBuilder" /> for further customizations of the <see cref="MySqlDataSource" />.</param>
/// <param name="connectionLifetime">The lifetime with which to register the <see cref="MySqlConnection" /> in the container. Defaults to <see cref="ServiceLifetime.Transient" />.</param>
/// <param name="dataSourceLifetime">The lifetime with which to register the <see cref="MySqlDataSource" /> service in the container. Defaults to <see cref="ServiceLifetime.Singleton" />.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddMySqlDataSource(
this IServiceCollection serviceCollection,
string connectionString,
Action<IServiceProvider, MySqlDataSourceBuilder> dataSourceBuilderAction,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton) =>
DoAddMySqlDataSource(serviceCollection, connectionString, ServiceProviderDataSourceBuilderThunk, connectionLifetime, dataSourceLifetime, builderActionState: dataSourceBuilderAction);

/// <summary>
/// Registers a <see cref="MySqlDataSource" /> and a <see cref="MySqlConnection" /> in the <see cref="IServiceCollection" />.
Expand All @@ -58,7 +75,7 @@ public static IServiceCollection AddKeyedMySqlDataSource(
string connectionString,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton) =>
DoAddMySqlDataSource(serviceCollection, serviceKey, connectionString, dataSourceBuilderAction: null, connectionLifetime, dataSourceLifetime);
DoAddMySqlDataSource(serviceCollection, serviceKey, connectionString, dataSourceBuilderAction: null, connectionLifetime, dataSourceLifetime, builderActionState: null);

/// <summary>
/// Registers a <see cref="MySqlDataSource" /> and a <see cref="MySqlConnection" /> in the <see cref="IServiceCollection" />.
Expand All @@ -79,14 +96,36 @@ public static IServiceCollection AddKeyedMySqlDataSource(
Action<MySqlDataSourceBuilder> dataSourceBuilderAction,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton) =>
DoAddMySqlDataSource(serviceCollection, serviceKey, connectionString, dataSourceBuilderAction, connectionLifetime, dataSourceLifetime);
DoAddMySqlDataSource(serviceCollection, serviceKey, connectionString, DataSourceBuilderThunk, connectionLifetime, dataSourceLifetime, builderActionState: dataSourceBuilderAction);

/// <summary>
/// Registers a <see cref="MySqlDataSource" /> and a <see cref="MySqlConnection" /> in the <see cref="IServiceCollection" />.
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="serviceKey">The <see cref="ServiceDescriptor.ServiceKey"/> of the service.</param>
/// <param name="connectionString">A MySQL connection string.</param>
/// <param name="dataSourceBuilderAction">An action to configure the <see cref="MySqlDataSourceBuilder" /> for further customizations of the <see cref="MySqlDataSource" />.</param>
/// <param name="connectionLifetime">The lifetime with which to register the <see cref="MySqlConnection" /> in the container. Defaults to <see cref="ServiceLifetime.Transient" />.</param>
/// <param name="dataSourceLifetime">The lifetime with which to register the <see cref="MySqlDataSource" /> service in the container. Defaults to <see cref="ServiceLifetime.Singleton" />.</param>
/// <returns>The same service collection so that multiple calls can be chained.</returns>
/// <remarks>If the <paramref name="serviceKey"/> is a <see langword="string"/>, it will automatically be used to initialize the data source name; this can
/// be overridden by the <paramref name="dataSourceBuilderAction"/> configuration action.</remarks>
public static IServiceCollection AddKeyedMySqlDataSource(
this IServiceCollection serviceCollection,
object? serviceKey,
string connectionString,
Action<IServiceProvider, MySqlDataSourceBuilder> dataSourceBuilderAction,
ServiceLifetime connectionLifetime = ServiceLifetime.Transient,
ServiceLifetime dataSourceLifetime = ServiceLifetime.Singleton) =>
DoAddMySqlDataSource(serviceCollection, serviceKey, connectionString, ServiceProviderDataSourceBuilderThunk, connectionLifetime, dataSourceLifetime, builderActionState: dataSourceBuilderAction);

private static IServiceCollection DoAddMySqlDataSource(
this IServiceCollection serviceCollection,
string connectionString,
Action<MySqlDataSourceBuilder>? dataSourceBuilderAction,
Action<IServiceProvider, MySqlDataSourceBuilder, object?>? dataSourceBuilderAction,
ServiceLifetime connectionLifetime,
ServiceLifetime dataSourceLifetime)
ServiceLifetime dataSourceLifetime,
object? builderActionState)
{
serviceCollection.TryAdd(
new ServiceDescriptor(
Expand All @@ -95,7 +134,7 @@ private static IServiceCollection DoAddMySqlDataSource(
{
var dataSourceBuilder = new MySqlDataSourceBuilder(connectionString)
.UseLoggerFactory(serviceProvider.GetService<ILoggerFactory>());
dataSourceBuilderAction?.Invoke(dataSourceBuilder);
dataSourceBuilderAction?.Invoke(serviceProvider, dataSourceBuilder, builderActionState);
return dataSourceBuilder.Build();
},
dataSourceLifetime));
Expand All @@ -115,9 +154,10 @@ private static IServiceCollection DoAddMySqlDataSource(
this IServiceCollection serviceCollection,
object? serviceKey,
string connectionString,
Action<MySqlDataSourceBuilder>? dataSourceBuilderAction,
Action<IServiceProvider, MySqlDataSourceBuilder, object?>? dataSourceBuilderAction,
ServiceLifetime connectionLifetime,
ServiceLifetime dataSourceLifetime)
ServiceLifetime dataSourceLifetime,
object? builderActionState)
{
serviceCollection.TryAdd(
new ServiceDescriptor(
Expand All @@ -128,7 +168,7 @@ private static IServiceCollection DoAddMySqlDataSource(
var dataSourceBuilder = new MySqlDataSourceBuilder(connectionString)
.UseLoggerFactory(serviceProvider.GetService<ILoggerFactory>())
.UseName(serviceKey as string);
dataSourceBuilderAction?.Invoke(dataSourceBuilder);
dataSourceBuilderAction?.Invoke(serviceProvider, dataSourceBuilder, builderActionState);
return dataSourceBuilder.Build();
},
dataSourceLifetime));
Expand All @@ -143,4 +183,10 @@ private static IServiceCollection DoAddMySqlDataSource(

return serviceCollection;
}

private static void DataSourceBuilderThunk(IServiceProvider serviceProvider, MySqlDataSourceBuilder dataSourceBuilder, object? state) =>
((Action<MySqlDataSourceBuilder>) state!)(dataSourceBuilder);

private static void ServiceProviderDataSourceBuilderThunk(IServiceProvider serviceProvider, MySqlDataSourceBuilder dataSourceBuilder, object? state) =>
((Action<IServiceProvider, MySqlDataSourceBuilder>) state!)(serviceProvider, dataSourceBuilder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public async Task MySqlDataSourceCanSetName()
Assert.Equal("MyName", dataSource.Name);
}

[Fact]
public async Task MySqlDataSourceCanSetNameFromServiceProvider()
{
var serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton("MyName");
serviceCollection.AddMySqlDataSource(c_connectionString, (sp, builder) =>
{
builder.UseName(sp.GetRequiredService<string>());
});

await using var serviceProvider = serviceCollection.BuildServiceProvider();
var dataSource = serviceProvider.GetRequiredService<MySqlDataSource>();
Assert.Equal("MyName", dataSource.Name);
}

[Fact]
public async Task KeyedMySqlDataSourceIsRegistered()
{
Expand Down Expand Up @@ -98,6 +114,35 @@ public async Task StringKeyedMySqlDataSourceHasNameSet()
Assert.Equal(c_connectionString, connection.ConnectionString);
}

[Fact]
public async Task KeyedMySqlDataSourceCanSetName()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedMySqlDataSource("key", c_connectionString, builder => builder.UseName("MyName"));

await using var serviceProvider = serviceCollection.BuildServiceProvider();

var dataSource = serviceProvider.GetRequiredKeyedService<MySqlDataSource>("key");
Assert.Equal("MyName", dataSource.Name);
await using var connection = dataSource.CreateConnection();
Assert.Equal(c_connectionString, connection.ConnectionString);
}

[Fact]
public async Task KeyedMySqlDataSourceCanSetNameFromServiceProvider()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton("MyName");
serviceCollection.AddKeyedMySqlDataSource("key", c_connectionString, (sp, builder) => builder.UseName(sp.GetRequiredService<string>()));

await using var serviceProvider = serviceCollection.BuildServiceProvider();

var dataSource = serviceProvider.GetRequiredKeyedService<MySqlDataSource>("key");
Assert.Equal("MyName", dataSource.Name);
await using var connection = dataSource.CreateConnection();
Assert.Equal(c_connectionString, connection.ConnectionString);
}

[Fact]
public async Task KeyedMySqlDataSourceRetrievedWithStringKeyHasName()
{
Expand Down

0 comments on commit 1d508bc

Please sign in to comment.