Skip to content

Commit 2d0f430

Browse files
committed
add flexibility when registering with connectionString
1 parent 252b9a3 commit 2d0f430

File tree

2 files changed

+53
-65
lines changed

2 files changed

+53
-65
lines changed

src/FluentCommand/DataConfigurationBuilder.cs

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace FluentCommand;
1515
public class DataConfigurationBuilder
1616
{
1717
private readonly IServiceCollection _services;
18-
private string _connectionName;
19-
private string _connectionString;
18+
private string _nameOrConnectionString;
2019
private Type _providerFactoryType;
2120
private Type _dataCacheType;
2221
private Type _queryGeneratorType;
@@ -33,15 +32,15 @@ public DataConfigurationBuilder(IServiceCollection services)
3332

3433

3534
/// <summary>
36-
/// The name of the connection to resolve the connection string from configuration.
35+
/// Set the connection string or the name of connection string located in the application configuration
3736
/// </summary>
38-
/// <param name="connectionName">Name of the connection.</param>
37+
/// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
3938
/// <returns>
4039
/// The same configuration builder so that multiple calls can be chained.
4140
/// </returns>
42-
public DataConfigurationBuilder UseConnectionName(string connectionName)
41+
public DataConfigurationBuilder UseConnectionName(string nameOrConnectionString)
4342
{
44-
_connectionName = connectionName;
43+
_nameOrConnectionString = nameOrConnectionString;
4544
return this;
4645
}
4746

@@ -54,7 +53,7 @@ public DataConfigurationBuilder UseConnectionName(string connectionName)
5453
/// </returns>
5554
public DataConfigurationBuilder UseConnectionString(string connectionString)
5655
{
57-
_connectionString = connectionString;
56+
_nameOrConnectionString = connectionString;
5857
return this;
5958
}
6059

@@ -323,34 +322,18 @@ internal void AddConfiguration()
323322
var queryGenerator = _queryGeneratorType ?? typeof(IQueryGenerator);
324323
var queryLogger = _queryLoggerType ?? typeof(IDataQueryLogger);
325324

326-
if (_connectionName.HasValue())
325+
_services.TryAddSingleton<IDataConfiguration>(sp =>
327326
{
328-
_services.TryAddSingleton<IDataConfiguration>(sp =>
329-
{
330-
var configuration = sp.GetRequiredService<IConfiguration>();
331-
var connectionString = configuration.GetConnectionString(_connectionName);
332-
333-
return new DataConfiguration(
334-
sp.GetRequiredService(providerFactory) as DbProviderFactory,
335-
connectionString,
336-
sp.GetService(dataCache) as IDataCache,
337-
sp.GetService(queryGenerator) as IQueryGenerator,
338-
sp.GetService(queryLogger) as IDataQueryLogger
339-
);
340-
});
341-
}
342-
else
343-
{
344-
_services.TryAddSingleton<IDataConfiguration>(sp =>
345-
new DataConfiguration(
346-
sp.GetRequiredService(providerFactory) as DbProviderFactory,
347-
_connectionString,
348-
sp.GetService(dataCache) as IDataCache,
349-
sp.GetService(queryGenerator) as IQueryGenerator,
350-
sp.GetService(queryLogger) as IDataQueryLogger
351-
)
327+
var connectionString = ResolveConnectionString(sp, _nameOrConnectionString);
328+
329+
return new DataConfiguration(
330+
sp.GetRequiredService(providerFactory) as DbProviderFactory,
331+
connectionString,
332+
sp.GetService(dataCache) as IDataCache,
333+
sp.GetService(queryGenerator) as IQueryGenerator,
334+
sp.GetService(queryLogger) as IDataQueryLogger
352335
);
353-
}
336+
});
354337

355338
_services.TryAddTransient<IDataSessionFactory>(sp => sp.GetService<IDataConfiguration>());
356339
_services.TryAddTransient<IDataSession, DataSession>();
@@ -366,34 +349,18 @@ internal void AddConfiguration<TDiscriminator>()
366349
var queryGenerator = _queryGeneratorType ?? typeof(IQueryGenerator);
367350
var queryLogger = _queryLoggerType ?? typeof(IDataQueryLogger);
368351

369-
if (_connectionName.HasValue())
370-
{
371-
_services.TryAddSingleton<IDataConfiguration<TDiscriminator>>(sp =>
372-
{
373-
var configuration = sp.GetRequiredService<IConfiguration>();
374-
var connectionString = configuration.GetConnectionString(_connectionName);
375-
376-
return new DataConfiguration<TDiscriminator>(
377-
sp.GetRequiredService(providerFactory) as DbProviderFactory,
378-
connectionString,
379-
sp.GetService(dataCache) as IDataCache,
380-
sp.GetService(queryGenerator) as IQueryGenerator,
381-
sp.GetService(queryLogger) as IDataQueryLogger
382-
);
383-
});
384-
}
385-
else
352+
_services.TryAddSingleton<IDataConfiguration<TDiscriminator>>(sp =>
386353
{
387-
_services.TryAddSingleton<IDataConfiguration<TDiscriminator>>(sp =>
388-
new DataConfiguration<TDiscriminator>(
389-
sp.GetRequiredService(providerFactory) as DbProviderFactory,
390-
_connectionString,
391-
sp.GetService(dataCache) as IDataCache,
392-
sp.GetService(queryGenerator) as IQueryGenerator,
393-
sp.GetService(queryLogger) as IDataQueryLogger
394-
)
354+
var connectionString = ResolveConnectionString(sp, _nameOrConnectionString);
355+
356+
return new DataConfiguration<TDiscriminator>(
357+
sp.GetRequiredService(providerFactory) as DbProviderFactory,
358+
connectionString,
359+
sp.GetService(dataCache) as IDataCache,
360+
sp.GetService(queryGenerator) as IQueryGenerator,
361+
sp.GetService(queryLogger) as IDataQueryLogger
395362
);
396-
}
363+
});
397364

398365
_services.TryAddTransient<IDataSessionFactory<TDiscriminator>>(sp => sp.GetService<IDataConfiguration<TDiscriminator>>());
399366
_services.TryAddTransient<IDataSession<TDiscriminator>, DataSession<TDiscriminator>>();
@@ -422,4 +389,25 @@ private void RegisterDefaults()
422389
_services.TryAddSingleton<IDataQueryLogger, DataQueryLogger>();
423390

424391
}
392+
393+
private static string ResolveConnectionString(IServiceProvider serviceProvider, string nameOrConnectionString)
394+
{
395+
var isConnectionString = nameOrConnectionString.IndexOfAny([';', '=']) > 0;
396+
if (isConnectionString)
397+
return nameOrConnectionString;
398+
399+
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
400+
401+
// first try connection strings section
402+
var connectionString = configuration.GetConnectionString(nameOrConnectionString);
403+
if (connectionString.HasValue())
404+
return connectionString;
405+
406+
// next try root collection
407+
connectionString = configuration[nameOrConnectionString];
408+
if (connectionString.HasValue())
409+
return connectionString;
410+
411+
return null;
412+
}
425413
}

