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

Upgrade MongoDB.Driver to 3.1.0. #21807

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<PackageVersion Include="EphemeralMongo6.runtime.linux-x64" Version="1.1.3" />
<PackageVersion Include="EphemeralMongo6.runtime.osx-x64" Version="1.1.3" />
<PackageVersion Include="EphemeralMongo6.runtime.win-x64" Version="1.1.3" />
<PackageVersion Include="MongoSandbox.Core" Version="1.0.1" />
<PackageVersion Include="MongoSandbox6.runtime.linux-x64" Version="1.0.1" />
<PackageVersion Include="MongoSandbox6.runtime.osx-x64" Version="1.0.1" />
<PackageVersion Include="MongoSandbox6.runtime.win-x64" Version="1.0.1" />
<PackageVersion Include="FluentValidation" Version="11.10.0" />
<PackageVersion Include="Google.Cloud.Storage.V1" Version="4.10.0" />
<PackageVersion Include="Hangfire.AspNetCore" Version="1.8.17" />
Expand Down Expand Up @@ -111,7 +115,7 @@
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="8.3.0" />
<PackageVersion Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.3.0" />
<PackageVersion Include="Minio" Version="6.0.3" />
<PackageVersion Include="MongoDB.Driver" Version="2.29.0" />
<PackageVersion Include="MongoDB.Driver" Version="3.1.0" />
<PackageVersion Include="NEST" Version="7.17.5" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Nito.AsyncEx.Context" Version="5.1.2" />
Expand Down
6 changes: 4 additions & 2 deletions build/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ if ($full -eq "-f")
"../templates/module/aspnet-core",
"../templates/app/aspnet-core",
"../templates/console",
"../templates/wpf",
"../templates/app-nolayers/aspnet-core",
"../abp_io/AbpIoLocalization",
"../source-code"
)
)
if ($env:OS -eq "Windows_NT") {
solutionPaths += "../templates/wpf"
}
}else{
Write-host ""
Write-host ":::::::::::::: !!! You are in development mode !!! ::::::::::::::" -ForegroundColor red -BackgroundColor yellow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public async Task<IdentityUser> FindByNormalizedUserNameAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync())
return await (await GetQueryableAsync())
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
GetCancellationToken(cancellationToken)
Expand All @@ -125,10 +125,10 @@ public async Task<IdentityUser> FindByNormalizedUserNameAsync(
`GetCancellationToken` fallbacks to the `ICancellationTokenProvider.Token` to obtain the cancellation token if it is not provided by the caller code.

* **Do** ignore the `includeDetails` parameters for the repository implementation since MongoDB loads the aggregate root as a whole (including sub collections) by default.
* **Do** use the `GetMongoQueryableAsync()` method to obtain an `IQueryable<TEntity>` to perform queries wherever possible. Because;
* `GetMongoQueryableAsync()` method automatically uses the `ApplyDataFilters` method to filter the data based on the current data filters (like soft delete and multi-tenancy).
* **Do** use the `GetQueryableAsync()` method to obtain an `IQueryable<TEntity>` to perform queries wherever possible. Because;
* `GetQueryableAsync()` method automatically uses the `ApplyDataFilters` method to filter the data based on the current data filters (like soft delete and multi-tenancy).
* Using `IQueryable<TEntity>` makes the code as much as similar to the EF Core repository implementation and easy to write and read.
* **Do** implement data filtering if it is not possible to use the `GetMongoQueryable()` method.
* **Do** implement data filtering if it is not possible to use the `GetQueryable()` method.

## Module Class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,6 @@ This method is suggested;

* If you are developing an application and you **don't plan to change** EF Core in the future, or you can **tolerate** it if you need to change it later. We believe that's reasonable if you are developing a final application.

#### MongoDB Case

If you are using [MongoDB](../../data/mongodb), you need to add the [Volo.Abp.MongoDB](https://www.nuget.org/packages/Volo.Abp.MongoDB) NuGet package to your project. Even in this case, you can't directly use async LINQ extensions (like `ToListAsync`) because MongoDB doesn't provide async extension methods for `IQueryable<T>`, but provides for `IMongoQueryable<T>`. You need to cast the query to `IMongoQueryable<T>` first to be able to use the async extension methods.

**Example: Cast `IQueryable<T>` to `IMongoQueryable<T>` and use `ToListAsync()`**

````csharp
var queryable = await _personRepository.GetQueryableAsync();
var people = ((IMongoQueryable<Person>) queryable
.Where(p => p.Name.Contains(nameFilter)))
.ToListAsync();
````

### Option-2: Use the IRepository Async Extension Methods

ABP provides async extension methods for the repositories, just similar to async LINQ extension methods.
Expand Down
8 changes: 4 additions & 4 deletions docs/en/guides/microservice-mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ Here we use `BookStore.ProductService` project as an example:
int skipCount = 0,
CancellationToken cancellationToken = default)
{
var query = ApplyFilter(await GetMongoQueryableAsync(cancellationToken), filterText, name, priceMin, priceMax);
var query = ApplyFilter(await GetQueryableAsync(cancellationToken), filterText, name, priceMin, priceMax);
query = query.OrderBy(string.IsNullOrWhiteSpace(sorting) ? ProductConsts.GetDefaultSorting(false) : sorting);
return await query.As<IMongoQueryable<Product>>().PageBy<Product, IMongoQueryable<Product>>(skipCount, maxResultCount).ToListAsync(cancellationToken);
return await query.PageBy(skipCount, maxResultCount).ToListAsync(cancellationToken);
}

public async Task<long> GetCountAsync(
Expand All @@ -109,8 +109,8 @@ Here we use `BookStore.ProductService` project as an example:
float? priceMax = null,
CancellationToken cancellationToken = default)
{
var query = ApplyFilter(await GetMongoQueryableAsync(cancellationToken), filterText, name, priceMin, priceMax);
return await query.As<IMongoQueryable<Product>>().LongCountAsync(GetCancellationToken(cancellationToken));
var query = ApplyFilter(await GetQueryableAsync(cancellationToken), filterText, name, priceMin, priceMax);
return await query.LongCountAsync(GetCancellationToken(cancellationToken));
}

protected virtual IQueryable<Product> ApplyFilter(
Expand Down
7 changes: 3 additions & 4 deletions docs/en/tutorials/book-store/part-07.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public class MongoDbAuthorRepository

public async Task<Author> FindByNameAsync(string name)
{
var queryable = await GetMongoQueryableAsync();
var queryable = await GetQueryableAsync();
return await queryable.FirstOrDefaultAsync(author => author.Name == name);
}

Expand All @@ -190,14 +190,13 @@ public class MongoDbAuthorRepository
string sorting,
string filter = null)
{
var queryable = await GetMongoQueryableAsync();
var queryable = await GetQueryableAsync();
return await queryable
.WhereIf<Author, IMongoQueryable<Author>>(
.WhereIf<Author, IQueryable<Author>>(
!filter.IsNullOrWhiteSpace(),
author => author.Name.Contains(filter)
)
.OrderBy(sorting)
.As<IMongoQueryable<Author>>()
.Skip(skipCount)
.Take(maxResultCount)
.ToListAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
Expand All @@ -20,10 +21,7 @@ public interface IMongoDbRepository<TEntity> : IRepository<TEntity>

Task<IMongoCollection<TEntity>> GetCollectionAsync(CancellationToken cancellationToken = default);

[Obsolete("Use GetMongoQueryableAsync method.")]
IMongoQueryable<TEntity> GetMongoQueryable();

Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync(CancellationToken cancellationToken = default, AggregateOptions? options = null);
Task<IQueryable<TEntity>> GetQueryableAsync(CancellationToken cancellationToken = default, AggregateOptions? options = null);

Task<IAggregateFluent<TEntity>> GetAggregateAsync(CancellationToken cancellationToken = default, AggregateOptions? options = null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,19 @@ await CreateEntitiesFilterAsync(hardDeletedEntities),
public async override Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);
return await (await GetMongoQueryableAsync(cancellationToken)).ToListAsync(cancellationToken);
return await (await GetQueryableAsync(cancellationToken)).ToListAsync(cancellationToken);
}

public async override Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = false, CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);
return await (await GetMongoQueryableAsync(cancellationToken)).Where(predicate).ToListAsync(cancellationToken);
return await (await GetQueryableAsync(cancellationToken)).Where(predicate).ToListAsync(cancellationToken);
}

