diff --git a/FruityFoundation.DataAccess.Core/DbConnectionFactory.cs b/FruityFoundation.DataAccess.Core/DbConnectionFactory.cs index 84dd0ca..7a9c3fc 100644 --- a/FruityFoundation.DataAccess.Core/DbConnectionFactory.cs +++ b/FruityFoundation.DataAccess.Core/DbConnectionFactory.cs @@ -1,30 +1,27 @@ using System.Data.Common; using FruityFoundation.DataAccess.Abstractions; +using Microsoft.Extensions.DependencyInjection; namespace FruityFoundation.DataAccess.Core; public class DbConnectionFactory : IDbConnectionFactory { - private readonly Func _readWriteConnectionFactory; - private readonly Func _readOnlyConnectionFactory; + private readonly IServiceProvider _serviceProvider; - public DbConnectionFactory(Func readWriteConnectionFactory, Func readOnlyConnectionFactory) + public DbConnectionFactory(IServiceProvider serviceProvider) { - _readWriteConnectionFactory = readWriteConnectionFactory; - _readOnlyConnectionFactory = readOnlyConnectionFactory; + _serviceProvider = serviceProvider; } public INonTransactionalDbConnection CreateConnection() { - var connection = _readWriteConnectionFactory(); - var nonTxConnection = new NonTransactionalDbConnection(connection); + var nonTxConnection = _serviceProvider.GetRequiredService>(); return nonTxConnection; } public INonTransactionalDbConnection CreateReadOnlyConnection() { - var connection = _readOnlyConnectionFactory(); - var nonTxConnection = new NonTransactionalDbConnection(connection); + var nonTxConnection = _serviceProvider.GetRequiredService>(); return nonTxConnection; } } diff --git a/FruityFoundation.DataAccess.Core/FruityFoundation.DataAccess.Core.csproj b/FruityFoundation.DataAccess.Core/FruityFoundation.DataAccess.Core.csproj index b6cb3c8..5ad6cea 100644 --- a/FruityFoundation.DataAccess.Core/FruityFoundation.DataAccess.Core.csproj +++ b/FruityFoundation.DataAccess.Core/FruityFoundation.DataAccess.Core.csproj @@ -22,6 +22,7 @@ + diff --git a/FruityFoundation.DataAccess.Core/ServiceCollectionExtensions.cs b/FruityFoundation.DataAccess.Core/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..dfa19c1 --- /dev/null +++ b/FruityFoundation.DataAccess.Core/ServiceCollectionExtensions.cs @@ -0,0 +1,20 @@ +using System.Diagnostics.CodeAnalysis; +using FruityFoundation.DataAccess.Abstractions; +using Microsoft.Extensions.DependencyInjection; + +namespace FruityFoundation.DataAccess.Core; + +[ExcludeFromCodeCoverage(Justification = "Dependency injection helpers")] +public static class ServiceCollectionExtensions +{ + public static void AddDataAccessCore( + this IServiceCollection services, + Func> readWriteConnectionImplementationFactory, + Func> readOnlyConnectionImplementationFactory + ) + { + services.AddTransient>(readWriteConnectionImplementationFactory); + services.AddTransient>(readOnlyConnectionImplementationFactory); + services.AddSingleton(); + } +}