@ -27,13 +27,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 ;
public virtual TMongoDbContext DbContext = > DbContextProvider . GetDbContext ( ) ;
protected async Task < IClientSessionHandle > GetSessionHandleAsync ( CancellationToken cancellationToken = default )
{
return ( await GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ) . SessionHandle ;
}
[Obsolete("Use GetDbContextAsync method.")]
protected virtual TMongoDbContext DbContext = > DbContextProvider . GetDbContext ( ) ;
protected Task < TMongoDbContext > GetDbContextAsync ( CancellationToken cancellationToken = default )
{
return DbContextProvider . GetDbContextAsync ( GetCancellationToken ( cancellationToken ) ) ;
}
protected IMongoDbContextProvider < TMongoDbContext > DbContextProvider { get ; }
@ -59,24 +83,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 )
) ;
@ -87,33 +114,38 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
public override async Task InsertManyAsync ( IEnumerable < TEntity > 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 < TEntity > ( ) ;
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 C ollection. InsertManyAsync (
SessionHandle ,
entities ,
await c ollection. InsertManyAsync (
dbContext . SessionHandle ,
entityArray ,
cancellationToken : cancellationToken ) ;
}
else
{
await C ollection. InsertManyAsync (
entities ,
await c ollection. InsertManyAsync (
entityArray ,
cancellationToken : cancellationToken ) ;
}
}
public async override Task < TEntity > UpdateAsync (
public override async Task < TEntity > UpdateAsync (
TEntity entity ,
bool autoSave = false ,
CancellationToken cancellationToken = default )
@ -135,20 +167,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 )
@ -165,12 +198,13 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
public override async Task UpdateManyAsync ( IEnumerable < TEntity > 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 ) ;
@ -186,37 +220,40 @@ 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 < WriteModel < TEntity > > replaceRequests = new List < WriteModel < TEntity > > ( ) ;
foreach ( var entity in entities )
foreach ( var entity in entityArray )
{
replaceRequests . Add ( new ReplaceOneModel < TEntity > ( CreateEntityFilter ( entity ) , entity ) ) ;
}
if ( SessionHandle ! = null )
var collection = dbContext . Collection < TEntity > ( ) ;
if ( dbContext . SessionHandle ! = null )
{
result = await C ollection. BulkWriteAsync ( SessionHandle , replaceRequests ) ;
result = await c ollection. BulkWriteAsync ( dbContext . SessionHandle , replaceRequests , cancellationToken : cancellationToken ) ;
}
else
{
result = await C ollection. BulkWriteAsync ( replaceRequests ) ;
result = await c ollection. BulkWriteAsync ( replaceRequests , cancellationToken : cancellationToken ) ;
}
if ( result . MatchedCount < entitiesCount )
if ( result . MatchedCount < entityArray . Length )
{
ThrowOptimisticConcurrencyException ( ) ;
}
}
public async override Task DeleteAsync (
public override async Task DeleteAsync (
TEntity entity ,
bool autoSave = false ,
CancellationToken cancellationToken = default )
@ -224,15 +261,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 )
@ -240,7 +280,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 )
@ -256,17 +296,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 )
) ;
@ -284,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 < TEntity > ( ) ;
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 C ollection. UpdateManyAsync (
SessionHandle ,
CreateEntitiesFilter ( entities ) ,
updateResult = await c ollection. UpdateManyAsync (
dbContext . SessionHandle ,
CreateEntitiesFilter ( entityArray ) ,
Builders < TEntity > . Update . Set ( x = > ( ( ISoftDelete ) x ) . IsDeleted , true )
) ;
}
else
{
updateResult = await C ollection. UpdateManyAsync (
CreateEntitiesFilter ( entities ) ,
updateResult = await c ollection. UpdateManyAsync (
CreateEntitiesFilter ( entityArray ) ,
Builders < TEntity > . Update . Set ( x = > ( ( ISoftDelete ) x ) . IsDeleted , true )
) ;
}
@ -325,17 +370,17 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
else
{
DeleteResult deleteResult ;
if ( SessionHandle ! = null )
if ( dbContext . SessionHandle ! = null )
{
deleteResult = await C ollection. DeleteManyAsync (
SessionHandle ,
CreateEntitiesFilter ( entities )
deleteResult = await c ollection. DeleteManyAsync (
dbContext . SessionHandle ,
CreateEntitiesFilter ( entityArray )
) ;
}
else
{
deleteResult = await C ollection. DeleteManyAsync (
CreateEntitiesFilter ( entities )
deleteResult = await c ollection. DeleteManyAsync (
CreateEntitiesFilter ( entityArray )
) ;
}
@ -346,38 +391,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 )
{
@ -385,25 +436,49 @@ 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 ( ) ) ;
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 > ;
@ -552,30 +627,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 ) ;
@ -616,16 +680,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 (
@ -638,9 +707,11 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
public virtual async Task DeleteManyAsync ( [ NotNull ] IEnumerable < TKey > 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 ) ;
}