Skip to content

Commit 9c175bd

Browse files
author
Vadim Belov
committed
Add overload for AddPostgresDbContext with impl type
Added a new AddPostgresDbContext<TContext, TImplementation> overload to ServiceCollectionExtensions.cs. This allows registration of a DbContext with separate interface and implementation types, supports configuration via PostgresOptionsBuilder and DbContextOptionsBuilder, and can add a design-time DbContext factory for the implementation type. Includes XML documentation and usage example.
1 parent b726538 commit 9c175bd

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,51 @@ public static IServiceCollection AddPostgresDbContext<TContext>(this IServiceCol
5353
return services;
5454
}
5555

56+
/// <summary>
57+
/// Adds a <see cref="DbContext"/> to the <see cref="IServiceCollection"/> resolving <see cref="IConfiguration"/> from DI.
58+
/// Builds the connection string from the configured section (default: "DatabaseSettings") and/or prefixed keys.
59+
/// </summary>
60+
/// <typeparam name="TContext"> The type of <see cref="DbContext"/> to add. </typeparam>
61+
/// <typeparam name="TImplementation"> The implementation type of <see cref="DbContext"/> to add. </typeparam>
62+
/// <param name="services"> The <see cref="IServiceCollection"/> instance. </param>
63+
/// <param name="setup"> Optional action to configure the <see cref="PostgresOptionsBuilder"/> (section name, prefix, pool size, etc). </param>
64+
/// <param name="setupContextOptions"> Optional action to configure the <see cref="DbContextOptionsBuilder"/> (e.g. enable sensitive data logging). </param>
65+
/// <returns> Current <see cref="IServiceCollection"/> instance. </returns>
66+
/// <exception cref="KeyNotFoundException"> When required database settings are missing. </exception>
67+
/// <example>
68+
/// <code>
69+
/// builder.Services.AddPostgresDbContext&lt;MyDbContext&gt;(f =&gt; { f.ConfigurationSection = "Db"; });
70+
/// </code>
71+
/// </example>
72+
public static IServiceCollection AddPostgresDbContext<TContext, TImplementation>(
73+
this IServiceCollection services,
74+
Action<PostgresOptionsBuilder>? setup = null,
75+
Action<DbContextOptionsBuilder>? setupContextOptions = null)
76+
where TContext : DbContext
77+
where TImplementation : TContext
78+
{
79+
PostgresOptionsBuilder contextFactory = new();
80+
setup?.Invoke(contextFactory);
81+
82+
services.AddDbContext<TContext, TImplementation>((sp, builder) =>
83+
{
84+
var configuration = sp.GetRequiredService<IConfiguration>();
85+
string connectionString = BuildConnectionString(configuration, contextFactory);
86+
builder.UseNpgsql(connectionString);
87+
setupContextOptions?.Invoke(builder);
88+
if (contextFactory.UseLazyLoadingProxies)
89+
{
90+
builder.UseLazyLoadingProxies();
91+
}
92+
}, contextLifetime: contextFactory.ContextLifetime);
93+
94+
if (contextFactory.AddDesignTimeDbContextFactory)
95+
{
96+
services.AddScoped<IDesignTimeDbContextFactory<TImplementation>, DesignTimeDbContextFactory<TImplementation>>();
97+
}
98+
return services;
99+
}
100+
56101
private static string BuildConnectionString(IConfiguration configuration, PostgresOptionsBuilder contextFactory)
57102
{
58103
bool isDevelopment = GetIsDevelopment(configuration);

0 commit comments

Comments
 (0)