FruityFoundation/FruityFoundation.DataAccess.Sqlite/ServiceCollectionExtensions.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

37 lines
1.2 KiB
C#

using System.Diagnostics.CodeAnalysis;
using FruityFoundation.DataAccess.Abstractions;
using Microsoft.Extensions.DependencyInjection;
namespace FruityFoundation.DataAccess.Sqlite;
[ExcludeFromCodeCoverage(Justification = "Dependency injection helpers")]
public static class ServiceCollectionExtensions
{
public static void AddSqliteDataAccess(
this IServiceCollection services,
Func<IServiceProvider, string> getReadWriteConnectionString,
Func<IServiceProvider, string> getReadOnlyConnectionString
)
{
services.AddScoped<INonTransactionalDbConnection<ReadWrite>>(serviceProvider =>
{
var connectionFactory = serviceProvider.GetRequiredService<IDbConnectionFactory>();
return connectionFactory.CreateConnection();
});
services.AddScoped<INonTransactionalDbConnection<ReadOnly>>(serviceProvider =>
{
var connectionFactory = serviceProvider.GetRequiredService<IDbConnectionFactory>();
return connectionFactory.CreateReadOnlyConnection();
});
services.AddSingleton<IDbConnectionFactory, SqliteDbConnectionFactory>(serviceProvider =>
new SqliteDbConnectionFactory(
serviceProvider,
getReadOnlyConnectionString: getReadOnlyConnectionString,
getReadWriteConnectionString: getReadWriteConnectionString));
}
}