Skip to content

Commit

Permalink
Add FilterQueryable method to IMongoDbRepositoryFilterer.
Browse files Browse the repository at this point in the history
Resolve #17203
  • Loading branch information
maliming committed Jul 27, 2023
1 parent a2875f1 commit 5032cf3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;
using Volo.Abp.Domain.Entities;
Expand All @@ -8,6 +9,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB;
public interface IMongoDbRepositoryFilterer<TEntity> where TEntity : class, IEntity
{
Task AddGlobalFiltersAsync(List<FilterDefinition<TEntity>> filters);

TQueryable FilterQueryable<TQueryable>(TQueryable query) where TQueryable : IQueryable<TEntity>;
}

public interface IMongoDbRepositoryFilterer<TEntity, TKey> : IMongoDbRepositoryFilterer<TEntity> where TEntity : class, IEntity<TKey>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -99,6 +98,8 @@ protected Task<TMongoDbContext> GetDbContextAsync(CancellationToken cancellation

public IMongoDbBulkOperationProvider BulkOperationProvider => LazyServiceProvider.LazyGetService<IMongoDbBulkOperationProvider>();

public IMongoDbRepositoryFilterer<TEntity> RepositoryFilterer => LazyServiceProvider.LazyGetService<IMongoDbRepositoryFilterer<TEntity>>();

public MongoDbRepository(IMongoDbContextProvider<TMongoDbContext> dbContextProvider)
{
DbContextProvider = dbContextProvider;
Expand Down Expand Up @@ -738,6 +739,15 @@ protected virtual void ThrowOptimisticConcurrencyException()
{
throw new AbpDbConcurrencyException("Database operation expected to affect 1 row but actually affected 0 row. Data may have been modified or deleted since entities were loaded. This exception has been thrown on optimistic concurrency check.");
}

protected override TQueryable ApplyDataFilters<TQueryable, TOtherEntity>(TQueryable query)
{
if (typeof(TOtherEntity) == typeof(TEntity))
{
return base.ApplyDataFilters<TQueryable, TOtherEntity>((TQueryable)RepositoryFilterer.FilterQueryable(query.As<IQueryable<TEntity>>()));
}
return base.ApplyDataFilters<TQueryable, TOtherEntity>(query);
}
}

public class MongoDbRepository<TMongoDbContext, TEntity, TKey>
Expand All @@ -746,7 +756,7 @@ public class MongoDbRepository<TMongoDbContext, TEntity, TKey>
where TMongoDbContext : IAbpMongoDbContext
where TEntity : class, IEntity<TKey>
{
public IMongoDbRepositoryFilterer<TEntity, TKey> RepositoryFilterer { get; set; }
public IMongoDbRepositoryFilterer<TEntity, TKey> RepositoryFiltererWithKey => LazyServiceProvider.LazyGetService<IMongoDbRepositoryFilterer<TEntity, TKey>>();

public MongoDbRepository(IMongoDbContextProvider<TMongoDbContext> dbContextProvider)
: base(dbContextProvider)
Expand Down Expand Up @@ -798,13 +808,23 @@ public virtual async Task DeleteManyAsync([NotNull] IEnumerable<TKey> ids, bool
await DeleteManyAsync(entities, autoSave, cancellationToken);
}

protected override TQueryable ApplyDataFilters<TQueryable, TOtherEntity>(TQueryable query)
{
if (typeof(TOtherEntity) == typeof(TEntity))
{
return base.ApplyDataFilters<TQueryable, TOtherEntity>((TQueryable)RepositoryFiltererWithKey.FilterQueryable(query.As<IQueryable<TEntity>>()));
}

return base.ApplyDataFilters<TQueryable, TOtherEntity>(query);
}

protected async override Task<FilterDefinition<TEntity>> CreateEntityFilterAsync(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null)
{
return await RepositoryFilterer.CreateEntityFilterAsync(entity, withConcurrencyStamp, concurrencyStamp);
return await RepositoryFiltererWithKey.CreateEntityFilterAsync(entity, withConcurrencyStamp, concurrencyStamp);
}

protected async override Task<FilterDefinition<TEntity>> CreateEntitiesFilterAsync(IEnumerable<TEntity> entities, bool withConcurrencyStamp = false)
{
return await RepositoryFilterer.CreateEntitiesFilterAsync(entities, withConcurrencyStamp);
return await RepositoryFiltererWithKey.CreateEntitiesFilterAsync(entities, withConcurrencyStamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public virtual Task AddGlobalFiltersAsync(List<FilterDefinition<TEntity>> filter

return Task.CompletedTask;
}

public virtual TQueryable FilterQueryable<TQueryable>(TQueryable query) where TQueryable : IQueryable<TEntity>
{
return query;
}
}

public class MongoDbRepositoryFilterer<TEntity, TKey> : MongoDbRepositoryFilterer<TEntity>,
Expand Down Expand Up @@ -99,4 +104,9 @@ public virtual async Task<FilterDefinition<TEntity>> CreateEntitiesFilterAsync(I

return Builders<TEntity>.Filter.And(filters);
}

public virtual TQueryable FilterQueryable<TQueryable>(TQueryable query) where TQueryable : IQueryable<TEntity>
{
return query;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MultiTenancy;
using Volo.Abp.TestApp.Domain;
using Xunit;

namespace Volo.Abp.MongoDB.DataFiltering;

[Collection(MongoTestCollection.Name)]
public class MongoDbAsyncQueryableProvider_Tests : MongoDbTestBase
{
private readonly IRepository<City, Guid> _personRepository;

protected override void AfterAddApplication(IServiceCollection services)
{
services.AddTransient<IMongoDbRepositoryFilterer<City, Guid>, TestFilterQueryable>();
base.AfterAddApplication(services);
}

public MongoDbAsyncQueryableProvider_Tests()
{
_personRepository = GetRequiredService<IRepository<City, Guid>>();
}

[Fact]
public async Task Test()
{
var cities = await _personRepository.GetListAsync();
cities.Count.ShouldBe(1);
cities[0].Name.ShouldBe("Istanbul");
}
}

public class TestFilterQueryable : MongoDbRepositoryFilterer<City, Guid>
{
public TestFilterQueryable(
IDataFilter dataFilter,
ICurrentTenant currentTenant) :
base(dataFilter, currentTenant)
{
}

public override TQueryable FilterQueryable<TQueryable>(TQueryable query)
{
return (TQueryable)query.Where(p => p.Name == "Istanbul");
}
}

0 comments on commit 5032cf3

Please sign in to comment.