-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolved #1051: Should get DbContext which does not define AutoReposi…
…toryTypes attribute if more than one dbcontext
- Loading branch information
Showing
5 changed files
with
174 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Abp.EntityFramework/EntityFramework/IDbContextTypeMatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Abp.EntityFramework | ||
{ | ||
public interface IDbContextTypeMatcher | ||
{ | ||
void Add(Type sourceDbContextType, Type targetDbContextType); | ||
|
||
void Populate(Type[] dbContextTypes); | ||
|
||
Type GetConcreteType(Type sourceDbContextType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/Tests/Abp.EntityFramework.Tests/DbContextTypeMatcher_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using Abp.Domain.Entities; | ||
using Abp.Domain.Repositories; | ||
using Abp.Domain.Uow; | ||
using Abp.EntityFramework.Repositories; | ||
using Abp.MultiTenancy; | ||
using Abp.Tests; | ||
using NSubstitute; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Abp.EntityFramework.Tests | ||
{ | ||
public class DbContextTypeMatcher_Tests : TestBaseWithLocalIocManager | ||
{ | ||
private int? _tenantId = 1; | ||
|
||
private readonly DbContextTypeMatcher matcher; | ||
|
||
public DbContextTypeMatcher_Tests() | ||
{ | ||
var fakeUow = Substitute.For<IUnitOfWork>(); | ||
fakeUow.GetTenantId().Returns(callInfo => _tenantId); | ||
var fakeCurrentUowProvider = Substitute.For<ICurrentUnitOfWorkProvider>(); | ||
fakeCurrentUowProvider.Current.Returns(fakeUow); | ||
|
||
matcher = new DbContextTypeMatcher(fakeCurrentUowProvider); | ||
matcher.Populate(new [] | ||
{ | ||
typeof(MyDerivedDbContext1), | ||
typeof(MyDerivedDbContext2), | ||
typeof(MyDerivedDbContext3) | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_Get_Same_Types_For_Defined_Non_Abstract_Types() | ||
{ | ||
matcher.GetConcreteType(typeof(MyDerivedDbContext1)).ShouldBe(typeof(MyDerivedDbContext1)); | ||
matcher.GetConcreteType(typeof(MyDerivedDbContext2)).ShouldBe(typeof(MyDerivedDbContext2)); | ||
matcher.GetConcreteType(typeof(MyDerivedDbContext3)).ShouldBe(typeof(MyDerivedDbContext3)); | ||
} | ||
|
||
[Fact] | ||
public void Should_Get_Same_Types_For_Undefined_Non_Abstract_Types() | ||
{ | ||
matcher.GetConcreteType(typeof(MyDerivedDbContextNotDefined)).ShouldBe(typeof(MyDerivedDbContextNotDefined)); | ||
} | ||
|
||
[Fact] | ||
public void Should_Get_Single_DbContext_For_Current_Tenancy_Side_When_BaseDbContext_Requested() | ||
{ | ||
//Should return MyDerivedDbContext3 since it defines MultiTenancySides.Tenant | ||
matcher.GetConcreteType(typeof(MyCommonDbContext)).ShouldBe(typeof(MyDerivedDbContext3)); | ||
} | ||
|
||
[Fact] | ||
public void Should_Throw_Exception_If_Multiple_DbContext_For_Current_Tenancy_Side_When_BaseDbContext_Requested() | ||
{ | ||
_tenantId = null; //switching to host side (which have more than 1 dbcontext) | ||
matcher.GetConcreteType(typeof(MyCommonDbContext)).ShouldBe(typeof(MyDerivedDbContext1)); | ||
} | ||
|
||
private abstract class MyCommonDbContext : AbpDbContext | ||
{ | ||
|
||
} | ||
|
||
[MultiTenancySide(MultiTenancySides.Host)] | ||
private class MyDerivedDbContext1 : MyCommonDbContext | ||
{ | ||
|
||
} | ||
|
||
[AutoRepositoryTypes( //Does not matter parameters for these tests | ||
typeof(IRepository<>), | ||
typeof(IRepository<,>), | ||
typeof(EfRepositoryBase<,>), | ||
typeof(EfRepositoryBase<,,>) | ||
)] | ||
[MultiTenancySide(MultiTenancySides.Host)] | ||
private class MyDerivedDbContext2 : MyCommonDbContext | ||
{ | ||
|
||
} | ||
|
||
[MultiTenancySide(MultiTenancySides.Tenant)] | ||
private class MyDerivedDbContext3 : MyCommonDbContext | ||
{ | ||
|
||
} | ||
|
||
private class MyDerivedDbContextNotDefined : MyCommonDbContext | ||
{ | ||
|
||
} | ||
|
||
private class MyCommonEntity : Entity | ||
{ | ||
|
||
} | ||
} | ||
} |