Skip to content

Commit fd793d6

Browse files
committed
Add support for DbContextOptionsBuilder customization
Enhanced `ServiceCollectionExtensions` to invoke a new `ContextOptionsBuilder` action from `PostgresContextFactory` during `AddDbContext` setup. Introduced the `ContextOptionsBuilder` property in `PostgresContextFactory` to allow users to customize `DbContextOptionsBuilder`. Improves flexibility for configuring DbContext options.
1 parent 6cb8a2d commit fd793d6

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

Sources/EasyExtensions.EntityFrameworkCore.Npgsql/Extensions/ServiceCollectionExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public static class ServiceCollectionExtensions
1919
/// </summary>
2020
/// <typeparam name="TContext"> The type of <see cref="DbContext"/> to add. </typeparam>
2121
/// <param name="services"> The <see cref="IServiceCollection"/> instance. </param>
22-
/// <param name="setup"> Optional action to configure the <see cref="PostgresContextFactory"/> (section name, prefix, pool size, etc). </param>
22+
/// <param name="setup"> Optional action to configure the <see cref="PostgresOptionsBuilder"/> (section name, prefix, pool size, etc). </param>
23+
/// <param name="setupContextOptions"> Optional action to configure the <see cref="DbContextOptionsBuilder"/> (e.g. enable sensitive data logging). </param>
2324
/// <returns> Current <see cref="IServiceCollection"/> instance. </returns>
2425
/// <exception cref="KeyNotFoundException"> When required database settings are missing. </exception>
2526
/// <example>
@@ -28,16 +29,17 @@ public static class ServiceCollectionExtensions
2829
/// </code>
2930
/// </example>
3031
public static IServiceCollection AddPostgresDbContext<TContext>(this IServiceCollection services,
31-
Action<PostgresContextFactory>? setup = null) where TContext : DbContext
32+
Action<PostgresOptionsBuilder>? setup = null, Action<DbContextOptionsBuilder>? setupContextOptions = null) where TContext : DbContext
3233
{
33-
PostgresContextFactory contextFactory = new();
34+
PostgresOptionsBuilder contextFactory = new();
3435
setup?.Invoke(contextFactory);
3536

3637
services.AddDbContext<TContext>((sp, builder) =>
3738
{
3839
var configuration = sp.GetRequiredService<IConfiguration>();
3940
string connectionString = BuildConnectionString(configuration, contextFactory);
4041
builder.UseNpgsql(connectionString);
42+
setupContextOptions?.Invoke(builder);
4143
if (contextFactory.UseLazyLoadingProxies)
4244
{
4345
builder.UseLazyLoadingProxies();
@@ -51,7 +53,7 @@ public static IServiceCollection AddPostgresDbContext<TContext>(this IServiceCol
5153
return services;
5254
}
5355

54-
private static string BuildConnectionString(IConfiguration configuration, PostgresContextFactory contextFactory)
56+
private static string BuildConnectionString(IConfiguration configuration, PostgresOptionsBuilder contextFactory)
5557
{
5658
bool isDevelopment = GetIsDevelopment(configuration);
5759
var settings = configuration.GetSection(contextFactory.ConfigurationSection);
@@ -82,7 +84,7 @@ private static string BuildConnectionString(IConfiguration configuration, Postgr
8284
return builder.ConnectionString;
8385
}
8486

85-
private static string? TryGetSetting(IConfigurationSection settings, string key, IConfiguration configuration, PostgresContextFactory contextFactory)
87+
private static string? TryGetSetting(IConfigurationSection settings, string key, IConfiguration configuration, PostgresOptionsBuilder contextFactory)
8688
{
8789
if (configuration[contextFactory.ConfigurationPrefix + key] is string value)
8890
{
@@ -95,7 +97,7 @@ private static string BuildConnectionString(IConfiguration configuration, Postgr
9597
return settings[key];
9698
}
9799

98-
private static string GetSetting(IConfigurationSection settings, string key, IConfiguration configuration, PostgresContextFactory contextFactory)
100+
private static string GetSetting(IConfigurationSection settings, string key, IConfiguration configuration, PostgresOptionsBuilder contextFactory)
99101
{
100102
if (configuration[contextFactory.ConfigurationPrefix + key] is string value)
101103
{

Sources/EasyExtensions.EntityFrameworkCore.Npgsql/Factories/PostgresContextFactory.cs renamed to Sources/EasyExtensions.EntityFrameworkCore.Npgsql/Factories/PostgresOptionsBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace EasyExtensions.EntityFrameworkCore.Npgsql.Factories
99
/// <summary>
1010
/// This class is used to set up the <see cref="DbContext"/> for PostgreSQL with the specified arguments.
1111
/// </summary>
12-
public class PostgresContextFactory
12+
public class PostgresOptionsBuilder
1313
{
1414
/// <summary>
1515
/// The maximum pool size for the database connection, default is 100.

0 commit comments

Comments
 (0)