using System.Data.Common;
namespace Infrastructure.Sql;
///
/// Base class for ADO.NET-based repositories, providing shared connection creation and
/// entity-mapping infrastructure for derived repository implementations.
///
/// The entity type managed by the repository.
/// The factory used to create database connections.
public abstract class Repository(ISqlConnectionFactory connectionFactory)
where T : class
{
///
/// Creates and opens a new database connection using the configured .
///
/// An open ready for use.
/// Thrown when the connection cannot be opened.
protected async Task CreateConnection()
{
DbConnection connection = connectionFactory.CreateConnection();
await connection.OpenAsync();
return connection;
}
///
/// Maps the current row of a data reader to an instance of .
///
/// The data reader positioned on the row to map.
/// The mapped entity instance.
protected abstract T MapToEntity(DbDataReader reader);
}