From 6cbccd3d7dd8308bd6e65fd28362f36c61dedd5e Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 5 Jul 2024 20:03:12 -0400 Subject: [PATCH] feat: add data access DI helper --- .../DbConnectionFactory.cs | 15 ++++++-------- .../FruityFoundation.DataAccess.Core.csproj | 1 + .../ServiceCollectionExtensions.cs | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 FruityFoundation.DataAccess.Core/ServiceCollectionExtensions.cs 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(); + } +}