mirror of https://github.com/abpframework/abp.git
committed by
GitHub
4 changed files with 112 additions and 45 deletions
@ -0,0 +1,18 @@ |
|||
using System.Collections.Generic; |
|||
using MongoDB.Driver; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories.MongoDB |
|||
{ |
|||
public interface IMongoDbRepositoryFilterer<TEntity> where TEntity : class, IEntity |
|||
{ |
|||
void AddGlobalFilters(List<FilterDefinition<TEntity>> filters); |
|||
} |
|||
|
|||
public interface IMongoDbRepositoryFilterer<TEntity, TKey> : IMongoDbRepositoryFilterer<TEntity> where TEntity : class, IEntity<TKey> |
|||
{ |
|||
FilterDefinition<TEntity> CreateEntityFilter(TKey id, bool applyFilters = false); |
|||
|
|||
FilterDefinition<TEntity> CreateEntityFilter(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null); |
|||
} |
|||
} |
|||
@ -0,0 +1,79 @@ |
|||
using System.Collections.Generic; |
|||
using MongoDB.Driver; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.Domain.Repositories.MongoDB |
|||
{ |
|||
public class MongoDbRepositoryFilterer<TEntity> : IMongoDbRepositoryFilterer<TEntity> |
|||
where TEntity : class, IEntity |
|||
{ |
|||
protected IDataFilter DataFilter { get; } |
|||
|
|||
protected ICurrentTenant CurrentTenant { get; } |
|||
|
|||
public MongoDbRepositoryFilterer(IDataFilter dataFilter, ICurrentTenant currentTenant) |
|||
{ |
|||
DataFilter = dataFilter; |
|||
CurrentTenant = currentTenant; |
|||
} |
|||
|
|||
public virtual void AddGlobalFilters(List<FilterDefinition<TEntity>> filters) |
|||
{ |
|||
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled<ISoftDelete>()) |
|||
{ |
|||
filters.Add(Builders<TEntity>.Filter.Eq(e => ((ISoftDelete) e).IsDeleted, false)); |
|||
} |
|||
|
|||
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) |
|||
{ |
|||
var tenantId = CurrentTenant.Id; |
|||
filters.Add(Builders<TEntity>.Filter.Eq(e => ((IMultiTenant) e).TenantId, tenantId)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public class MongoDbRepositoryFilterer<TEntity, TKey> : MongoDbRepositoryFilterer<TEntity>, |
|||
IMongoDbRepositoryFilterer<TEntity, TKey> |
|||
where TEntity : class, IEntity<TKey> |
|||
{ |
|||
public MongoDbRepositoryFilterer(IDataFilter dataFilter, ICurrentTenant currentTenant) |
|||
: base(dataFilter, currentTenant) |
|||
{ |
|||
} |
|||
|
|||
public FilterDefinition<TEntity> CreateEntityFilter(TKey id, bool applyFilters = false) |
|||
{ |
|||
var filters = new List<FilterDefinition<TEntity>> |
|||
{ |
|||
Builders<TEntity>.Filter.Eq(e => e.Id, id) |
|||
}; |
|||
|
|||
if (applyFilters) |
|||
{ |
|||
AddGlobalFilters(filters); |
|||
} |
|||
|
|||
return Builders<TEntity>.Filter.And(filters); |
|||
} |
|||
|
|||
public FilterDefinition<TEntity> CreateEntityFilter(TEntity entity, bool withConcurrencyStamp = false, string concurrencyStamp = null) |
|||
{ |
|||
if (!withConcurrencyStamp || !(entity is IHasConcurrencyStamp entityWithConcurrencyStamp)) |
|||
{ |
|||
return Builders<TEntity>.Filter.Eq(e => e.Id, entity.Id); |
|||
} |
|||
|
|||
if (concurrencyStamp == null) |
|||
{ |
|||
concurrencyStamp = entityWithConcurrencyStamp.ConcurrencyStamp; |
|||
} |
|||
|
|||
return Builders<TEntity>.Filter.And( |
|||
Builders<TEntity>.Filter.Eq(e => e.Id, entity.Id), |
|||
Builders<TEntity>.Filter.Eq(e => ((IHasConcurrencyStamp) e).ConcurrencyStamp, concurrencyStamp) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue