File tree Expand file tree Collapse file tree 18 files changed +70
-33
lines changed
Expand file tree Collapse file tree 18 files changed +70
-33
lines changed Original file line number Diff line number Diff line change @@ -4,16 +4,21 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.Infrastructure.Database;
44using Microsoft . EntityFrameworkCore ;
55using Microsoft . Extensions . Configuration ;
66using Microsoft . Extensions . DependencyInjection ;
7+ using Microsoft . Extensions . Options ;
78using Repositories ;
89
910internal static class DatabaseModule
1011{
11- private const string ConnectionStringName = "Contracts " ;
12+ private const string DatabaseConfigurationSection = "Database " ;
1213
1314 internal static IServiceCollection AddDatabase ( this IServiceCollection services , IConfiguration configuration )
1415 {
15- var connectionString = configuration . GetConnectionString ( ConnectionStringName ) ;
16- services . AddDbContext < ContractsPersistence > ( options => options . UseNpgsql ( connectionString ) ) ;
16+ services . Configure < DatabaseOptions > ( options => configuration . GetSection ( DatabaseConfigurationSection ) . Bind ( options ) ) ;
17+ services . AddDbContext < ContractsPersistence > ( ( serviceProvider , options ) =>
18+ {
19+ var databaseOptions = serviceProvider . GetRequiredService < IOptions < DatabaseOptions > > ( ) ;
20+ options . UseNpgsql ( databaseOptions . Value . ConnectionString ) ;
21+ } ) ;
1722 services . AddRepositories ( ) ;
1823
1924 return services ;
Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Contracts . Infrastructure . Database ;
2+
3+ internal sealed class DatabaseOptions
4+ {
5+ public string ? ConnectionString { get ; init ; }
6+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,6 @@ internal sealed class ContractsDatabaseConfiguration : IDatabaseConfiguration
1010
1111 public Dictionary < string , string ? > Get ( ) => new ( )
1212 {
13- { "ConnectionStrings:Contracts " , _connectionString }
13+ { "Database:ConnectionString " , _connectionString }
1414 } ;
1515}
Original file line number Diff line number Diff line change 88 "FeatureManagement" : {
99 "Contracts" : true ,
1010 },
11- "ConnectionStrings " : {
12- "Contracts " : " Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword"
11+ "Database " : {
12+ "ConnectionString " : " Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword"
1313 },
1414 "EventBus" : {
1515 "Uri" : " rabbitmq" ,
Original file line number Diff line number Diff line change 99 "FeatureManagement" : {
1010 "Contracts" : true ,
1111 },
12- "ConnectionStrings " : {
13- "Contracts " : " "
12+ "Database " : {
13+ "ConnectionString " : " "
1414 },
1515 "ExternalEventBus" : {
1616 "Uri" : " localhost" ,
Original file line number Diff line number Diff line change 1111 "Passes" : true ,
1212 "Reports" : true
1313 },
14- "ConnectionStrings" : {
15- "Passes" : " Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword" ,
16- "Reports" : " Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword" ,
17- "Offers" : " Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword"
14+ "Database" : {
15+ "ConnectionString" : " Host=postgres:5432;Database=fitnet;Username=postgres;Password=mysecretpassword"
1816 },
1917 "EventBus" : {
2018 "Uri" : " rabbitmq" ,
Original file line number Diff line number Diff line change 1717 "Enabled" : true
1818 }
1919 },
20- "ConnectionStrings" : {
21- "Passes" : " " ,
22- "Reports" : " " ,
23- "Offers" : " "
20+ "Database" : {
21+ "ConnectionString" : " "
2422 },
2523 "EventBus" : {
2624 "Uri" : " localhost" ,
Original file line number Diff line number Diff line change @@ -4,15 +4,20 @@ namespace EvolutionaryArchitecture.Fitnet.Offers.DataAccess.Database;
44using Microsoft . EntityFrameworkCore ;
55using Microsoft . Extensions . Configuration ;
66using Microsoft . Extensions . DependencyInjection ;
7+ using Microsoft . Extensions . Options ;
78
89internal static class DatabaseModule
910{
10- private const string ConnectionStringName = "Offers " ;
11+ private const string DatabaseConfigurationSection = "Database " ;
1112
1213 internal static IServiceCollection AddDatabase ( this IServiceCollection services , IConfiguration configuration )
1314 {
14- var connectionString = configuration . GetConnectionString ( ConnectionStringName ) ;
15- services . AddDbContext < OffersPersistence > ( options => options . UseNpgsql ( connectionString ) ) ;
15+ services . Configure < DatabaseOptions > ( _ => configuration . GetSection ( DatabaseConfigurationSection ) ) ;
16+ services . AddDbContext < OffersPersistence > ( ( serviceProvider , options ) =>
17+ {
18+ var databaseOptions = serviceProvider . GetRequiredService < IOptions < DatabaseOptions > > ( ) ;
19+ options . UseNpgsql ( databaseOptions . Value . ConnectionString ) ;
20+ } ) ;
1621
1722 return services ;
1823 }
@@ -23,4 +28,4 @@ internal static IApplicationBuilder UseDatabase(this IApplicationBuilder applica
2328
2429 return applicationBuilder ;
2530 }
26- }
31+ }
Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Offers . DataAccess . Database ;
2+
3+ internal sealed class DatabaseOptions
4+ {
5+ public string ? ConnectionString { get ; init ; }
6+ }
Original file line number Diff line number Diff line change @@ -10,6 +10,6 @@ internal sealed class OffersDatabaseConfiguration : IDatabaseConfiguration
1010
1111 public Dictionary < string , string ? > Get ( ) => new ( )
1212 {
13- { "ConnectionStrings:Offers " , _connectionString }
13+ { "Database:ConnectionString " , _connectionString }
1414 } ;
1515}
You can’t perform that action at this time.
0 commit comments