public async override Task<long> GetCountAsync(CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);
return await (await GetMongoQueryableAsync(cancellationToken)).LongCountAsync(cancellationToken);
return await (await GetQueryableAsync(cancellationToken)).LongCountAsync(cancellationToken);
}

public async override Task<List<TEntity>> GetPagedListAsync(
Expand All @@ -479,10 +479,9 @@ public async override Task<List<TEntity>> GetPagedListAsync(
{
cancellationToken = GetCancellationToken(cancellationToken);

return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.OrderByIf<TEntity, IQueryable<TEntity>>(!sorting.IsNullOrWhiteSpace(), sorting)
.As<IMongoQueryable<TEntity>>()
.PageBy<TEntity, IMongoQueryable<TEntity>>(skipCount, maxResultCount)
.PageBy<TEntity, IQueryable<TEntity>>(skipCount, maxResultCount)
.ToListAsync(cancellationToken);
}

Expand All @@ -493,7 +492,7 @@ public async override Task DeleteAsync(
{
cancellationToken = GetCancellationToken(cancellationToken);

var entities = await (await GetMongoQueryableAsync(cancellationToken))
var entities = await (await GetQueryableAsync(cancellationToken))
.Where(predicate)
.ToListAsync(cancellationToken);

Expand Down Expand Up @@ -524,31 +523,20 @@ await collection.DeleteManyAsync(
}
}

[Obsolete("Use GetQueryableAsync method.")]
protected override IQueryable<TEntity> GetQueryable()
{
return GetMongoQueryable();
}

public async override Task<IQueryable<TEntity>> GetQueryableAsync()
{
return await GetMongoQueryableAsync();
}

public async override Task<TEntity?> FindAsync(
Expression<Func<TEntity, bool>> predicate,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
cancellationToken = GetCancellationToken(cancellationToken);

return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(predicate)
.SingleOrDefaultAsync(cancellationToken);
}

[Obsolete("Use GetMongoQueryableAsync method.")]
public virtual IMongoQueryable<TEntity> GetMongoQueryable()
[Obsolete("Use GetQueryableAsync method.")]
protected override IQueryable<TEntity> GetQueryable()
{
return ApplyDataFilters(
SessionHandle != null
Expand All @@ -557,19 +545,24 @@ public virtual IMongoQueryable<TEntity> GetMongoQueryable()
);
}

public virtual Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync(CancellationToken cancellationToken = default, AggregateOptions? aggregateOptions = null)
public async override Task<IQueryable<TEntity>> GetQueryableAsync()
{
return await GetQueryableAsync<TEntity>();
}

public async Task<IQueryable<TEntity>> GetQueryableAsync(CancellationToken cancellationToken = default, AggregateOptions? options = null)
{
return GetMongoQueryableAsync<TEntity>(cancellationToken, aggregateOptions);
return await GetQueryableAsync<TEntity>(cancellationToken, options);
}

protected virtual async Task<IMongoQueryable<TOtherEntity>> GetMongoQueryableAsync<TOtherEntity>(CancellationToken cancellationToken = default, AggregateOptions? aggregateOptions = null)
protected virtual async Task<IQueryable<TOtherEntity>> GetQueryableAsync<TOtherEntity>(CancellationToken cancellationToken = default, AggregateOptions? aggregateOptions = null)
{
cancellationToken = GetCancellationToken(cancellationToken);

var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TOtherEntity>();

return ApplyDataFilters<IMongoQueryable<TOtherEntity>, TOtherEntity>(
return ApplyDataFilters<IQueryable<TOtherEntity>, TOtherEntity>(
dbContext.SessionHandle != null
? collection.AsQueryable(dbContext.SessionHandle, aggregateOptions)
: collection.AsQueryable(aggregateOptions)
Expand Down Expand Up @@ -810,7 +803,7 @@ public virtual async Task<TEntity> GetAsync(
{
cancellationToken = GetCancellationToken(cancellationToken);

return await (await GetMongoQueryableAsync(cancellationToken))
return await (await GetQueryableAsync(cancellationToken))
.Where(x => x.Id!.Equals(id))
.FirstOrDefaultAsync(cancellationToken);
}
Expand All @@ -827,7 +820,7 @@ public virtual async Task DeleteManyAsync([NotNull] IEnumerable<TKey> ids, bool
{
cancellationToken = GetCancellationToken(cancellationToken);

var entities = await (await GetMongoQueryableAsync(cancellationToken))
var entities = await (await GetQueryableAsync(cancellationToken))
.Where(x => ids.Contains(x.Id))
.ToListAsync(cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
Expand Down Expand Up @@ -36,17 +37,17 @@ public static Task<IMongoCollection<TEntity>> GetCollectionAsync<TEntity>(this I
return repository.ToMongoDbRepository().GetCollectionAsync(cancellationToken);
}

[Obsolete("Use GetMongoQueryableAsync method.")]
public static IMongoQueryable<TEntity> GetMongoQueryable<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
[Obsolete("Use GetQueryableAsync method.")]
public static IQueryable<TEntity> GetQueryable<TEntity>(this IReadOnlyBasicRepository<TEntity> repository)
where TEntity : class, IEntity
{
return repository.ToMongoDbRepository().GetMongoQueryable();
return repository.ToMongoDbRepository().GetQueryable();
}

public static Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync<TEntity>(this IReadOnlyBasicRepository<TEntity> repository, CancellationToken cancellationToken = default, AggregateOptions? aggregateOptions = null)
public static Task<IQueryable<TEntity>> GetQueryableAsync<TEntity>(this IReadOnlyBasicRepository<TEntity> repository, CancellationToken cancellationToken = default, AggregateOptions? aggregateOptions = null)
where TEntity : class, IEntity
{
return repository.ToMongoDbRepository().GetMongoQueryableAsync(cancellationToken, aggregateOptions);
return repository.ToMongoDbRepository().GetQueryableAsync(cancellationToken, aggregateOptions);
}

public static Task<IAggregateFluent<TEntity>> GetAggregateAsync<TEntity>(this IReadOnlyBasicRepository<TEntity> repository, CancellationToken cancellationToken = default, AggregateOptions? aggregateOptions = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Volo.Abp.MongoDB;

public class AbpMongoDbDateTimeSerializer : DateTimeSerializer
public class AbpMongoDbDateTimeSerializer : StructSerializerBase<DateTime>
{
protected DateTimeKind DateTimeKind { get; set; }
protected bool DisableDateTimeNormalization { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using Volo.Abp.Domain;
using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.Modularity;
Expand All @@ -19,6 +22,8 @@ public override void PreConfigureServices(ServiceConfigurationContext context)

public override void ConfigureServices(ServiceConfigurationContext context)
{
BsonSerializer.TryRegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

context.Services.TryAddTransient(
typeof(IMongoDbContextProvider<>),
typeof(UnitOfWorkMongoDbContextProvider<>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public virtual async Task<List<IncomingEventInfo>> GetWaitingEventsAsync(int max
.WhereIf(transformedFilter != null, transformedFilter!)
.OrderBy(x => x.CreationTime)
.Take(maxCount)
.As<IMongoQueryable<IncomingEventRecord>>()
.ToListAsync(cancellationToken: cancellationToken);

return outgoingEventRecords
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public virtual async Task<List<OutgoingEventInfo>> GetWaitingEventsAsync(int max
.WhereIf(transformedFilter != null, transformedFilter!)
.OrderBy(x => x.CreationTime)
.Take(maxCount)
.As<IMongoQueryable<OutgoingEventRecord>>()
.ToListAsync(cancellationToken: cancellationToken);

return outgoingEventRecords
Expand Down
Loading
Loading