File tree Expand file tree Collapse file tree
Chapter-1-initial-architecture/Src/Fitnet Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Contracts . Data . Database ;
2+
3+ using System . ComponentModel . DataAnnotations ;
4+
5+ internal sealed class ContractsPersistenceOptions
6+ {
7+ public const string SectionName = "ConnectionStrings" ;
8+
9+ [ Required ] public string Contracts { get ; init ; } = string . Empty ;
10+ }
Original file line number Diff line number Diff line change 11namespace EvolutionaryArchitecture . Fitnet . Contracts . Data . Database ;
22
33using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . Extensions . Options ;
45
56internal static class DatabaseModule
67{
7- private const string ConnectionStringName = "Contracts" ;
8-
98 internal static IServiceCollection AddDatabase ( this IServiceCollection services , IConfiguration configuration )
109 {
11- var connectionString = configuration . GetConnectionString ( ConnectionStringName ) ;
12- services . AddDbContext < ContractsPersistence > ( options => options . UseNpgsql ( connectionString ) ) ;
10+ services . Configure < ContractsPersistenceOptions > (
11+ configuration . GetSection ( ContractsPersistenceOptions . SectionName ) ) ;
12+ services . AddOptionsWithValidateOnStart < ContractsPersistenceOptions > ( ) ;
13+ services . AddDbContext < ContractsPersistence > ( ( serviceProvider , options ) =>
14+ {
15+ var persistenceOptions = serviceProvider . GetRequiredService < IOptions < ContractsPersistenceOptions > > ( ) ;
16+ var connectionString = persistenceOptions . Value . Contracts ;
17+ options . UseNpgsql ( connectionString ) ;
18+ } ) ;
1319
1420 return services ;
1521 }
Original file line number Diff line number Diff line change 11namespace EvolutionaryArchitecture . Fitnet . Offers . Data . Database ;
22
33using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . Extensions . Options ;
45
56internal static class DatabaseModule
67{
7- private const string ConnectionStringName = "Offers" ;
8-
98 internal static IServiceCollection AddDatabase ( this IServiceCollection services , IConfiguration configuration )
109 {
11- var connectionString = configuration . GetConnectionString ( ConnectionStringName ) ;
12- services . AddDbContext < OffersPersistence > ( options => options . UseNpgsql ( connectionString ) ) ;
10+ services . Configure < OffersPersistenceOptions > ( configuration . GetSection ( OffersPersistenceOptions . SectionName ) ) ;
11+ services . AddOptionsWithValidateOnStart < OffersPersistenceOptions > ( ) ;
12+ services . AddDbContext < OffersPersistence > ( ( serviceProvider , options ) =>
13+ {
14+ var persistenceOptions = serviceProvider . GetRequiredService < IOptions < OffersPersistenceOptions > > ( ) ;
15+ var connectionString = persistenceOptions . Value . Offers ;
16+ options . UseNpgsql ( connectionString ) ;
17+ } ) ;
1318
1419 return services ;
1520 }
@@ -20,4 +25,4 @@ internal static IApplicationBuilder UseDatabase(this IApplicationBuilder applica
2025
2126 return applicationBuilder ;
2227 }
23- }
28+ }
Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Offers . Data . Database ;
2+
3+ using System . ComponentModel . DataAnnotations ;
4+
5+ internal sealed class OffersPersistenceOptions
6+ {
7+ public const string SectionName = "ConnectionStrings" ;
8+
9+ [ Required ] public string Offers { get ; init ; } = string . Empty ;
10+ }
Original file line number Diff line number Diff line change 11namespace EvolutionaryArchitecture . Fitnet . Passes . Data . Database ;
22
33using Microsoft . EntityFrameworkCore ;
4+ using Microsoft . Extensions . Options ;
45
56internal static class DatabaseModule
67{
7- private const string ConnectionStringName = "Passes" ;
8-
98 internal static IServiceCollection AddDatabase ( this IServiceCollection services , IConfiguration configuration )
109 {
11- var connectionString = configuration . GetConnectionString ( ConnectionStringName ) ;
12- services . AddDbContext < PassesPersistence > ( options => options . UseNpgsql ( connectionString ) ) ;
10+ services . Configure < PassesPersistenceOptions > ( configuration . GetSection ( PassesPersistenceOptions . SectionName ) ) ;
11+ services . AddOptionsWithValidateOnStart < PassesPersistenceOptions > ( ) ;
12+ services . AddDbContext < PassesPersistence > ( ( serviceProvider , options ) =>
13+ {
14+ var persistenceOptions = serviceProvider . GetRequiredService < IOptions < PassesPersistenceOptions > > ( ) ;
15+ var connectionString = persistenceOptions . Value . Passes ;
16+ options . UseNpgsql ( connectionString ) ;
17+ } ) ;
1318
1419 return services ;
1520 }
Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Passes . Data . Database ;
2+
3+ using System . ComponentModel . DataAnnotations ;
4+
5+ internal sealed class PassesPersistenceOptions
6+ {
7+ public const string SectionName = "ConnectionStrings" ;
8+
9+ [ Required ] public string Passes { get ; init ; } = string . Empty ;
10+ }
Original file line number Diff line number Diff line change 1818builder . Services . AddRequestsValidations ( ) ;
1919builder . Services . AddClock ( ) ;
2020
21+ // Add modules - each module registers its own options with validation
2122builder . Services . AddPasses ( builder . Configuration ) ;
2223builder . Services . AddContracts ( builder . Configuration ) ;
2324builder . Services . AddOffers ( builder . Configuration ) ;
24- builder . Services . AddReports ( ) ;
25+ builder . Services . AddReports ( builder . Configuration ) ;
2526
2627await using var app = builder . Build ( ) ;
2728
Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Reports . DataAccess ;
2+
3+ internal static class DataAccessModule
4+ {
5+ internal static IServiceCollection AddDataAccess ( this IServiceCollection services , IConfiguration configuration )
6+ {
7+ services . Configure < ReportsPersistenceOptions > ( configuration . GetSection ( ReportsPersistenceOptions . SectionName ) ) ;
8+ services . AddOptionsWithValidateOnStart < ReportsPersistenceOptions > ( ) ;
9+ services . AddScoped < IDatabaseConnectionFactory , DatabaseConnectionFactory > ( ) ;
10+
11+ return services ;
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ namespace EvolutionaryArchitecture . Fitnet . Reports . DataAccess ;
2+
3+ using System . ComponentModel . DataAnnotations ;
4+
5+ internal sealed class ReportsPersistenceOptions
6+ {
7+ public const string SectionName = "ConnectionStrings" ;
8+
9+ [ Required ] public string Reports { get ; init ; } = string . Empty ;
10+ }
Original file line number Diff line number Diff line change @@ -5,9 +5,9 @@ namespace EvolutionaryArchitecture.Fitnet.Reports;
55
66internal static class ReportsModule
77{
8- internal static IServiceCollection AddReports ( this IServiceCollection services )
8+ internal static IServiceCollection AddReports ( this IServiceCollection services , IConfiguration configuration )
99 {
10- services . AddDataAccess ( ) ;
10+ services . AddDataAccess ( configuration ) ;
1111 services . AddNewPassesRegistrationsPerMonthReport ( ) ;
1212
1313 return services ;
You can’t perform that action at this time.
0 commit comments