Skip to content

Apply options pattern for database connection strings to Chapter 3#213

Closed
Copilot wants to merge 11 commits intomainfrom
copilot/fix-b3f326a1-c320-410c-bfab-470271514e39
Closed

Apply options pattern for database connection strings to Chapter 3#213
Copilot wants to merge 11 commits intomainfrom
copilot/fix-b3f326a1-c320-410c-bfab-470271514e39

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Sep 3, 2025

This PR applies the options pattern for database connection strings to Chapter 3, following the same approach implemented in PR #212 for Chapter 2. The changes improve type safety and follow .NET configuration best practices by replacing direct configuration string access with strongly-typed options classes.

Changes Made

1. Created DatabaseOptions Classes

Added DatabaseOptions classes for each service module:

  • Fitnet.Contracts.Infrastructure/Database/DatabaseOptions.cs
  • Fitnet.Passes.DataAccess/Database/DatabaseOptions.cs
  • Fitnet.Offers.DataAccess/Database/DatabaseOptions.cs
  • Fitnet.Reports/DataAccess/DatabaseOptions.cs

2. Updated Database Modules

Replaced direct GetConnectionString() calls with the options pattern:

Before:

var connectionString = configuration.GetConnectionString("Contracts");
services.AddDbContext<ContractsPersistence>(options => options.UseNpgsql(connectionString));

After:

services.Configure<DatabaseOptions>(options => configuration.GetSection("Database").Bind(options));
services.AddDbContext<ContractsPersistence>((serviceProvider, options) =>
{
    var databaseOptions = serviceProvider.GetRequiredService<IOptions<DatabaseOptions>>();
    options.UseNpgsql(databaseOptions.Value.ConnectionString);
});

3. Updated DatabaseConnectionFactory

Modified the Reports module's DatabaseConnectionFactory to use IOptions<DatabaseOptions> instead of directly accessing configuration.

4. Updated Configuration Structure

Changed configuration files from the flat structure:

"ConnectionStrings": {
  "Contracts": "connection_string_here"
}

To the structured options pattern:

"Database": {
  "ConnectionString": "connection_string_here"
}

5. Updated Integration Test Configurations

Updated all *DatabaseConfiguration.cs files in integration tests to use the new configuration structure.

Benefits

  • Type Safety: Eliminates magic strings for connection string names
  • Configuration Validation: Strongly-typed options enable better validation
  • Consistency: Aligns Chapter 3 with Chapter 2's implementation approach
  • Maintainability: Centralized database configuration per service
  • Best Practices: Follows .NET's recommended configuration patterns

Testing

All integration test configurations have been updated to work with the new options pattern. The changes maintain backward compatibility for the database functionality while improving the configuration management approach.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits September 3, 2025 19:44
Co-authored-by: kamilbaczek <74410956+kamilbaczek@users.noreply.github.com>
Co-authored-by: kamilbaczek <74410956+kamilbaczek@users.noreply.github.com>
Copilot AI changed the title [WIP] Apply Options Pattern for Database Connection Strings in Chapter 3 Apply options pattern for database connection strings to Chapter 3 Sep 3, 2025
Copilot AI requested a review from kamilbaczek September 3, 2025 19:47
@kamilbaczek kamilbaczek marked this pull request as ready for review September 28, 2025 09:29
@kamilbaczek kamilbaczek deleted the copilot/fix-b3f326a1-c320-410c-bfab-470271514e39 branch January 27, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants