mirror of https://github.com/abpframework/abp.git
6 changed files with 243 additions and 1 deletions
@ -0,0 +1,56 @@ |
|||
using System; |
|||
using System.Linq.Expressions; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Query.SqlExpressions; |
|||
using Volo.Abp.EntityFrameworkCore.GlobalFilters; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; |
|||
|
|||
public class TenantDependentTranslationTestDbContext : AbpDbContext<TenantDependentTranslationTestDbContext> |
|||
{ |
|||
public DbSet<TenantDependentTranslationTestRow> Rows { get; set; } |
|||
|
|||
public TenantDependentTranslationTestDbContext(DbContextOptions<TenantDependentTranslationTestDbContext> options) |
|||
: base(options) |
|||
{ |
|||
} |
|||
|
|||
protected override void OnModelCreating(ModelBuilder modelBuilder) |
|||
{ |
|||
base.OnModelCreating(modelBuilder); |
|||
modelBuilder.Entity<TenantDependentTranslationTestRow>(builder => |
|||
{ |
|||
builder.ToTable("CustomTranslationRows"); |
|||
builder.Property(row => row.Name).HasMaxLength(100); |
|||
builder.HasQueryFilter(row => MatchesCurrentTenant(row.OwnerTenantId, true)); |
|||
}); |
|||
|
|||
var currentDbContext = this.GetService<AbpEfCoreCurrentDbContext>(); |
|||
modelBuilder.HasDbFunction(typeof(TenantDependentTranslationTestDbContext) |
|||
.GetMethod(nameof(MatchesCurrentTenant))) |
|||
.HasTranslation(arguments => |
|||
{ |
|||
var ownerTenantId = arguments[0]; |
|||
var boolParameter = arguments[1]; |
|||
var currentTenantId = currentDbContext.Context.CurrentTenant.Id; |
|||
|
|||
return new SqlBinaryExpression( |
|||
ExpressionType.Equal, |
|||
ownerTenantId, |
|||
new SqlConstantExpression(currentTenantId, typeof(Guid?), ownerTenantId.TypeMapping), |
|||
boolParameter.Type, |
|||
boolParameter.TypeMapping); |
|||
}); |
|||
} |
|||
|
|||
public static bool MatchesCurrentTenant(Guid? ownerTenantId, bool boolParameter) |
|||
{ |
|||
throw new NotSupportedException(AbpEfCoreDataFilterDbFunctionMethods.NotSupportedExceptionMessage); |
|||
} |
|||
|
|||
public override string GetCompiledQueryCacheKey() |
|||
{ |
|||
return $"{base.GetCompiledQueryCacheKey()}:{CurrentTenantId?.ToString() ?? "Null"}"; |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
using System; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Storage; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.EntityFrameworkCore.Sqlite; |
|||
using Volo.Abp.Modularity; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; |
|||
|
|||
[DependsOn(typeof(AbpEntityFrameworkCoreSqliteModule), typeof(AbpAutofacModule))] |
|||
public class TenantDependentTranslationTestModule : CompiledQueryCacheTestModuleBase |
|||
{ |
|||
public static readonly (string Name, Guid? OwnerTenantId)[] CustomRows = |
|||
{ |
|||
("A-row", TenantA), |
|||
("B-row", TenantB), |
|||
("Host-row", null) |
|||
}; |
|||
|
|||
protected override bool UseDbFunction => true; |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
base.ConfigureServices(context); |
|||
context.Services.AddAbpDbContext<TenantDependentTranslationTestDbContext>(); |
|||
} |
|||
|
|||
public override void OnApplicationInitialization(ApplicationInitializationContext context) |
|||
{ |
|||
base.OnApplicationInitialization(context); |
|||
|
|||
using var scope = context.ServiceProvider.CreateScope(); |
|||
using var uow = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>().Begin(); |
|||
var dbContext = scope.ServiceProvider.GetRequiredService<TenantDependentTranslationTestDbContext>(); |
|||
dbContext.GetService<IRelationalDatabaseCreator>().CreateTables(); |
|||
foreach (var row in CustomRows) |
|||
{ |
|||
dbContext.Database.ExecuteSqlInterpolated( |
|||
$@"INSERT INTO CustomTranslationRows (Id, OwnerTenantId, Name)
|
|||
VALUES ({Guid.NewGuid()}, {row.OwnerTenantId}, {row.Name})");
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; |
|||
|
|||
public class TenantDependentTranslationTestRow : Entity<Guid> |
|||
{ |
|||
public Guid? OwnerTenantId { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
} |
|||
@ -0,0 +1,87 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Metadata; |
|||
using Microsoft.EntityFrameworkCore.Query; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp.EntityFrameworkCore.GlobalFilters; |
|||
using Volo.Abp.MultiTenancy; |
|||
using Volo.Abp.TestApp.Testing; |
|||
using Volo.Abp.Uow; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.EntityFrameworkCore.DataFiltering.CompiledQueryCache; |
|||
|
|||
public class TenantDependentTranslation_Tests : TestAppTestBase<TenantDependentTranslationTestModule> |
|||
{ |
|||
[Theory] |
|||
[InlineData(false, false)] |
|||
[InlineData(false, true)] |
|||
[InlineData(true, false)] |
|||
[InlineData(true, true)] |
|||
public async Task Should_Isolate_Tenants_When_Custom_Translation_Includes_Tenant_In_Cache_Key( |
|||
bool asyncQuery, |
|||
bool hostFirst) |
|||
{ |
|||
var queryTag = Guid.NewGuid().ToString(); |
|||
var counter = GetRequiredService<QueryCompilationCounter>(); |
|||
var before = counter.Count; |
|||
IModel sharedModel = null; |
|||
var cacheKeys = new HashSet<object>(); |
|||
var tenants = hostFirst |
|||
? new Guid?[] |
|||
{ |
|||
null, |
|||
CompiledQueryCacheTestModuleBase.TenantA, |
|||
CompiledQueryCacheTestModuleBase.TenantB, |
|||
null, |
|||
CompiledQueryCacheTestModuleBase.TenantA |
|||
} |
|||
: new Guid?[] |
|||
{ |
|||
CompiledQueryCacheTestModuleBase.TenantA, |
|||
CompiledQueryCacheTestModuleBase.TenantB, |
|||
CompiledQueryCacheTestModuleBase.TenantA, |
|||
null, |
|||
CompiledQueryCacheTestModuleBase.TenantB |
|||
}; |
|||
|
|||
foreach (var tenantId in tenants) |
|||
{ |
|||
using var scope = ServiceProvider.CreateScope(); |
|||
using var tenant = scope.ServiceProvider.GetRequiredService<ICurrentTenant>().Change(tenantId); |
|||
using var uow = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>().Begin(); |
|||
var context = scope.ServiceProvider.GetRequiredService<TenantDependentTranslationTestDbContext>(); |
|||
context.UseDbFunction().ShouldBeTrue(); |
|||
context.CurrentTenant.Id.ShouldBe(tenantId); |
|||
|
|||
sharedModel ??= context.Model; |
|||
context.Model.ShouldBeSameAs(sharedModel); |
|||
|
|||
var query = context.Rows |
|||
.AsNoTracking() |
|||
.TagWith(queryTag) |
|||
.OrderBy(row => row.Name) |
|||
.Select(row => row.Name); |
|||
var cacheKeyGenerator = context.GetService<ICompiledQueryCacheKeyGenerator>(); |
|||
cacheKeyGenerator.ShouldBeOfType<AbpCompiledQueryCacheKeyGenerator>(); |
|||
var cacheKey = cacheKeyGenerator.GenerateCacheKey(query.Expression, asyncQuery); |
|||
cacheKeys.Add(cacheKey); |
|||
var names = asyncQuery ? await query.ToListAsync() : query.ToList(); |
|||
(counter.Count - before).ShouldBe(cacheKeys.Count); |
|||
|
|||
names.ShouldBe(TenantDependentTranslationTestModule.CustomRows |
|||
.Where(row => row.OwnerTenantId == tenantId) |
|||
.Select(row => row.Name) |
|||
.OrderBy(name => name) |
|||
.ToArray()); |
|||
await uow.CompleteAsync(); |
|||
} |
|||
|
|||
cacheKeys.Count.ShouldBe(3); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue