mirror of https://github.com/abpframework/abp.git
committed by
GitHub
34 changed files with 434 additions and 28 deletions
@ -0,0 +1,15 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.DependencyInjection |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] |
|||
public class ReplaceDbContextAttribute : Attribute |
|||
{ |
|||
public Type[] ReplacedDbContextTypes { get; } |
|||
|
|||
public ReplaceDbContextAttribute(params Type[] replacedDbContextTypes) |
|||
{ |
|||
ReplacedDbContextTypes = replacedDbContextTypes; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore.TestApp.ThirdDbContext; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.TestApp.FourthContext |
|||
{ |
|||
/* This dbcontext is just for testing to replace dbcontext from the application using ReplaceDbContextAttribute |
|||
*/ |
|||
public class FourthDbContext : AbpDbContext<FourthDbContext>, IFourthDbContext |
|||
{ |
|||
public DbSet<FourthDbContextDummyEntity> FourthDummyEntities { get; set; } |
|||
|
|||
public FourthDbContext(DbContextOptions<FourthDbContext> options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.TestApp.FourthContext |
|||
{ |
|||
public class FourthDbContextDummyEntity : AggregateRoot<Guid> |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.TestApp.FourthContext |
|||
{ |
|||
public interface IFourthDbContext : IEfCoreDbContext |
|||
{ |
|||
DbSet<FourthDbContextDummyEntity> FourthDummyEntities { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.test.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>net5.0</TargetFramework> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<PreserveCompilationContext>true</PreserveCompilationContext> |
|||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
|||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> |
|||
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.MongoDB\Volo.Abp.MongoDB.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,13 @@ |
|||
using MongoDB.Driver; |
|||
using Volo.Abp.EntityFrameworkCore.TestApp.FourthContext; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.FourthContext |
|||
{ |
|||
/* This dbcontext is just for testing to replace dbcontext from the application using ReplaceDbContextAttribute |
|||
*/ |
|||
public class FourthDbContext : AbpMongoDbContext, IFourthDbContext |
|||
{ |
|||
public IMongoCollection<FourthDbContextDummyEntity> FourthDummyEntities => Collection<FourthDbContextDummyEntity>(); |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.TestApp.FourthContext |
|||
{ |
|||
public class FourthDbContextDummyEntity : AggregateRoot<Guid> |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using MongoDB.Driver; |
|||
using Volo.Abp.EntityFrameworkCore.TestApp.FourthContext; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.FourthContext |
|||
{ |
|||
public interface IFourthDbContext : IAbpMongoDbContext |
|||
{ |
|||
IMongoCollection<FourthDbContextDummyEntity> FourthDummyEntities { get;} |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.MongoDB.TestApp.FourthContext; |
|||
using Volo.Abp.MongoDB.TestApp.ThirdDbContext; |
|||
using Volo.Abp.Threading; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.SecondContext |
|||
{ |
|||
[DependsOn(typeof(AbpMongoDbModule))] |
|||
public class AbpMongoDbTestSecondContextModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddMongoDbContext<SecondDbContext>(options => |
|||
{ |
|||
options.AddDefaultRepositories(); |
|||
}); |
|||
|
|||
context.Services.AddMongoDbContext<ThirdDbContext.ThirdDbContext>(options => |
|||
{ |
|||
options.AddDefaultRepositories<IThirdDbContext>(); |
|||
}); |
|||
|
|||
context.Services.AddMongoDbContext<FourthDbContext>(options => |
|||
{ |
|||
options.AddDefaultRepositories<IFourthDbContext>(); |
|||
}); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
SeedTestData(context); |
|||
} |
|||
|
|||
private static void SeedTestData(ApplicationInitializationContext context) |
|||
{ |
|||
using (var scope = context.ServiceProvider.CreateScope()) |
|||
{ |
|||
AsyncHelper.RunSync(() => scope.ServiceProvider |
|||
.GetRequiredService<SecondContextTestDataBuilder>() |
|||
.BuildAsync()); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.SecondContext |
|||
{ |
|||
public class BookInSecondDbContext : AggregateRoot<Guid> |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public BookInSecondDbContext() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public BookInSecondDbContext(Guid id, string name) |
|||
: base(id) |
|||
{ |
|||
Name = name; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
using System.ComponentModel.DataAnnotations.Schema; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.SecondContext |
|||
{ |
|||
public class PhoneInSecondDbContext : AggregateRoot |
|||
{ |
|||
public virtual Guid PersonId { get; set; } |
|||
|
|||
public virtual string Number { get; set; } |
|||
|
|||
public override object[] GetKeys() |
|||
{ |
|||
return new object[] {PersonId, Number}; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.Guids; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.SecondContext |
|||
{ |
|||
public class SecondContextTestDataBuilder : ITransientDependency |
|||
{ |
|||
private readonly IBasicRepository<BookInSecondDbContext, Guid> _bookRepository; |
|||
private readonly IGuidGenerator _guidGenerator; |
|||
|
|||
public SecondContextTestDataBuilder(IBasicRepository<BookInSecondDbContext, Guid> bookRepository, IGuidGenerator guidGenerator) |
|||
{ |
|||
_bookRepository = bookRepository; |
|||
_guidGenerator = guidGenerator; |
|||
} |
|||
|
|||
public async Task BuildAsync() |
|||
{ |
|||
await _bookRepository.InsertAsync( |
|||
new BookInSecondDbContext( |
|||
_guidGenerator.Create(), |
|||
"TestBook1" |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using MongoDB.Driver; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.SecondContext |
|||
{ |
|||
public class SecondDbContext : AbpMongoDbContext |
|||
{ |
|||
public IMongoCollection<BookInSecondDbContext> Books => Collection<BookInSecondDbContext>(); |
|||
|
|||
public IMongoCollection<PhoneInSecondDbContext> Phones => Collection<PhoneInSecondDbContext>(); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using MongoDB.Driver; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.ThirdDbContext |
|||
{ |
|||
public interface IThirdDbContext : IAbpMongoDbContext |
|||
{ |
|||
IMongoCollection<ThirdDbContextDummyEntity> DummyEntities { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using MongoDB.Driver; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.ThirdDbContext |
|||
{ |
|||
/* This dbcontext is just for testing to replace dbcontext from the application using AbpDbContextRegistrationOptions.ReplaceDbContext |
|||
*/ |
|||
public class ThirdDbContext : AbpMongoDbContext, IThirdDbContext |
|||
{ |
|||
public IMongoCollection<ThirdDbContextDummyEntity> DummyEntities => Collection<ThirdDbContextDummyEntity>(); |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.MongoDB.TestApp.ThirdDbContext |
|||
{ |
|||
public class ThirdDbContextDummyEntity : AggregateRoot<Guid> |
|||
{ |
|||
public string Value { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Shouldly; |
|||
using Volo.Abp.MongoDB.TestApp.FourthContext; |
|||
using Volo.Abp.MongoDB.TestApp.ThirdDbContext; |
|||
using Volo.Abp.TestApp.MongoDB; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.MongoDB |
|||
{ |
|||
[Collection(MongoTestCollection.Name)] |
|||
public class DbContext_Replace_Tests : MongoDbTestBase |
|||
{ |
|||
private readonly AbpMongoDbContextOptions _options; |
|||
|
|||
public DbContext_Replace_Tests() |
|||
{ |
|||
_options = GetRequiredService<IOptions<AbpMongoDbContextOptions>>().Value; |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Replace_DbContext() |
|||
{ |
|||
_options.GetReplacedTypeOrSelf(typeof(IThirdDbContext)).ShouldBe(typeof(TestAppMongoDbContext)); |
|||
_options.GetReplacedTypeOrSelf(typeof(IFourthDbContext)).ShouldBe(typeof(TestAppMongoDbContext)); |
|||
|
|||
(ServiceProvider.GetRequiredService<IThirdDbContext>() is TestAppMongoDbContext).ShouldBeTrue(); |
|||
(ServiceProvider.GetRequiredService<IFourthDbContext>() is TestAppMongoDbContext).ShouldBeTrue(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue