feat(db): implement options pattern for database connection strings chapter 2#212
Merged
kamilbaczek merged 12 commits intomainfrom Sep 10, 2025
Merged
Conversation
…uraiton-chapter2 # Conflicts: # Chapter-1-initial-architecture/Src/Fitnet/Reports/DataAccess/DatabaseConnectionFactory.cs
There was a problem hiding this comment.
Pull Request Overview
This PR implements the options pattern for database connection strings across multiple modules in the Fitnet application. It replaces direct configuration access with strongly-typed options classes that provide better type safety and validation.
- Introduces
PersistenceOptionsclasses for each module with required connection string properties - Updates database modules to use
IOptions<T>pattern with validation - Adds framework references to enable options validation features
Reviewed Changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| ReportsModule.cs (both chapters) | Updated to pass configuration to data access modules |
| *PersistenceOptions.cs files | New options classes defining connection string configurations |
| DatabaseModule.cs / DatabaseAccessModule.cs files | Refactored to use options pattern instead of direct configuration access |
| DatabaseConnectionFactory.cs | Updated to use options instead of IConfiguration |
| *.csproj files | Added framework references for ASP.NET Core features |
| Program.cs | Added comment explaining module registration pattern |
Comments suppressed due to low confidence (1)
Chapter-2-modules-separation/Src/Reports/Fitnet.Reports/DataAccess/DatabaseConnectionFactory.cs:24
- The DatabaseConnectionFactory is registered as a singleton but maintains mutable state (_connection) without thread synchronization. This could lead to race conditions when multiple threads access Create() simultaneously. Consider adding thread safety mechanisms or changing the registration lifetime.
internal sealed class DatabaseConnectionFactory(IOptions<ReportsPersistenceOptions> persistenceOptions) : IDatabaseConnectionFactory
{
private NpgsqlConnection? _connection;
public IDbConnection Create()
{
if (_connection is { State: ConnectionState.Open })
{
return _connection;
}
var connectionString = persistenceOptions.Value.Primary;
_connection = new NpgsqlConnection(connectionString);
_connection.Open();
return _connection;
}
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.