1- using EasyExtensions . EntityFrameworkCore . Npgsql . Builders ;
1+ using EasyExtensions . Abstractions ;
2+ using EasyExtensions . EntityFrameworkCore . Npgsql . Builders ;
23using EasyExtensions . EntityFrameworkCore . Npgsql . Migrations ;
4+ using EasyExtensions . EntityFrameworkCore . Npgsql . Providers ;
35using Microsoft . EntityFrameworkCore ;
46using Microsoft . EntityFrameworkCore . Design ;
57using Microsoft . Extensions . Configuration ;
68using Microsoft . Extensions . DependencyInjection ;
7- using Npgsql ;
89
910namespace EasyExtensions . EntityFrameworkCore . Npgsql . Extensions
1011{
@@ -28,17 +29,24 @@ public static class ServiceCollectionExtensions
2829 /// builder.Services.AddPostgresDbContext<MyDbContext>(f => { f.ConfigurationSection = "Db"; });
2930 /// </code>
3031 /// </example>
31- public static IServiceCollection AddPostgresDbContext < TContext > ( this IServiceCollection services ,
32- Action < PostgresOptionsBuilder > ? setup = null , Action < DbContextOptionsBuilder > ? setupContextOptions = null ) where TContext : DbContext
32+ public static IServiceCollection AddPostgresDbContext < TContext > (
33+ this IServiceCollection services ,
34+ Action < PostgresOptionsBuilder > ? setup = null ,
35+ Action < DbContextOptionsBuilder > ? setupContextOptions = null )
36+ where TContext : DbContext
3337 {
3438 PostgresOptionsBuilder contextFactory = new ( ) ;
3539 setup ? . Invoke ( contextFactory ) ;
3640
37- services . AddDbContext < TContext > ( ( sp , builder ) =>
41+ services . AddSingleton < IPostgresConnectionStringProvider > ( sp =>
3842 {
3943 var configuration = sp . GetRequiredService < IConfiguration > ( ) ;
40- string connectionString = BuildConnectionString ( configuration , contextFactory ) ;
41- builder . UseNpgsql ( connectionString ) ;
44+ return new PostgresConnectionStringProvider ( configuration , contextFactory ) ;
45+ } ) ;
46+ services . AddDbContext < TContext > ( ( sp , builder ) =>
47+ {
48+ var conStringProvider = sp . GetRequiredService < IPostgresConnectionStringProvider > ( ) ;
49+ builder . UseNpgsql ( conStringProvider . GetConnectionString ( ) ) ;
4250 setupContextOptions ? . Invoke ( builder ) ;
4351 if ( contextFactory . UseLazyLoadingProxies )
4452 {
@@ -79,11 +87,15 @@ public static IServiceCollection AddPostgresDbContext<TContext, TImplementation>
7987 PostgresOptionsBuilder contextFactory = new ( ) ;
8088 setup ? . Invoke ( contextFactory ) ;
8189
82- services . AddDbContext < TContext , TImplementation > ( ( sp , builder ) =>
90+ services . AddSingleton < IPostgresConnectionStringProvider > ( sp =>
8391 {
8492 var configuration = sp . GetRequiredService < IConfiguration > ( ) ;
85- string connectionString = BuildConnectionString ( configuration , contextFactory ) ;
86- builder . UseNpgsql ( connectionString ) ;
93+ return new PostgresConnectionStringProvider ( configuration , contextFactory ) ;
94+ } ) ;
95+ services . AddDbContext < TContext , TImplementation > ( ( sp , builder ) =>
96+ {
97+ var conStringProvider = sp . GetRequiredService < IPostgresConnectionStringProvider > ( ) ;
98+ builder . UseNpgsql ( conStringProvider . GetConnectionString ( ) ) ;
8799 setupContextOptions ? . Invoke ( builder ) ;
88100 if ( contextFactory . UseLazyLoadingProxies )
89101 {
@@ -97,80 +109,5 @@ public static IServiceCollection AddPostgresDbContext<TContext, TImplementation>
97109 }
98110 return services ;
99111 }
100-
101- private static string BuildConnectionString ( IConfiguration configuration , PostgresOptionsBuilder contextFactory )
102- {
103- bool isDevelopment = GetIsDevelopment ( configuration ) ;
104- var settings = configuration . GetSection ( contextFactory . ConfigurationSection ) ;
105- string host = GetSetting ( settings , "Host" , configuration , contextFactory ) ;
106- string portStr = GetSetting ( settings , "Port" , configuration , contextFactory ) ;
107- string username = GetSetting ( settings , "Username" , configuration , contextFactory ) ;
108- string password = GetSetting ( settings , "Password" , configuration , contextFactory ) ;
109- string database = GetSetting ( settings , "Database" , configuration , contextFactory ) ;
110- if ( isDevelopment )
111- {
112- database = TryGetSetting ( settings , "DatabaseDev" , configuration , contextFactory ) ?? database ;
113- }
114- NpgsqlConnectionStringBuilder builder = new ( )
115- {
116- Host = host ,
117- Username = username ,
118- Password = password ,
119- Database = database ,
120- Port = ushort . Parse ( portStr ) ,
121- Timezone = contextFactory . Timezone ,
122- Encoding = contextFactory . Encoding ,
123- Timeout = contextFactory . TimeoutSeconds ,
124- MaxPoolSize = contextFactory . MaxPoolSize ,
125- CommandTimeout = contextFactory . TimeoutSeconds ,
126- IncludeErrorDetail = contextFactory . IncludeErrorDetail ,
127- } ;
128- contextFactory . SetupConnectionString ? . Invoke ( builder ) ;
129- return builder . ConnectionString ;
130- }
131-
132- private static string ? TryGetSetting ( IConfigurationSection settings , string key , IConfiguration configuration , PostgresOptionsBuilder contextFactory )
133- {
134- if ( configuration [ contextFactory . ConfigurationPrefix + key ] is string value )
135- {
136- return value ;
137- }
138- if ( ! settings . Exists ( ) )
139- {
140- return null ;
141- }
142- return settings [ key ] ;
143- }
144-
145- private static string GetSetting ( IConfigurationSection settings ,
146- string key ,
147- IConfiguration configuration ,
148- PostgresOptionsBuilder contextFactory ,
149- string ? defaultValue = null )
150- {
151- if ( configuration [ contextFactory . ConfigurationPrefix + key ] is string value )
152- {
153- return value ;
154- }
155- if ( ! settings . Exists ( ) )
156- {
157- if ( defaultValue is not null )
158- {
159- return defaultValue ;
160- }
161- throw new KeyNotFoundException ( $ "{ contextFactory . ConfigurationSection } section or { contextFactory . ConfigurationPrefix } { key } is not set") ;
162- }
163- return settings [ key ] ?? throw new KeyNotFoundException ( $ "{ settings . Path } :{ key } is not set") ;
164- }
165-
166- private static bool GetIsDevelopment ( IConfiguration configuration )
167- {
168- bool result = ( Environment . GetEnvironmentVariable ( "ENVIRONMENT" ) ?? string . Empty ) == "Development" ;
169- if ( ! result )
170- {
171- result = ( configuration [ "ASPNETCORE_ENVIRONMENT" ] ?? string . Empty ) == "Development" ;
172- }
173- return result ;
174- }
175112 }
176113}
0 commit comments