Merge pull request #4606 from abpframework/maliming/mongodb-patch

Make MongoDbRepository convertible to IMongoDbRepository<TEntity>.
pull/4612/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit 73f963f179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,7 +19,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
{
public class MongoDbRepository<TMongoDbContext, TEntity>
: RepositoryBase<TEntity>,
IMongoDbRepository<TEntity>
IMongoDbRepository<TEntity>,
IMongoQueryable<TEntity>
where TMongoDbContext : IAbpMongoDbContext
where TEntity : class, IEntity
{
@ -167,7 +168,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
}
public override async Task<TEntity> FindAsync(
Expression<Func<TEntity, bool>> predicate,
Expression<Func<TEntity, bool>> predicate,
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
@ -322,6 +323,35 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
{
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.");
}
/// <summary>
/// IMongoQueryable<TEntity>
/// </summary>
/// <returns></returns>
public QueryableExecutionModel GetExecutionModel()
{
return GetMongoQueryable().GetExecutionModel();
}
/// <summary>
/// IMongoQueryable<TEntity>
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public IAsyncCursor<TEntity> ToCursor(CancellationToken cancellationToken = new CancellationToken())
{
return GetMongoQueryable().ToCursor(cancellationToken);
}
/// <summary>
/// IMongoQueryable<TEntity>
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IAsyncCursor<TEntity>> ToCursorAsync(CancellationToken cancellationToken = new CancellationToken())
{
return GetMongoQueryable().ToCursorAsync(cancellationToken);
}
}
public class MongoDbRepository<TMongoDbContext, TEntity, TKey>
@ -331,7 +361,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
where TEntity : class, IEntity<TKey>
{
public IMongoDbRepositoryFilterer<TEntity, TKey> RepositoryFilterer { get; set; }
public MongoDbRepository(IMongoDbContextProvider<TMongoDbContext> dbContextProvider)
: base(dbContextProvider)
{
@ -376,4 +406,4 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return RepositoryFilterer.CreateEntityFilter(entity, withConcurrencyStamp, concurrencyStamp);
}
}
}
}

@ -8,6 +8,7 @@ using Volo.Abp.DependencyInjection;
using Volo.Abp.Linq;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Volo.Abp.DynamicProxy;
namespace Volo.Abp.MongoDB
{
@ -15,12 +16,12 @@ namespace Volo.Abp.MongoDB
{
public bool CanExecute<T>(IQueryable<T> queryable)
{
return queryable.Provider.GetType().Namespace?.StartsWith("MongoDB") ?? false;
return ProxyHelper.UnProxy(queryable) is IMongoQueryable<T>;
}
protected IMongoQueryable<T> GetMongoQueryable<T>(IQueryable<T> queryable)
protected virtual IMongoQueryable<T> GetMongoQueryable<T>(IQueryable<T> queryable)
{
return (IMongoQueryable<T>) queryable;
return ProxyHelper.UnProxy(queryable).As<IMongoQueryable<T>>();
}
public Task<bool> ContainsAsync<T>(IQueryable<T> queryable, T item, CancellationToken cancellationToken = default)

@ -0,0 +1,76 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.TestApp.Domain;
using Volo.Abp.TestApp.Testing;
using Volo.Abp.Uow;
using Xunit;
namespace Volo.Abp.MongoDB.Repositories
{
[Collection(MongoTestCollection.Name)]
public class MongoDbAsyncQueryableProvider_Tests : TestAppTestBase<AbpMongoDbTestModule>
{
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IRepository<Person, Guid> _personRepository;
private readonly MongoDbAsyncQueryableProvider _mongoDbAsyncQueryableProvider;
public MongoDbAsyncQueryableProvider_Tests()
{
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
_personRepository = GetRequiredService<IRepository<Person, Guid>>();
_mongoDbAsyncQueryableProvider = GetRequiredService<MongoDbAsyncQueryableProvider>();
}
[Fact]
public void CanExecute()
{
_mongoDbAsyncQueryableProvider.CanExecute(_personRepository).ShouldBeTrue();
_mongoDbAsyncQueryableProvider.CanExecute(_personRepository.WithDetails()).ShouldBeTrue();
}
[Fact]
public async Task FirstOrDefaultAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
(await _mongoDbAsyncQueryableProvider.FirstOrDefaultAsync(_personRepository.Where(p => p.Name == "Douglas"))).ShouldNotBeNull();
await uow.CompleteAsync();
}
}
[Fact]
public async Task AnyAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
(await _mongoDbAsyncQueryableProvider.AnyAsync(_personRepository, p => p.Name == "Douglas")).ShouldBeTrue();
await uow.CompleteAsync();
}
}
[Fact]
public async Task CountAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
(await _mongoDbAsyncQueryableProvider.CountAsync(_personRepository.Where(p => p.Name == "Douglas"))).ShouldBeGreaterThan(0);
await uow.CompleteAsync();
}
}
[Fact]
public async Task LongCountAsync()
{
using (var uow = _unitOfWorkManager.Begin())
{
(await _mongoDbAsyncQueryableProvider.LongCountAsync(_personRepository)).ShouldBeGreaterThan(0);
await uow.CompleteAsync();
}
}
//More MongoDbAsyncQueryableProvider's method test.
}
}

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver.Linq;
using Shouldly;
using Volo.Abp.TestApp;
using Volo.Abp.TestApp.Domain;
@ -12,6 +13,15 @@ namespace Volo.Abp.MongoDB.Repositories
[Collection(MongoTestCollection.Name)]
public class Repository_Basic_Tests : Repository_Basic_Tests<AbpMongoDbTestModule>
{
[Fact]
public void ToMongoQueryable_Test()
{
((IMongoQueryable<Person>) PersonRepository).ShouldNotBeNull();
PersonRepository.As<IMongoQueryable<Person>>().ShouldNotBeNull();
((IMongoQueryable<Person>) PersonRepository.Where(p => p.Name == "Douglas")).ShouldNotBeNull();
PersonRepository.Where(p => p.Name == "Douglas").As<IMongoQueryable<Person>>().ShouldNotBeNull();
}
[Fact]
public async Task Linq_Queries()
{

Loading…
Cancel
Save