Browse Source

Add `DbContextTypeProvider`.

pull/15335/head
maliming 3 years ago
parent
commit
fd8a3b6b73
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 26
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EfCoreDbContextTypeProvider.cs
  2. 11
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/IEfCoreDbContextTypeProvider.cs
  3. 23
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs
  4. 11
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDbContextTypeProvider.cs
  5. 26
      framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoDbContextTypeProvider.cs
  6. 9
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs
  7. 9
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/DbContext_Replace_Tests.cs
  8. 14
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/DbContext_Replace_Tests.cs

26
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<AbpDbContextOptions> options)
{
_options = options.Value;
}
public Type GetDbContextType(Type dbContextType)
{
return _options.GetReplacedTypeOrSelf(dbContextType);
}
public virtual Task<Type> GetDbContextTypeAsync(Type dbContextType)
{
return Task.FromResult(_options.GetReplacedTypeOrSelf(dbContextType));
}
}

11
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<Type> GetDbContextTypeAsync(Type dbContextType);
}

23
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Uow/EntityFrameworkCore/UnitOfWorkDbContextProvider.cs

@ -19,7 +19,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
where TDbContext : IEfCoreDbContext
{
private const string TransactionsNotSupportedErrorMessage = "Current database does not support transactions. Your database may remain in an inconsistent state in an error case.";
public ILogger<UnitOfWorkDbContextProvider<TDbContext>> Logger { get; set; }
private readonly IUnitOfWorkManager _unitOfWorkManager;
@ -27,19 +27,20 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
private readonly ICancellationTokenProvider _cancellationTokenProvider;
private readonly ICurrentTenant _currentTenant;
private readonly AbpDbContextOptions _options;
private readonly IEfCoreDbContextTypeProvider _efCoreDbContextTypeProvider;
public UnitOfWorkDbContextProvider(
IUnitOfWorkManager unitOfWorkManager,
IConnectionStringResolver connectionStringResolver,
ICancellationTokenProvider cancellationTokenProvider,
ICurrentTenant currentTenant,
IOptions<AbpDbContextOptions> options)
IEfCoreDbContextTypeProvider efCoreDbContextTypeProvider)
{
_unitOfWorkManager = unitOfWorkManager;
_connectionStringResolver = connectionStringResolver;
_cancellationTokenProvider = cancellationTokenProvider;
_currentTenant = currentTenant;
_options = options.Value;
_efCoreDbContextTypeProvider = efCoreDbContextTypeProvider;
Logger = NullLogger<UnitOfWorkDbContextProvider<TDbContext>>.Instance;
}
@ -64,7 +65,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
throw new AbpException("A DbContext can only be created inside a unit of work!");
}
var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TDbContext));
var targetDbContextType = _efCoreDbContextTypeProvider.GetDbContextType(typeof(TDbContext));
var connectionStringName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType);
var connectionString = ResolveConnectionString(connectionStringName);
var dbContextKey = $"{targetDbContextType.FullName}_{connectionString}";
@ -86,7 +87,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
throw new AbpException("A DbContext can only be created inside a unit of work!");
}
var targetDbContextType = _options.GetReplacedTypeOrSelf(typeof(TDbContext));
var targetDbContextType = await _efCoreDbContextTypeProvider.GetDbContextTypeAsync(typeof(TDbContext));
var connectionStringName = ConnectionStringNameAttribute.GetConnStringName(targetDbContextType);
var connectionString = await ResolveConnectionStringAsync(connectionStringName);
@ -191,10 +192,10 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
return dbContext;
}
else
@ -247,7 +248,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
}
@ -286,7 +287,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
@ -329,7 +330,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
}
@ -347,7 +348,7 @@ public class UnitOfWorkDbContextProvider<TDbContext> : IDbContextProvider<TDbCon
{
Logger.LogError(TransactionsNotSupportedErrorMessage);
Logger.LogException(e);
return dbContext;
}
}

11
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoDbContextTypeProvider.cs

@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;
namespace Volo.Abp.MongoDB;
public interface IMongoDbContextTypeProvider
{
Type GetDbContextType(Type dbContextType);
Task<Type> GetDbContextTypeAsync(Type dbContextType);
}

26
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<AbpMongoDbContextOptions> options)
{
_options = options.Value;
}
public Type GetDbContextType(Type dbContextType)
{
return _options.GetReplacedTypeOrSelf(dbContextType);
}
public virtual Task<Type> GetDbContextTypeAsync(Type dbContextType)
{
return Task.FromResult(_options.GetReplacedTypeOrSelf(dbContextType));
}
}

9
framework/src/Volo.Abp.MongoDB/Volo/Abp/Uow/MongoDB/UnitOfWorkMongoDbContextProvider.cs

@ -25,18 +25,21 @@ public class UnitOfWorkMongoDbContextProvider<TMongoDbContext> : 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<AbpMongoDbContextOptions> options)
IOptions<AbpMongoDbContextOptions> options,
IMongoDbContextTypeProvider dbContextTypeProvider)
{
_unitOfWorkManager = unitOfWorkManager;
_connectionStringResolver = connectionStringResolver;
_cancellationTokenProvider = cancellationTokenProvider;
_currentTenant = currentTenant;
_dbContextTypeProvider = dbContextTypeProvider;
_options = options.Value;
Logger = NullLogger<UnitOfWorkMongoDbContextProvider<TMongoDbContext>>.Instance;
@ -63,7 +66,7 @@ public class UnitOfWorkMongoDbContextProvider<TMongoDbContext> : 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<TMongoDbContext> : 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}";

9
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<FourthDbContextDummyEntity, Guid> _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<IBasicRepository<FourthDbContextDummyEntity, Guid>>();
_personRepository = GetRequiredService<IPersonRepository>();
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_options = GetRequiredService<IOptions<AbpDbContextOptions>>().Value;
_dbContextTypeProvider = GetRequiredService<IEfCoreDbContextTypeProvider>();
}
[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<IThirdDbContext>() is TestAppDbContext).ShouldBeTrue();
(ServiceProvider.GetRequiredService<IFourthDbContext>() is TestAppDbContext).ShouldBeTrue();

14
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<IOptions<AbpMongoDbContextOptions>>().Value;
_dbContextTypeProvider = GetRequiredService<IMongoDbContextTypeProvider>();
}
[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<IThirdDbContext>() is TestAppMongoDbContext).ShouldBeTrue();
(ServiceProvider.GetRequiredService<IFourthDbContext>() is TestAppMongoDbContext).ShouldBeTrue();

Loading…
Cancel
Save