From 8f3c527126e6c5e89bf9c96a65910807bac8f6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 23 Dec 2020 20:14:13 +0300 Subject: [PATCH] Use async methods in the mongo repo. --- .../Repositories/MongoDB/MongoDbRepository.cs | 86 +++++++++++-------- .../MongoDbCoreRepositoryExtensions.cs | 24 +++++- 2 files changed, 74 insertions(+), 36 deletions(-) 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 52b78eb7a3..ab0c063191 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 @@ -114,28 +114,33 @@ namespace Volo.Abp.Domain.Repositories.MongoDB public override async Task InsertManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { - foreach (var entity in entities) + var entityArray = entities.ToArray(); + + foreach (var entity in entityArray) { await ApplyAbpConceptsForAddedEntityAsync(entity); } + var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); + var collection = dbContext.Collection(); + if (BulkOperationProvider != null) { - await BulkOperationProvider.InsertManyAsync(this, entities, SessionHandle, autoSave, cancellationToken); + await BulkOperationProvider.InsertManyAsync(this, entityArray, dbContext.SessionHandle, autoSave, cancellationToken); return; } - if (SessionHandle != null) + if (dbContext.SessionHandle != null) { - await Collection.InsertManyAsync( - SessionHandle, - entities, + await collection.InsertManyAsync( + dbContext.SessionHandle, + entityArray, cancellationToken: cancellationToken); } else { - await Collection.InsertManyAsync( - entities, + await collection.InsertManyAsync( + entityArray, cancellationToken: cancellationToken); } } @@ -193,12 +198,13 @@ namespace Volo.Abp.Domain.Repositories.MongoDB public override async Task UpdateManyAsync(IEnumerable entities, bool autoSave = false, CancellationToken cancellationToken = default) { - var isSoftDeleteEntity = typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)); + var entityArray = entities.ToArray(); - foreach (var entity in entities) + foreach (var entity in entityArray) { SetModificationAuditProperties(entity); + var isSoftDeleteEntity = typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)); if (isSoftDeleteEntity) { SetDeletionAuditProperties(entity); @@ -214,31 +220,34 @@ namespace Volo.Abp.Domain.Repositories.MongoDB SetNewConcurrencyStamp(entity); } + cancellationToken = GetCancellationToken(cancellationToken); + var dbContext = await GetDbContextAsync(cancellationToken); + if (BulkOperationProvider != null) { - await BulkOperationProvider.UpdateManyAsync(this, entities, SessionHandle, autoSave, cancellationToken); + await BulkOperationProvider.UpdateManyAsync(this, entityArray, dbContext.SessionHandle, autoSave, cancellationToken); return; } - var entitiesCount = entities.Count(); BulkWriteResult result; List> replaceRequests = new List>(); - foreach (var entity in entities) + foreach (var entity in entityArray) { replaceRequests.Add(new ReplaceOneModel(CreateEntityFilter(entity), entity)); } - if (SessionHandle != null) + var collection = dbContext.Collection(); + if (dbContext.SessionHandle != null) { - result = await Collection.BulkWriteAsync(SessionHandle, replaceRequests); + result = await collection.BulkWriteAsync(dbContext.SessionHandle, replaceRequests, cancellationToken: cancellationToken); } else { - result = await Collection.BulkWriteAsync(replaceRequests); + result = await collection.BulkWriteAsync(replaceRequests, cancellationToken: cancellationToken); } - if (result.MatchedCount < entitiesCount) + if (result.MatchedCount < entityArray.Length) { ThrowOptimisticConcurrencyException(); } @@ -315,35 +324,40 @@ namespace Volo.Abp.Domain.Repositories.MongoDB bool autoSave = false, CancellationToken cancellationToken = default) { - foreach (var entity in entities) + var entityArray = entities.ToArray(); + + foreach (var entity in entityArray) { await ApplyAbpConceptsForDeletedEntityAsync(entity); - var oldConcurrencyStamp = SetNewConcurrencyStamp(entity); + SetNewConcurrencyStamp(entity); } + var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); + var collection = dbContext.Collection(); + if (BulkOperationProvider != null) { - await BulkOperationProvider.DeleteManyAsync(this, entities, SessionHandle, autoSave, cancellationToken); + await BulkOperationProvider.DeleteManyAsync(this, entityArray, dbContext.SessionHandle, autoSave, cancellationToken); return; } - var entitiesCount = entities.Count(); + var entitiesCount = entityArray.Count(); if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) { UpdateResult updateResult; - if (SessionHandle != null) + if (dbContext.SessionHandle != null) { - updateResult = await Collection.UpdateManyAsync( - SessionHandle, - CreateEntitiesFilter(entities), + updateResult = await collection.UpdateManyAsync( + dbContext.SessionHandle, + CreateEntitiesFilter(entityArray), Builders.Update.Set(x => ((ISoftDelete)x).IsDeleted, true) ); } else { - updateResult = await Collection.UpdateManyAsync( - CreateEntitiesFilter(entities), + updateResult = await collection.UpdateManyAsync( + CreateEntitiesFilter(entityArray), Builders.Update.Set(x => ((ISoftDelete)x).IsDeleted, true) ); } @@ -356,17 +370,17 @@ namespace Volo.Abp.Domain.Repositories.MongoDB else { DeleteResult deleteResult; - if (SessionHandle != null) + if (dbContext.SessionHandle != null) { - deleteResult = await Collection.DeleteManyAsync( - SessionHandle, - CreateEntitiesFilter(entities) + deleteResult = await collection.DeleteManyAsync( + dbContext.SessionHandle, + CreateEntitiesFilter(entityArray) ); } else { - deleteResult = await Collection.DeleteManyAsync( - CreateEntitiesFilter(entities) + deleteResult = await collection.DeleteManyAsync( + CreateEntitiesFilter(entityArray) ); } @@ -689,9 +703,11 @@ namespace Volo.Abp.Domain.Repositories.MongoDB public virtual async Task DeleteManyAsync([NotNull] IEnumerable ids, bool autoSave = false, CancellationToken cancellationToken = default) { - var entities = await GetMongoQueryable() + cancellationToken = GetCancellationToken(cancellationToken); + + var entities = await (await GetMongoQueryableAsync(cancellationToken)) .Where(x => ids.Contains(x.Id)) - .ToListAsync(GetCancellationToken(cancellationToken)); + .ToListAsync(cancellationToken); await DeleteManyAsync(entities, autoSave, cancellationToken); } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs index 12da0afaa2..ff367dc554 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using MongoDB.Driver; using MongoDB.Driver.Linq; using Volo.Abp.Domain.Entities; @@ -8,24 +9,45 @@ namespace Volo.Abp.Domain.Repositories { public static class MongoDbCoreRepositoryExtensions { + [Obsolete("Use GetDatabaseAsync method.")] public static IMongoDatabase GetDatabase(this IBasicRepository repository) where TEntity : class, IEntity { return repository.ToMongoDbRepository().Database; } + public static Task GetDatabaseAsync(this IBasicRepository repository) + where TEntity : class, IEntity + { + return repository.ToMongoDbRepository().GetDatabaseAsync(); + } + + [Obsolete("Use GetCollection method.")] public static IMongoCollection GetCollection(this IBasicRepository repository) where TEntity : class, IEntity { return repository.ToMongoDbRepository().Collection; } + public static Task> GetCollectionAsync(this IBasicRepository repository) + where TEntity : class, IEntity + { + return repository.ToMongoDbRepository().GetCollectionAsync(); + } + + [Obsolete("Use GetMongoQueryableAsync method.")] public static IMongoQueryable GetMongoQueryable(this IBasicRepository repository) where TEntity : class, IEntity { return repository.ToMongoDbRepository().GetMongoQueryable(); } + public static Task> GetMongoQueryableAsync(this IBasicRepository repository) + where TEntity : class, IEntity + { + return repository.ToMongoDbRepository().GetMongoQueryableAsync(); + } + public static IMongoDbRepository ToMongoDbRepository(this IBasicRepository repository) where TEntity : class, IEntity { @@ -38,4 +60,4 @@ namespace Volo.Abp.Domain.Repositories return mongoDbRepository; } } -} \ No newline at end of file +}