@ -25,13 +25,37 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
where TMongoDbContext : IAbpMongoDbContext
where TEntity : class , IEntity
{
[Obsolete("Use GetCollectionAsync method.")]
public virtual IMongoCollection < TEntity > Collection = > DbContext . Collection < TEntity > ( ) ;
public async Task < IMongoCollection < TEntity > > GetCollectionAsync ( CancellationToken cancellationToken = default )
{
return ( await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ) . Collection < TEntity > ( ) ;
}
[Obsolete("Use GetDatabaseAsync method.")]
public virtual IMongoDatabase Database = > DbContext . Database ;
public virtual IClientSessionHandle SessionHandle = > DbContext . SessionHandle ;
public async Task < IMongoDatabase > GetDatabaseAsync ( CancellationToken cancellationToken = default )
{
return ( await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ) . Database ;
}
[Obsolete("Use GetSessionHandleAsync method.")]
protected virtual IClientSessionHandle SessionHandle = > DbContext . SessionHandle ;
protected async Task < IClientSessionHandle > GetSessionHandleAsync ( CancellationToken cancellationToken = default )
{
return ( await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ) . SessionHandle ;
}
[Obsolete("Use GetDbContextAsync method.")]
protected virtual TMongoDbContext DbContext = > DbContextProvider . GetDbContext ( ) ;
public virtual TMongoDbContext DbContext = > DbContextProvider . GetDbContext ( ) ;
protected Task < TMongoDbContext > GetDbContextAsync ( CancellationToken cancellationToken = default )
{
return DbContextProvider . GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ;
}
protected IMongoDbContextProvider < TMongoDbContext > DbContextProvider { get ; }
@ -55,24 +79,27 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
GuidGenerator = SimpleGuidGenerator . Instance ;
}
public async override Task < TEntity > InsertAsync (
public override async Task < TEntity > InsertAsync (
TEntity entity ,
bool autoSave = false ,
CancellationToken cancellationToken = default )
{
await ApplyAbpConceptsForAddedEntityAsync ( entity ) ;
if ( SessionHandle ! = null )
var dbContext = await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ;
var collection = dbContext . Collection < TEntity > ( ) ;
if ( dbContext . SessionHandle ! = null )
{
await Collection . InsertOneAsync (
SessionHandle ,
await c ollection. InsertOneAsync (
dbContext . SessionHandle ,
entity ,
cancellationToken : GetCancellationToken ( cancellationToken )
) ;
}
else
{
await C ollection. InsertOneAsync (
await c ollection. InsertOneAsync (
entity ,
cancellationToken : GetCancellationToken ( cancellationToken )
) ;
@ -81,7 +108,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity ;
}
public async override Task < TEntity > UpdateAsync (
public override async Task < TEntity > UpdateAsync (
TEntity entity ,
bool autoSave = false ,
CancellationToken cancellationToken = default )
@ -103,20 +130,21 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
var oldConcurrencyStamp = SetNewConcurrencyStamp ( entity ) ;
ReplaceOneResult result ;
if ( SessionHandle ! = null )
var dbContext = await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ;
var collection = dbContext . Collection < TEntity > ( ) ;
if ( dbContext . SessionHandle ! = null )
{
result = await Collection . ReplaceOneAsync (
SessionHandle ,
result = await c ollection. ReplaceOneAsync (
dbContext . SessionHandle ,
CreateEntityFilter ( entity , true , oldConcurrencyStamp ) ,
entity ,
cancellationToken : GetCancellationToken ( cancellationToken )
) ;
}
else
{
result = await C ollection. ReplaceOneAsync (
result = await c ollection. ReplaceOneAsync (
CreateEntityFilter ( entity , true , oldConcurrencyStamp ) ,
entity ,
cancellationToken : GetCancellationToken ( cancellationToken )
@ -131,7 +159,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity ;
}
public async override Task DeleteAsync (
public override async Task DeleteAsync (
TEntity entity ,
bool autoSave = false ,
CancellationToken cancellationToken = default )
@ -139,15 +167,18 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
await ApplyAbpConceptsForDeletedEntityAsync ( entity ) ;
var oldConcurrencyStamp = SetNewConcurrencyStamp ( entity ) ;
var dbContext = await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ;
var collection = dbContext . Collection < TEntity > ( ) ;
if ( entity is ISoftDelete softDeleteEntity & & ! IsHardDeleted ( entity ) )
{
softDeleteEntity . IsDeleted = true ;
ReplaceOneResult result ;
if ( SessionHandle ! = null )
if ( dbContext . SessionHandle ! = null )
{
result = await C ollection. ReplaceOneAsync (
SessionHandle ,
result = await c ollection. ReplaceOneAsync (
dbContext . SessionHandle ,
CreateEntityFilter ( entity , true , oldConcurrencyStamp ) ,
entity ,
cancellationToken : GetCancellationToken ( cancellationToken )
@ -155,7 +186,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
}
else
{
result = await C ollection. ReplaceOneAsync (
result = await c ollection. ReplaceOneAsync (
CreateEntityFilter ( entity , true , oldConcurrencyStamp ) ,
entity ,
cancellationToken : GetCancellationToken ( cancellationToken )
@ -171,17 +202,17 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
{
DeleteResult result ;
if ( SessionHandle ! = null )
if ( dbContext . SessionHandle ! = null )
{
result = await C ollection. DeleteOneAsync (
SessionHandle ,
result = await c ollection. DeleteOneAsync (
dbContext . SessionHandle ,
CreateEntityFilter ( entity , true , oldConcurrencyStamp ) ,
cancellationToken : GetCancellationToken ( cancellationToken )
) ;
}
else
{
result = await C ollection. DeleteOneAsync (
result = await c ollection. DeleteOneAsync (
CreateEntityFilter ( entity , true , oldConcurrencyStamp ) ,
GetCancellationToken ( cancellationToken )
) ;
@ -194,38 +225,44 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
}
}
public async override Task < List < TEntity > > GetListAsync ( bool includeDetails = false , CancellationToken cancellationToken = default )
public override async Task < List < TEntity > > GetListAsync ( bool includeDetails = false , CancellationToken cancellationToken = default )
{
return await GetMongoQueryable ( ) . ToListAsync ( GetCancellationToken ( cancellationToken ) ) ;
cancellationToken = GetCancellationToken ( cancellationToken ) ;
return await ( await GetMongoQueryableAsync ( cancellationToken ) ) . ToListAsync ( cancellationToken ) ;
}
public async override Task < long > GetCountAsync ( CancellationToken cancellationToken = default )
public override async Task < long > GetCountAsync ( CancellationToken cancellationToken = default )
{
return await GetMongoQueryable ( ) . LongCountAsync ( GetCancellationToken ( cancellationToken ) ) ;
cancellationToken = GetCancellationToken ( cancellationToken ) ;
return await ( await GetMongoQueryableAsync ( cancellationToken ) ) . LongCountAsync ( cancellationToken ) ;
}
public async override Task < List < TEntity > > GetPagedListAsync (
public override async Task < List < TEntity > > GetPagedListAsync (
int skipCount ,
int maxResultCount ,
string sorting ,
bool includeDetails = false ,
CancellationToken cancellationToken = default )
{
return await GetMongoQueryable ( )
cancellationToken = GetCancellationToken ( cancellationToken ) ;
return await ( await GetMongoQueryableAsync ( cancellationToken ) )
. OrderBy ( sorting )
. As < IMongoQueryable < TEntity > > ( )
. PageBy < TEntity , IMongoQueryable < TEntity > > ( skipCount , maxResultCount )
. ToListAsync ( GetCancellationToken ( cancellationToken ) ) ;
. ToListAsync ( cancellationToken ) ;
}
public async override Task DeleteAsync (
public override async Task DeleteAsync (
Expression < Func < TEntity , bool > > predicate ,
bool autoSave = false ,
CancellationToken cancellationToken = default )
{
var entities = await GetMongoQueryable ( )
cancellationToken = GetCancellationToken ( cancellationToken ) ;
var entities = await ( await GetMongoQueryableAsync ( cancellationToken ) )
. Where ( predicate )
. ToListAsync ( GetCancellationToken ( cancellationToken ) ) ;
. ToListAsync ( cancellationToken ) ;
foreach ( var entity in entities )
{
@ -233,25 +270,45 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
}
}
[Obsolete("Use GetQueryableAsync method.")]
protected override IQueryable < TEntity > GetQueryable ( )
{
return GetMongoQueryable ( ) ;
}
public async override Task < TEntity > FindAsync (
public override async Task < IQueryable < TEntity > > GetQueryableAsync ( )
{
return await GetMongoQueryableAsync ( ) ;
}
public override async Task < TEntity > FindAsync (
Expression < Func < TEntity , bool > > predicate ,
bool includeDetails = true ,
CancellationToken cancellationToken = default )
{
return await GetMongoQueryable ( )
return await ( await GetMongoQueryableAsync ( cancellationToken ) )
. Where ( predicate )
. SingleOrDefaultAsync ( GetCancellationToken ( cancellationToken ) ) ;
}
[Obsolete("Use GetMongoQueryableAsync method.")]
public virtual IMongoQueryable < TEntity > GetMongoQueryable ( )
{
return ApplyDataFilters ( SessionHandle ! = null ? Collection . AsQueryable ( SessionHandle ) : Collection . AsQueryable ( ) ) ;
}
public async Task < IMongoQueryable < TEntity > > GetMongoQueryableAsync ( CancellationToken cancellationToken = default )
{
var dbContext = await GetDbContextAsync ( cancellationToken ) ;
var collection = dbContext . Collection < TEntity > ( ) ;
return ApplyDataFilters (
dbContext . SessionHandle ! = null
? collection . AsQueryable ( dbContext . SessionHandle )
: collection . AsQueryable ( )
) ;
}
protected virtual bool IsHardDeleted ( TEntity entity )
{
var hardDeletedEntities = UnitOfWorkManager ? . Current ? . Items . GetOrDefault ( UnitOfWorkItemNames . HardDeletedEntities ) as HashSet < IEntity > ;
@ -393,30 +450,19 @@ 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>
[Obsolete("This method will be removed in future versions.")]
public QueryableExecutionModel GetExecutionModel ( )
{
return GetMongoQueryable ( ) . GetExecutionModel ( ) ;
}
/// <summary>
/// IMongoQueryable<TEntity>
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[Obsolete("This method will be removed in future versions.")]
public IAsyncCursor < TEntity > ToCursor ( CancellationToken cancellationToken = new CancellationToken ( ) )
{
return GetMongoQueryable ( ) . ToCursor ( cancellationToken ) ;
}
/// <summary>
/// IMongoQueryable<TEntity>
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[Obsolete("This method will be removed in future versions.")]
public Task < IAsyncCursor < TEntity > > ToCursorAsync ( CancellationToken cancellationToken = new CancellationToken ( ) )
{
return GetMongoQueryable ( ) . ToCursorAsync ( cancellationToken ) ;
@ -457,16 +503,21 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool includeDetails = true ,
CancellationToken cancellationToken = default )
{
if ( SessionHandle ! = null )
cancellationToken = GetCancellationToken ( cancellationToken ) ;
var dbContext = await GetDbContextAsync ( cancellationToken ) ;
var collection = dbContext . Collection < TEntity > ( ) ;
if ( dbContext . SessionHandle ! = null )
{
return await Collection
. Find ( SessionHandle , RepositoryFilterer . CreateEntityFilter ( id , true ) )
. FirstOrDefaultAsync ( GetCancellationToken ( cancellationToken ) ) ;
return await c ollection
. Find ( dbContext . SessionHandle , RepositoryFilterer . CreateEntityFilter ( id , true ) )
. FirstOrDefaultAsync ( cancellationToken ) ;
}
return await C ollection
return await c ollection
. Find ( RepositoryFilterer . CreateEntityFilter ( id , true ) )
. FirstOrDefaultAsync ( GetCancellationToken ( cancellationToken ) ) ;
. FirstOrDefaultAsync ( cancellationToken ) ;
}
public virtual Task DeleteAsync (