Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base aggregation API to MongoDB repository #7464

Merged
merged 2 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/en/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public async override Task DeleteAsync(

### Access to the MongoDB API

In most cases, you want to hide MongoDB APIs behind a repository (this is the main purpose of the repository). However, if you want to access the MongoDB API over the repository, you can use `GetDatabaseAsync()` or `GetCollectionAsync()` extension methods. Example:
In most cases, you want to hide MongoDB APIs behind a repository (this is the main purpose of the repository). However, if you want to access the MongoDB API over the repository, you can use `GetDatabaseAsync()`, `GetCollectionAsync()` or `GetAggregateAsync()` extension methods. Example:

```csharp
public class BookService
Expand All @@ -270,6 +270,7 @@ public class BookService
{
IMongoDatabase database = await _bookRepository.GetDatabaseAsync();
IMongoCollection<Book> books = await _bookRepository.GetCollectionAsync();
IAggregateFluent<Book> bookAggregate = await _bookRepository.GetAggregateAsync();
}
}
```
Expand Down
9 changes: 5 additions & 4 deletions docs/zh-Hans/MongoDB.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async override Task DeleteAsync(

#### 访问MongoDB API

大多数情况下,你想要将MongoDB API隐藏在仓储后面(这是仓储的主要目的).如果你想在仓储之上访问MongoDB API,你可以使用`GetDatabase()`或`GetCollection()`方法.例如:
大多数情况下,你想要将MongoDB API隐藏在仓储后面(这是仓储的主要目的).如果你想在仓储之上访问MongoDB API,你可以使用`GetDatabaseAsync()`, `GetAggregateAsync()` 或`GetCollectionAsync()`方法.例如:

```csharp
public class BookService
Expand All @@ -223,10 +223,11 @@ public class BookService
_bookRepository = bookRepository;
}

public void Foo()
public async Task FooAsync()
{
IMongoDatabase database = _bookRepository.GetDatabase();
IMongoCollection<Book> books = _bookRepository.GetCollection();
IMongoDatabase database = await _bookRepository.GetDatabaseAsync();
IMongoCollection<Book> books = await _bookRepository.GetCollectionAsync();
IAggregateFluent<Book> bookAggregate = await _bookRepository.GetAggregateAsync();
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface IMongoDbRepository<TEntity> : IRepository<TEntity>
IMongoQueryable<TEntity> GetMongoQueryable();

Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync(CancellationToken cancellationToken = default);

Task<IAggregateFluent<TEntity>> GetAggregateAsync(CancellationToken cancellationToken = default);
}

public interface IMongoDbRepository<TEntity, TKey> : IMongoDbRepository<TEntity>, IRepository<TEntity, TKey>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Volo.Abp.EventBus.Local;
using Volo.Abp.Guids;
using Volo.Abp.MongoDB;
using Volo.Abp.MultiTenancy;

namespace Volo.Abp.Domain.Repositories.MongoDB
{
Expand Down Expand Up @@ -473,6 +474,17 @@ public async Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync(CancellationT
);
}

public async Task<IAggregateFluent<TEntity>> GetAggregateAsync(CancellationToken cancellationToken = default)
{
var dbContext = await GetDbContextAsync(cancellationToken);
var collection = await GetCollectionAsync(cancellationToken);

return ApplyDataFilters(
dbContext.SessionHandle != null
? collection.Aggregate(dbContext.SessionHandle)
: collection.Aggregate());
}

protected virtual bool IsHardDeleted(TEntity entity)
{
var hardDeletedEntities = UnitOfWorkManager?.Current?.Items.GetOrDefault(UnitOfWorkItemNames.HardDeletedEntities) as HashSet<IEntity>;
Expand Down Expand Up @@ -621,6 +633,22 @@ 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 virtual IAggregateFluent<TEntity> ApplyDataFilters(IAggregateFluent<TEntity> aggregate)
{
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled<ISoftDelete>())
{
aggregate = aggregate.Match(e => ((ISoftDelete)e).IsDeleted == false);
}

if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled<IMultiTenant>())
{
var tenantId = CurrentTenant.Id;
aggregate = aggregate.Match(e => ((IMultiTenant)e).TenantId == tenantId);
}

return aggregate;
}

[Obsolete("This method will be removed in future versions.")]
public QueryableExecutionModel GetExecutionModel()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
Expand All @@ -16,10 +17,10 @@ public static class MongoDbCoreRepositoryExtensions
return repository.ToMongoDbRepository().Database;
}

public static Task<IMongoDatabase> GetDatabaseAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository)
public static Task<IMongoDatabase> GetDatabaseAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository, CancellationToken cancellationToken = default)
where TEntity : class, IEntity<TKey>
{
return repository.ToMongoDbRepository().GetDatabaseAsync();
return repository.ToMongoDbRepository().GetDatabaseAsync(cancellationToken);
}

[Obsolete("Use GetCollectionAsync method.")]
Expand All @@ -29,10 +30,10 @@ public static class MongoDbCoreRepositoryExtensions
return repository.ToMongoDbRepository().Collection;
}

public static Task<IMongoCollection<TEntity>> GetCollectionAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository)
public static Task<IMongoCollection<TEntity>> GetCollectionAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository, CancellationToken cancellationToken = default)
where TEntity : class, IEntity<TKey>
{
return repository.ToMongoDbRepository().GetCollectionAsync();
return repository.ToMongoDbRepository().GetCollectionAsync(cancellationToken);
}

[Obsolete("Use GetMongoQueryableAsync method.")]
Expand All @@ -42,10 +43,16 @@ public static class MongoDbCoreRepositoryExtensions
return repository.ToMongoDbRepository().GetMongoQueryable();
}

public static Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository)
public static Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository, CancellationToken cancellationToken = default)
where TEntity : class, IEntity<TKey>
{
return repository.ToMongoDbRepository().GetMongoQueryableAsync();
return repository.ToMongoDbRepository().GetMongoQueryableAsync(cancellationToken);
}

public static Task<IAggregateFluent<TEntity>> GetAggregateAsync<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository, CancellationToken cancellationToken = default)
where TEntity : class, IEntity<TKey>
{
return repository.ToMongoDbRepository().GetAggregateAsync(cancellationToken);
}

public static IMongoDbRepository<TEntity, TKey> ToMongoDbRepository<TEntity, TKey>(this IBasicRepository<TEntity, TKey> repository)
Expand Down