Abstract Base Database Connection (BaseDatabaseConnection) Class
Namespace: Cxt.Account.Settings.API.DatabaseImplements: IDatabaseConnectionAbstract: Cannot be instantiated directly; base for concrete DB connection implementations.
Provides a foundational, abstract IDatabaseConnection implementation, encapsulating common ADO.NET DbConnection / DbCommand logic provider-agnostically.
Purpose
Reduces boilerplate in concrete DB connection classes (e.g., SqliteDatabaseConnection) by:
- Managing the underlying
DbConnection. - Implementing
IDatabaseConnectionmethods (OpenAsync,CloseAsync,ExecuteNonQueryAsync,ExecuteReaderAsync,DisposeAsync). - Providing parameter addition mechanism for
DbCommand. - Delegating provider-specific
DbCommandcreation to derived classes (via abstractCreateCommand). Facilitates multi-provider DB support (e.g., SQLite, SQL Server) by abstracting common ADO.NET operations.
Key Components
protected readonly DbConnection _connection;- Stores the provider-specific
DbConnection(e.g.,SqliteConnection) supplied by a derived class.
- Stores the provider-specific
Constructor: BaseDatabaseConnection
csharp
protected BaseDatabaseConnection(System.Data.Common.DbConnection connection)- Description: Initializes the
_connectionfield. - Parameters:
Parameter Type Description connectionSystem.Data.Common.DbConnectionThe provider-specific database connection instance. - Throws:
ArgumentNullExceptionifconnectionis null.
Implemented Methods (from IDatabaseConnection)
Open Async
csharp
public async Task OpenAsync(CancellationToken cancellationToken = default)- Description: Delegates to the underlying
_connection.OpenAsync(cancellationToken). - Parameters:
Parameter Type Default Description cancellationTokenCancellationTokendefault For operation cancellation.
Close Async
csharp
public async Task CloseAsync()- Description: Delegates to the underlying
_connection.CloseAsync().
Execute Non Query Async
csharp
public async Task<int> ExecuteNonQueryAsync(string sql, Dictionary<string, object> parameters, CancellationToken cancellationToken = default)- Description: Creates a command, adds parameters, executes it as a non-query, and disposes the command.
- Calls abstract
CreateCommand(sql)(implemented by derived class). - Adds parameters via
AddParameters. - Executes via
command.ExecuteNonQueryAsync(cancellationToken). - Disposes
DbCommandviausing.
- Calls abstract
- Parameters:
Parameter Type Default Description sqlstringSQL command string. parametersDictionary<string, object>SQL parameters dictionary. cancellationTokenCancellationTokendefault For operation cancellation. - Returns:
Task<int>resolving to the number of affected rows.
Execute Reader Async
csharp
public async Task<System.Data.Common.DbDataReader> ExecuteReaderAsync(string sql, Dictionary<string, object> parameters, CancellationToken cancellationToken = default)- Description: Creates a command, adds parameters, and executes it to return a
DbDataReader.- Calls
CreateCommand(sql). - Adds parameters via
AddParameters. - Executes via
command.ExecuteReaderAsync(cancellationToken).
- Calls
- Parameters:
Parameter Type Default Description sqlstringSQL query string. parametersDictionary<string, object>SQL parameters dictionary. cancellationTokenCancellationTokendefault For operation cancellation. - Returns:
Task<System.Data.Common.DbDataReader>resolving to aDbDataReader.
Dispose Async
csharp
public async ValueTask DisposeAsync()- Description: Implements
IAsyncDisposable. Delegates to the underlying_connection.DisposeAsync().
Abstract and Virtual Methods
Create Command (Abstract)
csharp
protected abstract System.Data.Common.DbCommand CreateCommand(string sql);- Description: Must be implemented by derived classes to create a provider-specific
DbCommand(e.g.,SqliteCommand) associated with the current_connection. - Parameters:
Parameter Type Description sqlstringThe SQL string to be used for the command. - Returns:
System.Data.Common.DbCommand- The provider-specific command.
Add Parameters (Virtual)
csharp
protected virtual void AddParameters(System.Data.Common.DbCommand command, Dictionary<string, object> parameters)- Description: Default implementation for adding parameters to a
DbCommand. Iterates through theparametersdictionary, creates aDbParameterfor each, sets itsParameterNameandValue, and adds it to thecommand.Parameterscollection. - Parameters:
Parameter Type Description commandSystem.Data.Common.DbCommandThe command to which parameters will be added. parametersDictionary<string, object>A dictionary of parameter names and their values. - Behavior: This method can be overridden by derived classes for custom parameter handling if needed.
How It Works with Derived Classes
Derived classes (e.g., SqliteDatabaseConnection) extend BaseDatabaseConnection by:
- Supplying a provider-specific
DbConnection(e.g.,new SqliteConnection(connectionString)) to the base constructor. - Implementing
protected abstract DbCommand CreateCommand(string sql)to return a provider-specificDbCommand(e.g.,new SqliteCommand(sql, (SqliteConnection)_connection)).
This promotes code reuse and separates common DB logic from provider-specifics.