diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/IMongoDbRepositoryFilterer.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/IMongoDbRepositoryFilterer.cs index e1d01e60ac..a40329240b 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/IMongoDbRepositoryFilterer.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/IMongoDbRepositoryFilterer.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; using MongoDB.Driver; using Volo.Abp.Domain.Entities; @@ -6,14 +7,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB; public interface IMongoDbRepositoryFilterer where TEntity : class, IEntity { - void AddGlobalFilters(List> filters); + Task AddGlobalFiltersAsync(List> filters); } public interface IMongoDbRepositoryFilterer : IMongoDbRepositoryFilterer where TEntity : class, IEntity { - FilterDefinition CreateEntityFilter(TKey id, bool applyFilters = false); + Task> CreateEntityFilterAsync(TKey id, bool applyFilters = false); - FilterDefinition CreateEntityFilter(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null); + Task> CreateEntityFilterAsync(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null); /// /// Creates filter for given entities. @@ -24,7 +25,7 @@ public interface IMongoDbRepositoryFilterer : IMongoDbRepositoryF /// Entities to be filtered. /// Set true to use GlobalFilters. Default is false. /// Created . - FilterDefinition CreateEntitiesFilter(IEnumerable entities, bool applyFilters = false); + Task> CreateEntitiesFilterAsync(IEnumerable entities, bool applyFilters = false); /// /// Creates filter for given ids. @@ -34,5 +35,5 @@ public interface IMongoDbRepositoryFilterer : IMongoDbRepositoryF /// /// Entity Ids to be filtered. /// Set true to use GlobalFilters. Default is false. - FilterDefinition CreateEntitiesFilter(IEnumerable ids, bool applyFilters = false); + Task> CreateEntitiesFilterAsync(IEnumerable ids, bool applyFilters = false); } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 70ec100526..388330e69a 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -2,7 +2,6 @@ using JetBrains.Annotations; using MongoDB.Driver; using MongoDB.Driver.Linq; using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; @@ -105,7 +104,7 @@ public class MongoDbRepository DbContextProvider = dbContextProvider; } - public override async Task InsertAsync( + public async override Task InsertAsync( TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) @@ -136,7 +135,7 @@ public class MongoDbRepository return entity; } - public override async Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { cancellationToken = GetCancellationToken(cancellationToken); @@ -171,7 +170,7 @@ public class MongoDbRepository } } - public override async Task UpdateAsync( + public async override Task UpdateAsync( TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) @@ -202,7 +201,7 @@ public class MongoDbRepository { result = await collection.ReplaceOneAsync( dbContext.SessionHandle, - CreateEntityFilter(entity, true, oldConcurrencyStamp), + await CreateEntityFilterAsync(entity, true, oldConcurrencyStamp), entity, cancellationToken: cancellationToken ); @@ -210,7 +209,7 @@ public class MongoDbRepository else { result = await collection.ReplaceOneAsync( - CreateEntityFilter(entity, true, oldConcurrencyStamp), + await CreateEntityFilterAsync(entity, true, oldConcurrencyStamp), entity, cancellationToken: cancellationToken ); @@ -224,7 +223,7 @@ public class MongoDbRepository return entity; } - public override async Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) + public async override Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { var entityArray = entities.ToArray(); @@ -259,10 +258,10 @@ public class MongoDbRepository BulkWriteResult result; - List> replaceRequests = new List>(); + var replaceRequests = new List>(); foreach (var entity in entityArray) { - replaceRequests.Add(new ReplaceOneModel(CreateEntityFilter(entity), entity)); + replaceRequests.Add(new ReplaceOneModel(await CreateEntityFilterAsync(entity), entity)); } var collection = dbContext.Collection(); @@ -281,7 +280,7 @@ public class MongoDbRepository } } - public override async Task DeleteAsync( + public async override Task DeleteAsync( TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) @@ -304,7 +303,7 @@ public class MongoDbRepository { result = await collection.ReplaceOneAsync( dbContext.SessionHandle, - CreateEntityFilter(entity, true, oldConcurrencyStamp), + await CreateEntityFilterAsync(entity, true, oldConcurrencyStamp), entity, cancellationToken: cancellationToken ); @@ -312,7 +311,7 @@ public class MongoDbRepository else { result = await collection.ReplaceOneAsync( - CreateEntityFilter(entity, true, oldConcurrencyStamp), + await CreateEntityFilterAsync(entity, true, oldConcurrencyStamp), entity, cancellationToken: cancellationToken ); @@ -333,14 +332,14 @@ public class MongoDbRepository { result = await collection.DeleteOneAsync( dbContext.SessionHandle, - CreateEntityFilter(entity, true, oldConcurrencyStamp), + await CreateEntityFilterAsync(entity, true, oldConcurrencyStamp), cancellationToken: cancellationToken ); } else { result = await collection.DeleteOneAsync( - CreateEntityFilter(entity, true, oldConcurrencyStamp), + await CreateEntityFilterAsync(entity, true, oldConcurrencyStamp), cancellationToken ); } @@ -352,7 +351,7 @@ public class MongoDbRepository } } - public override async Task DeleteManyAsync( + public async override Task DeleteManyAsync( IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) @@ -391,10 +390,11 @@ public class MongoDbRepository { BulkWriteResult updateResult; - var replaceRequests = new List>( - softDeletedEntities.Select(entity => new ReplaceOneModel( - CreateEntityFilter(entity.Key, true, entity.Value), entity.Key)) - ); + var replaceRequests = new List>(); + foreach (var softDeletedEntity in softDeletedEntities) + { + replaceRequests.Add(new ReplaceOneModel(await CreateEntityFilterAsync(softDeletedEntity.Key, true, softDeletedEntity.Value), softDeletedEntity.Key)); + } if (dbContext.SessionHandle != null) { @@ -420,13 +420,13 @@ public class MongoDbRepository { deleteResult = await collection.DeleteManyAsync( dbContext.SessionHandle, - CreateEntitiesFilter(hardDeletedEntities), + await CreateEntitiesFilterAsync(hardDeletedEntities), cancellationToken: cancellationToken); } else { deleteResult = await collection.DeleteManyAsync( - CreateEntitiesFilter(hardDeletedEntities), + await CreateEntitiesFilterAsync(hardDeletedEntities), cancellationToken: cancellationToken); } @@ -437,25 +437,25 @@ public class MongoDbRepository } } - public override async Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) + public async override Task> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) { cancellationToken = GetCancellationToken(cancellationToken); return await (await GetMongoQueryableAsync(cancellationToken)).ToListAsync(cancellationToken); } - public override async Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) + public async override Task> GetListAsync(Expression> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) { cancellationToken = GetCancellationToken(cancellationToken); return await (await GetMongoQueryableAsync(cancellationToken)).Where(predicate).ToListAsync(cancellationToken); } - public override async Task GetCountAsync(CancellationToken cancellationToken = default) + public async override Task GetCountAsync(CancellationToken cancellationToken = default) { cancellationToken = GetCancellationToken(cancellationToken); return await (await GetMongoQueryableAsync(cancellationToken)).LongCountAsync(cancellationToken); } - public override async Task> GetPagedListAsync( + public async override Task> GetPagedListAsync( int skipCount, int maxResultCount, string sorting, @@ -471,7 +471,7 @@ public class MongoDbRepository .ToListAsync(cancellationToken); } - public override async Task DeleteAsync( + public async override Task DeleteAsync( Expression> predicate, bool autoSave = false, CancellationToken cancellationToken = default) @@ -491,12 +491,12 @@ public class MongoDbRepository return GetMongoQueryable(); } - public override async Task> GetQueryableAsync() + public async override Task> GetQueryableAsync() { return await GetMongoQueryableAsync(); } - public override async Task FindAsync( + public async override Task FindAsync( Expression> predicate, bool includeDetails = true, CancellationToken cancellationToken = default) @@ -544,10 +544,22 @@ public class MongoDbRepository var dbContext = await GetDbContextAsync(cancellationToken); var collection = await GetCollectionAsync(cancellationToken); - return ApplyDataFilters( - dbContext.SessionHandle != null + var aggregate = dbContext.SessionHandle != null ? collection.Aggregate(dbContext.SessionHandle) - : collection.Aggregate()); + : collection.Aggregate(); + + if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled()) + { + aggregate = aggregate.Match(e => ((ISoftDelete)e).IsDeleted == false); + } + + if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled()) + { + var tenantId = CurrentTenant.Id; + aggregate = aggregate.Match(e => ((IMultiTenant)e).TenantId == tenantId); + } + + return aggregate; } protected virtual bool IsHardDeleted(TEntity entity) @@ -561,17 +573,17 @@ public class MongoDbRepository return hardDeletedEntities.Contains(entity); } - protected virtual FilterDefinition CreateEntityFilter(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) + protected virtual Task> CreateEntityFilterAsync(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) { throw new NotImplementedException( - $"{nameof(CreateEntityFilter)} is not implemented for MongoDB by default. It should be overriden and implemented by the deriving class!" + $"{nameof(CreateEntityFilterAsync)} is not implemented for MongoDB by default. It should be overriden and implemented by the deriving class!" ); } - protected virtual FilterDefinition CreateEntitiesFilter(IEnumerable entities, bool withConcurrencyStamp = false) + protected virtual Task> CreateEntitiesFilterAsync(IEnumerable entities, bool withConcurrencyStamp = false) { throw new NotImplementedException( - $"{nameof(CreateEntitiesFilter)} is not implemented for MongoDB by default. It should be overriden and implemented by the deriving class!" + $"{nameof(CreateEntitiesFilterAsync)} is not implemented for MongoDB by default. It should be overriden and implemented by the deriving class!" ); } @@ -710,22 +722,6 @@ public class MongoDbRepository { 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 virtual IAggregateFluent ApplyDataFilters(IAggregateFluent aggregate) - { - if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled()) - { - aggregate = aggregate.Match(e => ((ISoftDelete)e).IsDeleted == false); - } - - if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled()) - { - var tenantId = CurrentTenant.Id; - aggregate = aggregate.Match(e => ((IMultiTenant)e).TenantId == tenantId); - } - - return aggregate; - } } public class MongoDbRepository @@ -764,19 +760,7 @@ public class MongoDbRepository { cancellationToken = GetCancellationToken(cancellationToken); - var dbContext = await GetDbContextAsync(cancellationToken); - var collection = dbContext.Collection(); - - if (dbContext.SessionHandle != null) - { - return await collection - .Find(dbContext.SessionHandle, RepositoryFilterer.CreateEntityFilter(id, true)) - .FirstOrDefaultAsync(cancellationToken); - } - - return await collection - .Find(RepositoryFilterer.CreateEntityFilter(id, true)) - .FirstOrDefaultAsync(cancellationToken); + return await ApplyDataFilters(await GetMongoQueryableAsync(cancellationToken)).Where(x => x.Id.Equals(id)).FirstOrDefaultAsync(cancellationToken); } public virtual Task DeleteAsync( @@ -798,13 +782,13 @@ public class MongoDbRepository await DeleteManyAsync(entities, autoSave, cancellationToken); } - protected override FilterDefinition CreateEntityFilter(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) + protected async override Task> CreateEntityFilterAsync(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) { - return RepositoryFilterer.CreateEntityFilter(entity, withConcurrencyStamp, concurrencyStamp); + return await RepositoryFilterer.CreateEntityFilterAsync(entity, withConcurrencyStamp, concurrencyStamp); } - protected override FilterDefinition CreateEntitiesFilter(IEnumerable entities, bool withConcurrencyStamp = false) + protected async override Task> CreateEntitiesFilterAsync(IEnumerable entities, bool withConcurrencyStamp = false) { - return RepositoryFilterer.CreateEntitiesFilter(entities, withConcurrencyStamp); + return await RepositoryFilterer.CreateEntitiesFilterAsync(entities, withConcurrencyStamp); } } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepositoryFilterer.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepositoryFilterer.cs index 2c21ae5502..451d30f0da 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepositoryFilterer.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepositoryFilterer.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using MongoDB.Driver; using Volo.Abp.Data; using Volo.Abp.Domain.Entities; @@ -20,7 +21,7 @@ public class MongoDbRepositoryFilterer : IMongoDbRepositoryFilterer> filters) + public virtual Task AddGlobalFiltersAsync(List> filters) { if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled()) { @@ -32,6 +33,8 @@ public class MongoDbRepositoryFilterer : IMongoDbRepositoryFilterer.Filter.Eq(e => ((IMultiTenant)e).TenantId, tenantId)); } + + return Task.CompletedTask; } } @@ -44,7 +47,7 @@ public class MongoDbRepositoryFilterer : MongoDbRepositoryFiltere { } - public FilterDefinition CreateEntityFilter(TKey id, bool applyFilters = false) + public virtual async Task> CreateEntityFilterAsync(TKey id, bool applyFilters = false) { var filters = new List> { @@ -53,17 +56,17 @@ public class MongoDbRepositoryFilterer : MongoDbRepositoryFiltere if (applyFilters) { - AddGlobalFilters(filters); + await AddGlobalFiltersAsync(filters); } return Builders.Filter.And(filters); } - public FilterDefinition CreateEntityFilter(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) + public virtual Task> CreateEntityFilterAsync(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) { if (!withConcurrencyStamp || !(entity is IHasConcurrencyStamp entityWithConcurrencyStamp)) { - return Builders.Filter.Eq(e => e.Id, entity.Id); + return Task.FromResult(Builders.Filter.Eq(e => e.Id, entity.Id)); } if (concurrencyStamp == null) @@ -71,18 +74,18 @@ public class MongoDbRepositoryFilterer : MongoDbRepositoryFiltere concurrencyStamp = entityWithConcurrencyStamp.ConcurrencyStamp; } - return Builders.Filter.And( + return Task.FromResult>(Builders.Filter.And( Builders.Filter.Eq(e => e.Id, entity.Id), Builders.Filter.Eq(e => ((IHasConcurrencyStamp)e).ConcurrencyStamp, concurrencyStamp) - ); + )); } - public FilterDefinition CreateEntitiesFilter(IEnumerable entities, bool applyFilters = false) + public virtual async Task> CreateEntitiesFilterAsync(IEnumerable entities, bool applyFilters = false) { - return CreateEntitiesFilter(entities.Select(s => s.Id), applyFilters); + return await CreateEntitiesFilterAsync(entities.Select(s => s.Id), applyFilters); } - public FilterDefinition CreateEntitiesFilter(IEnumerable ids, bool applyFilters = false) + public virtual async Task> CreateEntitiesFilterAsync(IEnumerable ids, bool applyFilters = false) { var filters = new List>() { @@ -91,7 +94,7 @@ public class MongoDbRepositoryFilterer : MongoDbRepositoryFiltere if (applyFilters) { - AddGlobalFilters(filters); + await AddGlobalFiltersAsync(filters); } return Builders.Filter.And(filters);