Implemented data filtering for in memory db.

pull/179/head
Halil İbrahim Kalkan 8 years ago
parent f5fd28d413
commit 48aba4e333

@ -61,7 +61,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
protected override IQueryable<TEntity> GetQueryable()
{
return Collection.AsQueryable();
return ApplyDataFilters(Collection.AsQueryable());
}
}
}

@ -5,7 +5,9 @@ using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.Domain.Repositories
{
@ -24,6 +26,10 @@ namespace Volo.Abp.Domain.Repositories
public virtual IQueryProvider Provider => GetQueryable().Provider;
public IDataFilter DataFilter { get; set; }
public ICurrentTenant CurrentTenant { get; set; }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
@ -64,5 +70,21 @@ namespace Volo.Abp.Domain.Repositories
{
return GetQueryable().LongCount();
}
protected virtual IQueryable<TEntity> ApplyDataFilters(IQueryable<TEntity> query)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{
query = query.WhereIf(DataFilter.IsEnabled<ISoftDelete>(), e => ((ISoftDelete)e).IsDeleted == false);
}
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity)))
{
var tenantId = CurrentTenant.Id;
query = query.WhereIf(DataFilter.IsEnabled<IMultiTenant>(), e => ((IMultiTenant)e).TenantId == tenantId);
}
return query;
}
}
}

@ -0,0 +1,57 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.MemoryDb.DataFilters
{
public class MemoryDb_SoftDelete_DataFilter_Tests : MemoryDbTestBase
{
private readonly IQueryableRepository<Person> _personRepository;
private readonly IDataFilter _dataFilter;
public MemoryDb_SoftDelete_DataFilter_Tests()
{
_personRepository = ServiceProvider.GetRequiredService<IQueryableRepository<Person>>();
_dataFilter = GetRequiredService<IDataFilter>();
}
[Fact]
public void Should_Get_Deleted_Entities_When_Filter_Is_Disabled()
{
//Soft delete is enabled by default
var people = _personRepository.GetList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
using (_dataFilter.Disable<ISoftDelete>())
{
//Soft delete is disabled
people = _personRepository.GetList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeTrue();
using (_dataFilter.Enable<ISoftDelete>())
{
//Soft delete is enabled again
people = _personRepository.GetList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
}
//Soft delete is disabled (restored previous state)
people = _personRepository.GetList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeTrue();
}
//Soft delete is enabled (restored previous state)
people = _personRepository.GetList();
people.Any(p => !p.IsDeleted).ShouldBeTrue();
people.Any(p => p.IsDeleted).ShouldBeFalse();
}
}
}

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TestApp;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.MemoryDb.DataFilters
{
public class MemoryDb_MultiTenant_Filter_Tests : MemoryDbTestBase
{
private ICurrentTenant _fakeCurrentTenant;
private readonly IRepository<Person> _personRepository;
private readonly IDataFilter<IMultiTenant> _multiTenantFilter;
public MemoryDb_MultiTenant_Filter_Tests()
{
_personRepository = GetRequiredService<IRepository<Person>>();
_multiTenantFilter = GetRequiredService<IDataFilter<IMultiTenant>>();
}
protected override void AfterAddApplication(IServiceCollection services)
{
_fakeCurrentTenant = Substitute.For<ICurrentTenant>();
services.AddSingleton(_fakeCurrentTenant);
}
[Fact]
public async Task Should_Get_Person_For_Current_Tenant()
{
//TenantId = null
_fakeCurrentTenant.Id.Returns((Guid?)null);
var people = await _personRepository.GetListAsync();
people.Count.ShouldBe(1);
people.Any(p => p.Name == "Douglas").ShouldBeTrue();
//TenantId = TestDataBuilder.TenantId1
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId1);
people = await _personRepository.GetListAsync();
people.Count.ShouldBe(2);
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person1").ShouldBeTrue();
people.Any(p => p.Name == TestDataBuilder.TenantId1 + "-Person2").ShouldBeTrue();
//TenantId = TestDataBuilder.TenantId2
_fakeCurrentTenant.Id.Returns(TestDataBuilder.TenantId2);
people = await _personRepository.GetListAsync();
people.Count.ShouldBe(0);
}
[Fact]
public async Task Should_Get_All_People_When_MultiTenant_Filter_Is_Disabled()
{
List<Person> people;
using (_multiTenantFilter.Disable())
{
//Filter disabled manually
people = await _personRepository.GetListAsync();
people.Count.ShouldBe(3);
}
//Filter re-enabled automatically
people = await _personRepository.GetListAsync();
people.Count.ShouldBe(1);
}
}
}
Loading…
Cancel
Save