feat: add data access DI helper
This commit is contained in:
parent
d079f12b16
commit
6cbccd3d7d
|
@ -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<DbConnection> _readWriteConnectionFactory;
|
||||
private readonly Func<DbConnection> _readOnlyConnectionFactory;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public DbConnectionFactory(Func<DbConnection> readWriteConnectionFactory, Func<DbConnection> readOnlyConnectionFactory)
|
||||
public DbConnectionFactory(IServiceProvider serviceProvider)
|
||||
{
|
||||
_readWriteConnectionFactory = readWriteConnectionFactory;
|
||||
_readOnlyConnectionFactory = readOnlyConnectionFactory;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public INonTransactionalDbConnection<ReadWrite> CreateConnection()
|
||||
{
|
||||
var connection = _readWriteConnectionFactory();
|
||||
var nonTxConnection = new NonTransactionalDbConnection<ReadWrite>(connection);
|
||||
var nonTxConnection = _serviceProvider.GetRequiredService<INonTransactionalDbConnection<ReadWrite>>();
|
||||
return nonTxConnection;
|
||||
}
|
||||
|
||||
public INonTransactionalDbConnection<ReadOnly> CreateReadOnlyConnection()
|
||||
{
|
||||
var connection = _readOnlyConnectionFactory();
|
||||
var nonTxConnection = new NonTransactionalDbConnection<ReadOnly>(connection);
|
||||
var nonTxConnection = _serviceProvider.GetRequiredService<INonTransactionalDbConnection<ReadOnly>>();
|
||||
return nonTxConnection;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</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