src/FluentCommand/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public static class ServiceCollectionExtensions
1111
/// Adds the fluent command services with the specified connection string.
1212
/// </summary>
1313
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
14-
/// <param name="connectionString">The connection string to use.</param>
14+
/// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
1515
/// <returns>
1616
/// The same service collection so that multiple calls can be chained.
1717
/// </returns>
18-
public static IServiceCollection AddFluentCommand(this IServiceCollection services, string connectionString)
18+
public static IServiceCollection AddFluentCommand(this IServiceCollection services, string nameOrConnectionString)
1919
{
20-
services.AddFluentCommand(builder => builder.UseConnectionString(connectionString));
20+
services.AddFluentCommand(builder => builder.UseConnectionName(nameOrConnectionString));
2121

2222
return services;
2323
}
@@ -45,13 +45,13 @@ public static IServiceCollection AddFluentCommand(this IServiceCollection servic
4545
/// </summary>
4646
/// <typeparam name="TDiscriminator">The type of the discriminator.</typeparam>
4747
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
48-
/// <param name="connectionString">The connection string to use.</param>
48+
/// <param name="nameOrConnectionString">The connection string or the name of connection string located in the application configuration.</param>
4949
/// <returns>
5050
/// The same service collection so that multiple calls can be chained.
5151
/// </returns>
52-
public static IServiceCollection AddFluentCommand<TDiscriminator>(this IServiceCollection services, string connectionString)
52+
public static IServiceCollection AddFluentCommand<TDiscriminator>(this IServiceCollection services, string nameOrConnectionString)
5353
{
54-
services.AddFluentCommand<TDiscriminator>(builder => builder.UseConnectionString(connectionString));
54+
services.AddFluentCommand<TDiscriminator>(builder => builder.UseConnectionName(nameOrConnectionString));
5555

5656
return services;
5757
}

0 commit comments

Comments
 (0)