feat: add data access DI helper
This commit is contained in:
parent
d079f12b16
commit
6cbccd3d7d
|
@ -1,30 +1,27 @@
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using FruityFoundation.DataAccess.Abstractions;
|
using FruityFoundation.DataAccess.Abstractions;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace FruityFoundation.DataAccess.Core;
|
namespace FruityFoundation.DataAccess.Core;
|
||||||
|
|
||||||
public class DbConnectionFactory : IDbConnectionFactory
|
public class DbConnectionFactory : IDbConnectionFactory
|
||||||
{
|
{
|
||||||
private readonly Func<DbConnection> _readWriteConnectionFactory;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
private readonly Func<DbConnection> _readOnlyConnectionFactory;
|
|
||||||
|
|
||||||
public DbConnectionFactory(Func<DbConnection> readWriteConnectionFactory, Func<DbConnection> readOnlyConnectionFactory)
|
public DbConnectionFactory(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_readWriteConnectionFactory = readWriteConnectionFactory;
|
_serviceProvider = serviceProvider;
|
||||||
_readOnlyConnectionFactory = readOnlyConnectionFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public INonTransactionalDbConnection<ReadWrite> CreateConnection()
|
public INonTransactionalDbConnection<ReadWrite> CreateConnection()
|
||||||
{
|
{
|
||||||
var connection = _readWriteConnectionFactory();
|
var nonTxConnection = _serviceProvider.GetRequiredService<INonTransactionalDbConnection<ReadWrite>>();
|
||||||
var nonTxConnection = new NonTransactionalDbConnection<ReadWrite>(connection);
|
|
||||||
return nonTxConnection;
|
return nonTxConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public INonTransactionalDbConnection<ReadOnly> CreateReadOnlyConnection()
|
public INonTransactionalDbConnection<ReadOnly> CreateReadOnlyConnection()
|
||||||
{
|
{
|
||||||
var connection = _readOnlyConnectionFactory();
|
var nonTxConnection = _serviceProvider.GetRequiredService<INonTransactionalDbConnection<ReadOnly>>();
|
||||||
var nonTxConnection = new NonTransactionalDbConnection<ReadOnly>(connection);
|
|
||||||
return nonTxConnection;
|
return nonTxConnection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -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<IServiceProvider, INonTransactionalDbConnection<ReadWrite>> readWriteConnectionImplementationFactory,
|
||||||
|
Func<IServiceProvider, INonTransactionalDbConnection<ReadOnly>> readOnlyConnectionImplementationFactory
|
||||||
|
)
|
||||||
|
{
|
||||||
|
services.AddTransient<INonTransactionalDbConnection<ReadWrite>>(readWriteConnectionImplementationFactory);
|
||||||
|
services.AddTransient<INonTransactionalDbConnection<ReadOnly>>(readOnlyConnectionImplementationFactory);
|
||||||
|
services.AddSingleton<IDbConnectionFactory, DbConnectionFactory>();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user