FruityFoundation/FruityFoundation.DataAccess.Sqlite/SqliteDbConnectionFactory.cs
Kyle 0b4f638cb9
feat: factor connection factory into Sqlite package
BREAKING CHANGE: The previous abstraction was confusing/misleading and was forcefully registering all connections as transient.

These changes invert the relationship a bit and change the injectable INonTransactionalDbConnection<TConnectionType> into scoped. It also allows us to provide an explicit connection factory for Sqlite and an extension method that's easier to use while preserving the very slim nature of the core data access package.
2024-07-11 20:12:07 -04:00

41 lines
1.4 KiB
C#

using FruityFoundation.DataAccess.Abstractions;
using FruityFoundation.DataAccess.Core;
using Microsoft.Data.Sqlite;
namespace FruityFoundation.DataAccess.Sqlite;
public class SqliteDbConnectionFactory : IDbConnectionFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly Func<IServiceProvider, string> _getReadWriteConnectionString;
private readonly Func<IServiceProvider, string> _getReadOnlyConnectionString;
public SqliteDbConnectionFactory(
IServiceProvider serviceProvider,
Func<IServiceProvider, string> getReadWriteConnectionString,
Func<IServiceProvider, string> getReadOnlyConnectionString)
{
_serviceProvider = serviceProvider;
_getReadWriteConnectionString = getReadWriteConnectionString;
_getReadOnlyConnectionString = getReadOnlyConnectionString;
}
/// <inheritdoc />
public INonTransactionalDbConnection<ReadWrite> CreateConnection()
{
var connectionString = _getReadWriteConnectionString(_serviceProvider);
var connection = new SqliteConnection(connectionString);
return new NonTransactionalDbConnection<ReadWrite>(connection);
}
/// <inheritdoc />
public INonTransactionalDbConnection<ReadOnly> CreateReadOnlyConnection()
{
var connectionString = _getReadOnlyConnectionString(_serviceProvider);
var connection = new SqliteConnection(connectionString);
return new NonTransactionalDbConnection<ReadOnly>(connection);
}
}