@ -22,12 +22,11 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
public ILogger < UnitOfWorkDbContextProvider < TDbContext > > Logger { get ; set ; }
private readonly IUnitOfWorkManager _ unitOfWorkManager ;
private readonly IConnectionStringResolver _ connectionStringResolver ;
private readonly ICancellationTokenProvider _ cancellationTokenProvider ;
private readonly ICurrentTenant _ currentTenant ;
private readonly AbpDbContextOptions _ options ;
private readonly IEfCoreDbContextTypeProvider _ efCoreDbContextTypeProvider ;
protected readonly IUnitOfWorkManager UnitOfWorkManager ;
protected readonly IConnectionStringResolver ConnectionStringResolver ;
protected readonly ICancellationTokenProvider CancellationTokenProvider ;
protected readonly ICurrentTenant CurrentTenant ;
protected readonly IEfCoreDbContextTypeProvider EfCoreDbContextTypeProvider ;
public UnitOfWorkDbContextProvider (
IUnitOfWorkManager unitOfWorkManager ,
@ -36,20 +35,20 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
ICurrentTenant currentTenant ,
IEfCoreDbContextTypeProvider efCoreDbContextTypeProvider )
{
_ u nitOfWorkManager = unitOfWorkManager ;
_ c onnectionStringResolver = connectionStringResolver ;
_ c ancellationTokenProvider = cancellationTokenProvider ;
_ c urrentTenant = currentTenant ;
_ e fCoreDbContextTypeProvider = efCoreDbContextTypeProvider ;
U nitOfWorkManager = unitOfWorkManager ;
C onnectionStringResolver = connectionStringResolver ;
C ancellationTokenProvider = cancellationTokenProvider ;
C urrentTenant = currentTenant ;
E fCoreDbContextTypeProvider = efCoreDbContextTypeProvider ;
Logger = NullLogger < UnitOfWorkDbContextProvider < TDbContext > > . Instance ;
}
[Obsolete("Use GetDbContextAsync method.")]
public TDbContext GetDbContext ( )
public virtual TDbContext GetDbContext ( )
{
if ( UnitOfWork . EnableObsoleteDbContextCreationWarning & &
! UnitOfWorkManager . DisableObsoleteDbContextCreationWarning . Value )
! Uow . U nitOfWorkManager . DisableObsoleteDbContextCreationWarning . Value )
{
Logger . LogWarning (
"UnitOfWorkDbContextProvider.GetDbContext is deprecated. Use GetDbContextAsync instead! " +
@ -59,13 +58,13 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
Logger . LogWarning ( Environment . StackTrace . Truncate ( 2 0 4 8 ) ) ;
}
var unitOfWork = _ u nitOfWorkManager. Current ;
var unitOfWork = U nitOfWorkManager. Current ;
if ( unitOfWork = = null )
{
throw new AbpException ( "A DbContext can only be created inside a unit of work!" ) ;
}
var targetDbContextType = _ e fCoreDbContextTypeProvider. GetDbContextType ( typeof ( TDbContext ) ) ;
var targetDbContextType = E fCoreDbContextTypeProvider. GetDbContextType ( typeof ( TDbContext ) ) ;
var connectionStringName = ConnectionStringNameAttribute . GetConnStringName ( targetDbContextType ) ;
var connectionString = ResolveConnectionString ( connectionStringName ) ;
var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}" ;
@ -79,15 +78,15 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
return ( TDbContext ) ( ( EfCoreDatabaseApi ) databaseApi ) . DbContext ;
}
public async Task < TDbContext > GetDbContextAsync ( )
public virtual async Task < TDbContext > GetDbContextAsync ( )
{
var unitOfWork = _ u nitOfWorkManager. Current ;
var unitOfWork = U nitOfWorkManager. Current ;
if ( unitOfWork = = null )
{
throw new AbpException ( "A DbContext can only be created inside a unit of work!" ) ;
}
var targetDbContextType = await _ e fCoreDbContextTypeProvider. GetDbContextTypeAsync ( typeof ( TDbContext ) ) ;
var targetDbContextType = E fCoreDbContextTypeProvider. GetDbContextType ( typeof ( TDbContext ) ) ;
var connectionStringName = ConnectionStringNameAttribute . GetConnStringName ( targetDbContextType ) ;
var connectionString = await ResolveConnectionStringAsync ( connectionStringName ) ;
@ -108,7 +107,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
}
[Obsolete("Use CreateDbContextAsync method.")]
private TDbContext CreateDbContext ( IUnitOfWork unitOfWork , string connectionStringName , string connectionString )
protected virtual TDbContext CreateDbContext ( IUnitOfWork unitOfWork , string connectionStringName , string connectionString )
{
var creationContext = new DbContextCreationContext ( connectionStringName , connectionString ) ;
using ( DbContextCreationContext . Use ( creationContext ) )
@ -128,7 +127,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
}
}
private async Task < TDbContext > CreateDbContextAsync ( IUnitOfWork unitOfWork , string connectionStringName , string connectionString )
protected virtual async Task < TDbContext > CreateDbContextAsync ( IUnitOfWork unitOfWork , string connectionStringName , string connectionString )
{
var creationContext = new DbContextCreationContext ( connectionStringName , connectionString ) ;
using ( DbContextCreationContext . Use ( creationContext ) )
@ -149,14 +148,14 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
}
[Obsolete("Use CreateDbContextAsync.")]
private TDbContext CreateDbContext ( IUnitOfWork unitOfWork )
protected virtual TDbContext CreateDbContext ( IUnitOfWork unitOfWork )
{
return unitOfWork . Options . IsTransactional
? CreateDbContextWithTransaction ( unitOfWork )
: unitOfWork . ServiceProvider . GetRequiredService < TDbContext > ( ) ;
}
private async Task < TDbContext > CreateDbContextAsync ( IUnitOfWork unitOfWork )
protected virtual async Task < TDbContext > CreateDbContextAsync ( IUnitOfWork unitOfWork )
{
return unitOfWork . Options . IsTransactional
? await CreateDbContextWithTransactionAsync ( unitOfWork )
@ -164,7 +163,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
}
[Obsolete("Use CreateDbContextWithTransactionAsync.")]
private TDbContext CreateDbContextWithTransaction ( IUnitOfWork unitOfWork )
protected virtual TDbContext CreateDbContextWithTransaction ( IUnitOfWork unitOfWork )
{
var transactionApiKey = $"EntityFrameworkCore_{DbContextCreationContext.Current.ConnectionString}" ;
var activeTransaction = unitOfWork . FindTransactionApi ( transactionApiKey ) as EfCoreTransactionApi ;
@ -184,7 +183,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
new EfCoreTransactionApi (
dbtransaction ,
dbContext ,
_ c ancellationTokenProvider
C ancellationTokenProvider
)
) ;
}
@ -259,7 +258,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
}
}
private async Task < TDbContext > CreateDbContextWithTransactionAsync ( IUnitOfWork unitOfWork )
protected virtual async Task < TDbContext > CreateDbContextWithTransactionAsync ( IUnitOfWork unitOfWork )
{
var transactionApiKey = $"EntityFrameworkCore_{DbContextCreationContext.Current.ConnectionString}" ;
var activeTransaction = unitOfWork . FindTransactionApi ( transactionApiKey ) as EfCoreTransactionApi ;
@ -279,7 +278,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
new EfCoreTransactionApi (
dbTransaction ,
dbContext ,
_ c ancellationTokenProvider
C ancellationTokenProvider
)
) ;
}
@ -359,37 +358,37 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
}
}
private async Task < string > ResolveConnectionStringAsync ( string connectionStringName )
protected virtual async Task < string > ResolveConnectionStringAsync ( string connectionStringName )
{
// Multi-tenancy unaware contexts should always use the host connection string
if ( typeof ( TDbContext ) . IsDefined ( typeof ( IgnoreMultiTenancyAttribute ) , false ) )
{
using ( _ c urrentTenant. Change ( null ) )
using ( C urrentTenant. Change ( null ) )
{
return await _ c onnectionStringResolver. ResolveAsync ( connectionStringName ) ;
return await C onnectionStringResolver. ResolveAsync ( connectionStringName ) ;
}
}
return await _ c onnectionStringResolver. ResolveAsync ( connectionStringName ) ;
return await C onnectionStringResolver. ResolveAsync ( connectionStringName ) ;
}
[Obsolete("Use ResolveConnectionStringAsync method.")]
private string ResolveConnectionString ( string connectionStringName )
protected virtual string ResolveConnectionString ( string connectionStringName )
{
// Multi-tenancy unaware contexts should always use the host connection string
if ( typeof ( TDbContext ) . IsDefined ( typeof ( IgnoreMultiTenancyAttribute ) , false ) )
{
using ( _ c urrentTenant. Change ( null ) )
using ( C urrentTenant. Change ( null ) )
{
return _ c onnectionStringResolver. Resolve ( connectionStringName ) ;
return C onnectionStringResolver. Resolve ( connectionStringName ) ;
}
}
return _ c onnectionStringResolver. Resolve ( connectionStringName ) ;
return C onnectionStringResolver. Resolve ( connectionStringName ) ;
}
protected virtual CancellationToken GetCancellationToken ( CancellationToken preferredValue = default )
{
return _ c ancellationTokenProvider. FallbackToProvider ( preferredValue ) ;
return C ancellationTokenProvider. FallbackToProvider ( preferredValue ) ;
}
}