Apply options pattern for database connection strings to Chapter 4 #221
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR applies the options pattern for database connection strings from Chapter 3 to Chapter 4, addressing issue #213. The changes ensure consistency across chapters while respecting the architectural differences between a shared-database modular monolith (Chapter 3) and a database-per-module architecture (Chapter 4).
Problem
Chapter 4 was using direct
GetConnectionString()calls to retrieve database connection strings:This approach differs from Chapter 3's options pattern, which provides better testability, type safety, and follows .NET best practices.
Solution
Applied the options pattern consistently across all four modules (Contracts, Offers, Passes, Reports):
1. Created
DatabaseOptionsclass for each module2. Updated database modules to use
IOptions<DatabaseOptions>Contracts, Offers, and Passes (Entity Framework):
Reports (Dapper):
3. Updated configuration structure
Before:
{ "ConnectionStrings": { "Passes": "", "Reports": "", "Offers": "" } }After:
{ "Passes": { "Database": { "ConnectionString": "" } }, "Reports": { ... }, "Offers": { ... } }Architectural Differences
The implementation respects the key architectural difference between chapters:
"Database") - typical for a modular monolith with shared database"Passes:Database","Offers:Database", etc.) - supports database-per-module architecturePattern Consistency with Chapter 3
The implementation matches Chapter 3's patterns exactly:
.Bind(options)when configuring optionsGetSection()without.Bind()(both approaches work; consistency maintained)Benefits
DatabaseOptionsclassIOptions<T>Files Changed
DatabaseOptions.csfiles (one per module)DatabaseModule.csor equivalent filesReportsModule.csto pass configuration to data access layerFixes #213
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.