diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EfCoreDbContextTypeProvider.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EfCoreDbContextTypeProvider.cs new file mode 100644 index 0000000000..5d3d6f4208 --- /dev/null +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EfCoreDbContextTypeProvider.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.EntityFrameworkCore; + +public class EfCoreDbContextTypeProvider : IEfCoreDbContextTypeProvider, ITransientDependency +{ + private readonly AbpDbContextOptions _options; + + public EfCoreDbContextTypeProvider(IOptions options) + { + _options = options.Value; + } + + public Type GetDbContextType(Type dbContextType) + { + return _options.GetReplacedTypeOrSelf(dbContextType); + } + + public virtual Task GetDbContextTypeAsync(Type dbContextType) + { + return Task.FromResult(_options.GetReplacedTypeOrSelf(dbContextType)); + } +} diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContextTypeProvider.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContextTypeProvider.cs new file mode 100644 index 0000000000..48f751c415 --- /dev/null +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContextTypeProvider.cs @@ -0,0 +1,11 @@ +using System; +using System.Threading.Tasks; + +namespace Volo.Abp.EntityFrameworkCore; + +public interface IEfCoreDbContextTypeProvider +{ + Type GetDbContextType(Type dbContextType); + + Task GetDbContextTypeAsync(Type dbContextType); +} diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs index 9ea6ffef19..712d29ffdf 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs @@ -19,7 +19,7 @@ public class UnitOfWorkDbContextProvider : IDbContextProvider> Logger { get; set; } private readonly IUnitOfWorkManager _unitOfWorkManager; @@ -27,19 +27,20 @@ public class UnitOfWorkDbContextProvider : IDbContextProvider options) + IEfCoreDbContextTypeProvider efCoreDbContextTypeProvider) { _unitOfWorkManager = unitOfWorkManager; _connectionStringResolver = connectionStringResolver; _cancellationTokenProvider = cancellationTokenProvider; _currentTenant = currentTenant; - _options = options.Value; + _efCoreDbContextTypeProvider = efCoreDbContextTypeProvider; Logger = NullLogger>.Instance; } @@ -64,7 +65,7 @@ public class UnitOfWorkDbContextProvider : IDbContextProvider : IDbContextProvider : IDbContextProvider : IDbContextProvider : IDbContextProvider : IDbContextProvider : IDbContextProvider GetDbContextTypeAsync(Type dbContextType); +} diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoDbContextTypeProvider.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoDbContextTypeProvider.cs new file mode 100644 index 0000000000..e2d99e86b9 --- /dev/null +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoDbContextTypeProvider.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.MongoDB; + +public class MongoDbContextTypeProvider : IMongoDbContextTypeProvider, ITransientDependency +{ + private readonly AbpMongoDbContextOptions _options; + + public MongoDbContextTypeProvider(IOptions options) + { + _options = options.Value; + } + + public Type GetDbContextType(Type dbContextType) + { + return _options.GetReplacedTypeOrSelf(dbContextType); + } + + public virtual Task GetDbContextTypeAsync(Type dbContextType) + { + return Task.FromResult(_options.GetReplacedTypeOrSelf(dbContextType)); + } +} diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs index 2a4b95f6e0..57fe3271a1 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs @@ -25,18 +25,21 @@ public class UnitOfWorkMongoDbContextProvider : IMongoDbContext private readonly ICancellationTokenProvider _cancellationTokenProvider; private readonly ICurrentTenant _currentTenant; private readonly AbpMongoDbContextOptions _options; + private readonly IMongoDbContextTypeProvider _dbContextTypeProvider; public UnitOfWorkMongoDbContextProvider( IUnitOfWorkManager unitOfWorkManager, IConnectionStringResolver connectionStringResolver, ICancellationTokenProvider cancellationTokenProvider, ICurrentTenant currentTenant, - IOptions options) + IOptions options, + IMongoDbContextTypeProvider dbContextTypeProvider) { _unitOfWorkManager = unitOfWorkManager; _connectionStringResolver = connectionStringResolver; _cancellationTokenProvider = cancellationTokenProvider; _currentTenant = currentTenant; + _dbContextTypeProvider = dbContextTypeProvider; _options = options.Value; Logger = NullLogger>.Instance; @@ -63,7 +66,7 @@ public class UnitOfWorkMongoDbContextProvider : IMongoDbContext $"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } - var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TMongoDbContext)); + var targetDbContextType = _dbContextTypeProvider.GetDbContextType(typeof(TMongoDbContext)); var connectionString = ResolveConnectionString(targetDbContextType); var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}"; @@ -91,7 +94,7 @@ public class UnitOfWorkMongoDbContextProvider : IMongoDbContext $"A {nameof(IMongoDatabase)} instance can only be created inside a unit of work!"); } - var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TMongoDbContext)); + var targetDbContextType = await _dbContextTypeProvider.GetDbContextTypeAsync(typeof(TMongoDbContext)); var connectionString = await ResolveConnectionStringAsync(targetDbContextType); var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}"; diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs index 6cd544bb8b..ce26fd4f18 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Shouldly; using Volo.Abp.Domain.Repositories; using Volo.Abp.EntityFrameworkCore.TestApp.FourthContext; @@ -19,7 +18,7 @@ public class DbContext_Replace_Tests : EntityFrameworkCoreTestBase private readonly IBasicRepository _fourthDummyRepository; private readonly IPersonRepository _personRepository; private readonly IUnitOfWorkManager _unitOfWorkManager; - private readonly AbpDbContextOptions _options; + private readonly IEfCoreDbContextTypeProvider _dbContextTypeProvider; public DbContext_Replace_Tests() { @@ -27,14 +26,14 @@ public class DbContext_Replace_Tests : EntityFrameworkCoreTestBase _fourthDummyRepository = GetRequiredService>(); _personRepository = GetRequiredService(); _unitOfWorkManager = GetRequiredService(); - _options = GetRequiredService>().Value; + _dbContextTypeProvider = GetRequiredService(); } [Fact] public async Task Should_Replace_DbContext() { - _options.GetReplacedTypeOrSelf(typeof(IThirdDbContext)).ShouldBe(typeof(TestAppDbContext)); - _options.GetReplacedTypeOrSelf(typeof(IFourthDbContext)).ShouldBe(typeof(TestAppDbContext)); + (await _dbContextTypeProvider.GetDbContextTypeAsync(typeof(IThirdDbContext))).ShouldBe(typeof(TestAppDbContext)); + (await _dbContextTypeProvider.GetDbContextTypeAsync(typeof(IFourthDbContext))).ShouldBe(typeof(TestAppDbContext)); (ServiceProvider.GetRequiredService() is TestAppDbContext).ShouldBeTrue(); (ServiceProvider.GetRequiredService() is TestAppDbContext).ShouldBeTrue(); diff --git a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DbContext_Replace_Tests.cs b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DbContext_Replace_Tests.cs index 8c3b2a6819..3d872135e6 100644 --- a/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DbContext_Replace_Tests.cs +++ b/framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DbContext_Replace_Tests.cs @@ -1,5 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.MongoDB.TestApp.FourthContext; using Volo.Abp.MongoDB.TestApp.ThirdDbContext; @@ -11,18 +11,18 @@ namespace Volo.Abp.MongoDB; [Collection(MongoTestCollection.Name)] public class DbContext_Replace_Tests : MongoDbTestBase { - private readonly AbpMongoDbContextOptions _options; + private readonly IMongoDbContextTypeProvider _dbContextTypeProvider; public DbContext_Replace_Tests() { - _options = GetRequiredService>().Value; + _dbContextTypeProvider = GetRequiredService(); } [Fact] - public void Should_Replace_DbContext() + public async Task Should_Replace_DbContext() { - _options.GetReplacedTypeOrSelf(typeof(IThirdDbContext)).ShouldBe(typeof(TestAppMongoDbContext)); - _options.GetReplacedTypeOrSelf(typeof(IFourthDbContext)).ShouldBe(typeof(TestAppMongoDbContext)); + (await _dbContextTypeProvider.GetDbContextTypeAsync(typeof(IThirdDbContext))).ShouldBe(typeof(TestAppMongoDbContext)); + (await _dbContextTypeProvider.GetDbContextTypeAsync(typeof(IFourthDbContext))).ShouldBe(typeof(TestAppMongoDbContext)); (ServiceProvider.GetRequiredService() is TestAppMongoDbContext).ShouldBeTrue(); (ServiceProvider.GetRequiredService() is TestAppMongoDbContext).ShouldBeTrue();